# Set Custom Conversion Rate

{% hint style="warning" %}

#### **Important Notes:**

The **Custom Conversion Rate** feature is restricted and must be explicitly enabled by an administrator. Only merchants with prior approval will be able to set custom crypto-to-fiat conversion rates.

If you wish to use this feature, please contact customer support or your account administrator to request access.
{% endhint %}

This endpoint allows a merchant to configure a custom exchange rate between a cryptocurrency (e.g., USDT) and a fiat currency (e.g., Brazilian Real).

For example, if your products or services are priced in Brazilian Real (BRL), but you want your customers to pay in USDT, you can define the conversion rate manually. By doing so, you control how many USDT the customer must pay for a given BRL-denominated price, instead of relying on live market rates.

This is particularly useful in the following scenarios:

* You want to apply a fixed or preferential rate for your customers.
* You operate in markets with volatile exchange rates.
* You wish to include your own markup or discount within the rate.

Once set, this custom rate will be used during the checkout or payment process for conversions from BRL to USDT.

**Endpoint**

<mark style="color:green;">**`POST`**</mark>`https://api.tylt.money/transactions/merchant/setRate`

**Request Headers**

{% tabs %}
{% tab title="First Tab" %}

<table data-full-width="true"><thead><tr><th width="133">Name</th><th width="79">Type</th><th width="167">Example</th><th>Description</th></tr></thead><tbody><tr><td>X-TLP-APIKEY</td><td>string</td><td>93ee3c5e133697251b5362bcf9cc8532476785t8768075616f58d88</td><td>Your Tylt API Key, used to identify your account in API requests.</td></tr><tr><td>X-TLP-SIGNATURE</td><td>string</td><td>d0afef3853dfc8489c8b9affa5825171fdd7y7685675e4966a05f66ed2b3eaf9462b3c9c0</td><td>HMAC SHA-256 signature generated using the API Secret Key to secure the request.</td></tr></tbody></table>
{% endtab %}
{% endtabs %}

{% hint style="info" %}
When using the API, ensure to include your API Key and generate the signature for the request payload using your API Secret. The tables provided above contain example values for illustration purposes only. Please refer to the code snippets for detailed instructions on how to sign the request and generate the signature properly.
{% endhint %}

**Request Body**

{% tabs %}
{% tab title="Body" %}

<table data-header-hidden><thead><tr><th width="204"></th><th width="121"></th><th></th></tr></thead><tbody><tr><td><strong>Field Name</strong></td><td><strong>Type</strong></td><td><strong>Description</strong></td></tr><tr><td><code>transactionType</code></td><td><code>string</code></td><td>Represents the type of transaction for which the custom rate should be applied (e.g., "payin" or "payout"). <strong>Note:</strong> If not specified, the default value is "payin".      </td></tr><tr><td><code>fromCurrencySymbol</code></td><td><code>string</code></td><td>Represents the symbol/code of the fiat currency (e.g., "BRL", "INR", "USD"). <strong>Note:</strong> <code>fromCurrencySymbol</code> must belong to a fiat currency</td></tr><tr><td><code>toCurrencySymbol</code></td><td><code>number</code></td><td>Represents the symbol/code of the crypto currency (e.g., "USDT", "USDC", "DAI"). <strong>Note:</strong> toCurrencySymbol must belong to a crypto currency</td></tr><tr><td><code>rate</code></td><td><code>number</code></td><td>The custom conversion rate. </td></tr><tr><td><code>isReciprocal</code></td><td><code>number</code></td><td><p>Determines the direction of the exchange rate you are providing.</p><ul><li>Set <code>isReciprocal = 0</code> if your rate means:<br><strong>1 unit of <code>fromCurrencySymbol</code> = </strong><em><strong>N</strong></em><strong> units of <code>toCurrencySymbol</code></strong><br>(e.g., 1 BRL = 0.20 USDT)</li><li>Set <code>isReciprocal = 1</code> if your rate means:<br><strong>1 unit of <code>toCurrencySymbol</code> = </strong><em><strong>N</strong></em><strong> units of <code>fromCurrencySymbol</code></strong><br>(e.g., 1 USDT = 5 BRL)</li></ul><p>This flag ensures clarity on whether the rate is expressed in a direct or inverse form.</p></td></tr></tbody></table>
{% endtab %}
{% endtabs %}

**Code Snippet**

{% tabs %}
{% tab title="JavaScript (Axios)" %}

