# Exchange API

## API Endpoint

```
https://tronpulse.io/api/exchange
```

**Headers**

| Name         | Value              |
| ------------ | ------------------ |
| Content-Type | `application/json` |
| Api-Key      | `api-key`          |

## Exchange - Create Order

<mark style="color:green;">`POST`</mark> `/create_order`

**Body**

<table><thead><tr><th width="251">Name</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><code>order</code></td><td>json</td><td>Details of the order to be created (view JSON object description below)</td></tr><tr><td>  <code>order.receiver</code></td><td>string</td><td>Address to receiver the resource</td></tr><tr><td>  <code>order.resource_type</code></td><td>string</td><td>Resource type must be <code>energy</code></td></tr><tr><td>  <code>order.amount</code></td><td>number</td><td>Amount of resource to purchase</td></tr><tr><td>  <code>order.duration</code></td><td>string</td><td>Duration of the order: allowed values are <code>1h, 6h, 1d, 3d to 30d</code></td></tr><tr><td>  <code>order.price</code></td><td>number</td><td>Unit price of the order in SUN</td></tr><tr><td><code>signed_tx</code></td><td>json</td><td>Signed TRX payment transaction</td></tr></tbody></table>

**Response**

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

```json
{
    "status_code": "order_created",
    "data": {
        "order_id": "5527691da5524a45bb4bf268b87adc81"
    }
}
```

{% endtab %}

{% tab title="400" %}

```json
{
    "status_code": "status code of the error"
}
```

{% endtab %}
{% endtabs %}

**Code Example**

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

```javascript
// TronWeb library to interact with TRON blockchain
const tronweb = window.tronWeb;

const order = {
    "receiver": tronweb.defaultAddress.base58, // address to receiver energy
    "resource_type": "energy",
    "amount": 1000000, // amount of resource to purchase
    "duration": "3d", // allowed values are 1h, 6h, 1d, 3d to 30d
    "price": 65 // unit price in SUN - Check the latest minimum price on tronpule.io
}

// API endpoint
const url = 'https://tronpulse.io/api/exchange';

const options = {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        "Api-Key": "your-api-key"
    }
};

// Request API to compute order price
options.body = JSON.stringify({"order": order});
fetch(url + "/get_quote", options)
    .then(async response => {
        if (response.ok) {
            const quote = await response.json();
            // Build and sign payment transaction
            const tx = await tronweb.transactionBuilder.sendTrx(quote.data.payment_address, quote.data.order_total_price);
            const signed_tx = await tronweb.trx.sign(tx, private_key);
            // Request API to create order
            options.body = JSON.stringify({"order": order, "signed_tx": signed_tx});
            fetch(url + "/create_order", options)
                .then(async response => {
                    if (response.ok) {
                        // order successfuly created
                        const data = await response.json()
                        console.log(data)
                    }
                    else {
                        // request failed, display error
                        const data = await response.json()
                        console.log(data)
                    }
                })
        }
    })

```

{% endtab %}
{% endtabs %}

## Exchange - Get Order

<mark style="color:green;">`POST`</mark> `/get_order`

**Body**

| Name       | Type   | Description                               |
| ---------- | ------ | ----------------------------------------- |
| `order_id` | string | Order ID or "open" to get all open orders |

**Response**

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

```json
{
    "data": {
        "orders": [
            {
                "created_timestamp": 1713631199000,
                "order_id": "5527691da5524a45bb4bf268b87adc81",
                "status": "open",
                "receiver": "********",
                "duration": "3d",
                "resource_type": "energy",
                "amount": 1000000,
                "filled_amount": 0
            }
        ]
    }
}
```

{% endtab %}

{% tab title="400" %}

```json
{
    "status_code": "order_not_found"
}
```

{% endtab %}
{% endtabs %}

**Status Code**

| HTTP Status Code | Status Code               | Description                                                         |
| ---------------- | ------------------------- | ------------------------------------------------------------------- |
| 200              |                           | The request has been successfully processed.                        |
| 400              | order\_not\_found         | The specified order ID is unknown or does not exist.                |
| 400              | missing\_input\_parameter | The required parameter (order\_id) is not provided in the request.  |
| 401              | invalid\_api\_key         | The API key is either missing from the request header or incorrect. |

**Code Example**

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

```javascript
// TronWeb library to interact with TRON blockchain
const tronweb = window.tronWeb;

const order_id = "5527691da5524a45bb4bf268b87adc81" // or use 'open' to get all open orders

const url = 'https://tronpulse.io/api/exchange';

const options = {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        "Api-Key": "your-api-key"
    },
    body: JSON.stringify({"order_id": order_id})
};

// Request API to get orders
fetch(url + "/get_order", options)
    .then(async response => {
        if (response.ok) {
            // display orders
            const data = await response.json();
            console.log(data)
        }
        else {
            // request failed, display error
            const data = await response.json();
            console.log(data)
        }
    })
```

{% endtab %}
{% endtabs %}

## Exchange - Cancel Order

<mark style="color:green;">`POST`</mark> `/cancel_order`

**Body**

| Name       | Type   | Description |
| ---------- | ------ | ----------- |
| `order_id` | string | Order ID    |

**Response**

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

```json
{
    "status_code": "order_cancelled",
    "data": {
        "order_id": "5527691da5524a45bb4bf268b87adc81",
        "refund_tx": "******",
        "refund_amount": 164.0
    }
}
```

{% endtab %}

{% tab title="400" %}

```json
{
    "status_code": "cancellation_failed"
}
```

{% endtab %}
{% endtabs %}

**Code Example**

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

```javascript
// TronWeb library to interact with TRON blockchain
const tronweb = window.tronWeb;

const order_id = "5527691da5524a45bb4bf268b87adc81"

const url = 'https://tronpulse.io/api/exchange';

const options = {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        "Api-Key": "your-api-key"
    },
    body: JSON.stringify({"order_id": order_id})
};

// Request API to cancel order
fetch(url + "/cancel_order", options)
    .then(async response => {
        if (response.ok) {
            // order successfully cancelled 
            const data = await response.json();
            console.log(data)
        }
        else {
            // request failed, display error
            const data = await response.json();
            console.log(data)
        }
    })
```

{% endtab %}
{% endtabs %}
