> ## Documentation Index
> Fetch the complete documentation index at: https://docs.paytring.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

> Learn how to authenticate and secure API requests using Paytring.

### Authentication

All API endpoints are authenticated using Bearer tokens. The token must be included in the `Authorization` header of each request.

You can generate this token using Basic Authentication with your API key and secret:

```javascript theme={null}
const basicAuthToken = btoa(`${API_KEY}:${API_SECRET}`);
```

### Hashing

`hash value can be "none" for v2 API`

Hashing is a process of generating a unique value or a fixed-length string representation of data. This technique is used to store and retrieve data more efficiently and securely. Below is the explanation of how to create a hash for v1 and v2 APIs.

#### How to Create a Hash?

Follow these steps to create a hash:

1. **Sort all the parameters.**
2. **Join all the parameter string values using a `|` sign.**
3. **Append the key secret at the end.**
4. **Convert this string into a hash using the `SHA512()` function.**

#### Example Code

```javascript theme={null}
const params = {
    amount: "100",
    currency: "INR",
    callback_url: "https://httpbin.org/post",
    cname: "John Doe",
    email: "johndoe@email.com",
    key: "YOUR_API_KEY",
    phone: "8930395227",
    receipt_id: "TXN0438400150988993",
    notes: {
        udf1: "udf1",
        udf2: "udf2"
    },
};

// Step 1: Sort the object
const sortedParams = Object.keys(params).sort().reduce((accumulator, key) => {
    accumulator[key] = params[key];
    return accumulator;
}, {});

// Step 2: Join all string values with |
let valueString = "";
const allValues = Object.values(sortedParams);
for (let i = 0; i < allValues.length; i++) {
    if (typeof allValues[i] !== "object") {
        valueString += allValues[i] + "|";
    }
}

// Step 3: Append key secret
valueString += "API_SECRET";

// Step 4: Create hash and add to the params
const hash = CryptoJS.SHA512(valueString).toString();
params.hash = hash;
```

### Steps to Create a Hash

1. **Sort all the parameters.**
   * Skip all object values.
2. **Join all the string values using a `|` sign.**
3. **Add `KEY_SECRET` at the end of the string.**
4. **Convert this string into a hash using the `SHA512()` function.**