```javascript
import axios from "axios";
import crypto from "crypto";

const apiKey = "your-api-key";
const apiSecret = "your-api-secret";

const data = {
    transactionType: "payin", 
    fromCurrencySymbol: "INR", // compulsory
    toCurrencySymbol: "USDT", // compulsory
    rate: 90.50, // compulsory
    isReceiprocal: 1
};

/*
    fromCurrencySymbol and toCurrencySymbol are compulsory
    rate is compulsory
    fromCurrencySymbol can only be of currenctType 'fiat'
    toCurrencySymbol can only be of currenctType 'crypto'

    currencyType of a currency can be checked here - 
    https://docs.tylt.money/tylt-cpg-crypto-payment-gateway/api-reference/supporting-apis/supported-base-currency-list

    For your reference - 
    fromCurrencySymbol can be 'INR' and other fiat currencies
    toCurrencySymbol can be 'USDT' and other crypto currencies
*/

const signaturePayload = JSON.stringify(data);
const signature = crypto
    .createHmac("sha256", apiSecret)
    .update(signaturePayload)
    .digest("hex");

const url = `https://api.tylt.money/transactions/merchant/setRate`;

const config = {
    method: "post",
    url: url,
    headers: {
        "X-TLP-APIKEY": apiKey,
        "X-TLP-SIGNATURE": signature,
    },
    data: data,
};

axios
    .request(config)
    .then((response) => {
        console.log(JSON.stringify(response.data));
    })
    .catch((error) => {
        console.error(error);
    });import requests
```

{% endtab %}

{% tab title="Python" %}

```python
import requests
import hmac
import hashlib
import json

# Replace with your API Key and Secret
api_key = 'your-api-key'
api_secret = 'your-api-secret'

# Request body (all fields are compulsory)
body = {
    "transactionType": "payin", 
    "fromCurrencySymbol": "INR",   # fiat
    "toCurrencySymbol": "USDT",   # crypto
    "rate": 90.50,
    "isReceiprocal": 1
}

# Prepare body for signature
body_string = json.dumps(body, separators=(',', ':'), ensure_ascii=False)

# Create the HMAC SHA-256 signature
signature = hmac.new(api_secret.encode(), body_string.encode(), hashlib.sha256).hexdigest()

# Define headers
headers = {
    "X-TLP-APIKEY": api_key,
    "X-TLP-SIGNATURE": signature,
    "Content-Type": "application/json"
}

# Send the POST request
response = requests.post("https://api.tylt.money/transactions/merchant/setRate", headers=headers, data=body_string)

# Print the response
if response.status_code == 200:
    print("Response:", response.json())
else:
    print(f"Failed with status code {response.status_code}: {response.text}")

```

{% endtab %}

{% tab title="JavaScript (Fetch)" %}

```javascript
const fetch = require('node-fetch');
const crypto = require('crypto');

// Replace with your API Key and Secret
const apiKey = 'your-api-key';
const apiSecret = 'your-api-secret';

// Request body for setting the rate
const requestBody = {
    transactionType: "payin", 
    fromCurrencySymbol: 'INR',  // fiat
    toCurrencySymbol: 'USDT',   // crypto
    rate: 90.50,
    isReceiprocal: 1
};

// Convert request body to JSON
const raw = JSON.stringify(requestBody);

// Function to create HMAC SHA-256 signature
const createSignature = (secret, data) => {
    return crypto.createHmac('sha256', secret)
                 .update(data)
                 .digest('hex');
};

// Generate signature
const signature = createSignature(apiSecret, raw);

// Define headers
const headers = {
    "Content-Type": "application/json",
    "X-TLP-APIKEY": apiKey,
    "X-TLP-SIGNATURE": signature
};

// Function to send the request
const sendRequest = async (url, headers, body) => {
    const response = await fetch(url, {
        method: 'POST',
        headers: headers,
        body: body,
    });

    if (!response.ok) {
        const errorText = await response.text();
        throw new Error(`HTTP ${response.status}: ${errorText}`);
    }

    return response.json();
};

// Send the request
sendRequest('https://api.tylt.money/transactions/merchant/setRate', headers, raw)
    .then(result => console.log("Success:", result))
    .catch(error => console.error("Error:", error));

```

{% endtab %}
{% endtabs %}

**Response**

{% tabs %}
{% tab title="200" %}

```json
{
    "data": {},
    "errorCode": 0,
    "msg": "Rate inserted successfully"
}
```

{% endtab %}
{% endtabs %}

***
