# Exchange

{% hint style="warning" %}
Every request listed on this page requires [authorization](https://docs.0xpay.app/public-api/endpoints/broken-reference) to be successfully completed.&#x20;
{% endhint %}

## Get available exchange directions

<mark style="color:blue;">`GET`</mark> `https://public.api.0xpay.app/merchants/exchange/available-directions/`

You should use this endpoint before any exchange operations for following reasons:

1.Get available directions for exchange through 0xpay **(tickers)**.\
2.Get 0xpay exchange limitations **(min, max)**.\
3.Find the way how you should format your swaps **(precision, step)**.

{% tabs %}
{% tab title="200: OK Balances retrieved" %}
In response you'll get an array of two objects: `target` and `exchange` with info about available assets, limits, and amount precision.&#x20;

`amountStep` indicates the smallest portion of asset that can be added/substracted from the exchange amount, any smaller digits will not be accepted for the exchange.&#x20;

For example: if `amountStep=0.001`, then **`valid`**` ``amount: 5.005`, and anything containing smaller digits is **`not valid`**`: 1.0005` .&#x20;

{% code overflow="wrap" %}

```javascript
[
    {
        "target": {
            "ticker": "BNB", //asset to receive
            "amountStep": "0.01000000", //exchange amount can be changed by at least 0.01 BNB
        },
        "exchange": {
            "ticker": "BUSD",//asset to spend
            "amountStep": "0.00010000", //exchange amount can be changed by at least 0.0001 BUSD 
            "max": "10000", //maximum exchange amount in BUSD
            "min": "0.00010000"//minimum exchange amount in BUSD
        }
    },
    {
        "target": {
            "ticker": "BUSD",
            "amountStep": "0.00010000"
        },
        "exchange": {
            "ticker": "BNB",
            "amountStep": "0.01000000",
            "max": "1000",
            "min": "0.01000000"
        }
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Estimate exchange

<mark style="color:blue;">`GET`</mark> `https://public.api.0xpay.app/merchants/exchange/estimate/`

You can use this endpoint to estimate your exchange for later [creation](#post-create-exchange)

#### Query Parameters

| Name                                           | Type   | Description                                                                                                               |
| ---------------------------------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------- |
| targetTicker<mark style="color:red;">\*</mark> | string | Asset that you want to receive to your balance. Find values using "List all supported assets" request + Fiat ticker `KZT` |
| spendTicker<mark style="color:red;">\*</mark>  | string | Asset that you want to spend from your balance. Find values using "List all supported assets" request + Fiat ticker `KZT` |
| amount<mark style="color:red;">\*</mark>       | string | amount in decimal format                                                                                                  |
| side<mark style="color:red;">\*</mark>         | string | Your chosen direction, two possible values: `target` or `spend`                                                           |
| price                                          | string | actual price of the pair                                                                                                  |

{% tabs %}
{% tab title="200: OK Exchange estimate calculated" %}
In response, you'll get details for your upcoming exchange:&#x20;

{% code overflow="wrap" %}

```javascript
{
    "price": { //object
        "value": "40", // price of the exchange pair
        "pair": ["UAH", "USDT"] // "Pair" is array of two "ticker" strings, values can be found in API reference

    },
    "exchange": { // detailed summary of operation
        "amount": "4030", //amount to be exchanged
        "ticker": "UAH", //ticker to spend for exchange
        "fee": "20" // transaction fee
    },
    "target": {
        "amount": "100", // amount to receive to balance
        "ticker": "USDT" // ticker to receive to balance
    },
    "spend": {
        "amount": "4020", // amount to spend, including fees
        "ticker": "UAH" // ticker to spend
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="400: Bad Request Description of error codes" %}
{% code overflow="wrap" %}

```javascript
//List of Error codes with descriptions
//0 – Order validation error
//1 – Ticker pair not allowed
//2 – Not enough liquidity
{
    "code": 1,
    "description": "Ticker pair not allowed"
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Create exchange operation

<mark style="color:green;">`POST`</mark> `https://public.api.0xpay.app/merchants/exchange`

Use this endpoint to create an exchange of two assets.

**Status Notifications:** After the **Success** response, 0xpay API will produce [notifications](https://docs.0xpay.app/notifications#exchange) according to status updates on your exchange.

#### Request Body

| Name                                           | Type   | Description                                                                                                                                                                                           |
| ---------------------------------------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| spendTicker<mark style="color:red;">\*</mark>  | string | <p>Ticker you want to spend (exchange) from your balance.</p><p>Find values using "List all supported assets" request + Fiat ticker <code>UAH</code></p>                                              |
| amount<mark style="color:red;">\*</mark>       | string | decimal format number presented as a string                                                                                                                                                           |
| targetTicker<mark style="color:red;">\*</mark> | string | Asset that you want to receive to your balance. Find values using "List all supported assets" request + Fiat ticker `UAH`                                                                             |
| side<mark style="color:red;">\*</mark>         | string | Your chosen direction, two possible values: `target` or `spend`                                                                                                                                       |
| fee<mark style="color:red;">\*</mark>          | string | you can specify withdrawal fees that have to be greater than the actual fee, which can be useful for better fee calculation options on your side. If not the specified –fee will be set automatically |
| price<mark style="color:red;">\*</mark>        | string | actual price of the pairs                                                                                                                                                                             |
| localId                                        | string | if was specified error will be thrown if not unique, can be useful on your side to avoid the issue                                                                                                    |
| meta                                           | string | metadata that you can define and catch it back later with notification                                                                                                                                |

{% tabs %}
{% tab title="200: OK Exchange has been created successfully" %}
{% code overflow="wrap" %}

```javascript
{
    "id": "123e4567-e89b-12d3-a456-426614174000" //internal ID has been assigned to the exchange
}
```

{% endcode %}
{% endtab %}

{% tab title="400: Bad Request Description of error codes" %}

<pre class="language-javascript"><code class="lang-javascript"><strong>//List of all error codes for exchange operations
</strong><strong>//0 – Local id already placed
</strong>//1 – Not enough balance
//3 – Order validation error
//4 – Ticker pair not allowed
//5 – Withdraw amount too low
//6 – Not enough liquidity
{
    "code": 5,
    "description": "Withdraw amount too low"
}
</code></pre>

{% endtab %}
{% endtabs %}
