# Create order

To use this feature, you must have the API key. Read [here](https://docs.tronsave.io/developer/get-api-key) to view how to get our API key

[**Buy with API Key (Postman)**](https://www.postman.com/tronsave/workspace/tronsave/request/29749953-3882f194-909d-4977-b10c-eacfce32e8e4)

## Create a new buy energy order by API key

<mark style="color:orange;">`POST`</mark> `https://api.tronsave.io/v0/internal-buy-energy`

Create a new buy energy order by [API key](https://docs.tronsave.io/developer/get-api-key)

Rate limi&#x74;***: 15** requests per **1** second*

#### Headers

<table><thead><tr><th width="150">Name</th><th width="140">Type</th><th>Description</th></tr></thead><tbody><tr><td>apikey<mark style="color:red;">*</mark></td><td>String</td><td>Tronsave api key that present for internal account</td></tr></tbody></table>

#### Request Body

<table><thead><tr><th width="229">Name</th><th width="93">Type</th><th>Description</th></tr></thead><tbody><tr><td>resource_type<mark style="color:red;">*</mark></td><td>String</td><td>"ENERGY"</td></tr><tr><td>buy_energy_type<mark style="color:red;">*</mark></td><td>String</td><td><p>-"FAST" : If market ready to fill = 100%,  FAST = MEDIUM. If market ready to fill &#x3C; 100%,  FAST = MEDIUM + 10. If market ready to fill = 0%,  FAST = SLOW + 20.</p><p></p><p>-"MEDIUM" :  The lowest price for the maximum market fill for this order. If market ready to fill = 0%,  MEDIUM = SLOW + 10.</p><p></p><p>-"SLOW": The lowest price that can be set for this order.</p><p></p><p>-If price is number, price unit is equal to sun.</p><p></p></td></tr><tr><td>amount<mark style="color:red;">*</mark></td><td>Number</td><td>Amount of resource want to buy</td></tr><tr><td>allow_partial_fill<mark style="color:red;">*</mark></td><td>Boolean</td><td>if this value is true, order is able to filling from many delegator. Order can filling easier than if this value is false. Amount greater than 200k energy can set  this params</td></tr><tr><td>target_address<mark style="color:red;">*</mark></td><td>String</td><td>The address that received resource</td></tr><tr><td>duration_millisec</td><td>Number</td><td>order duration in milli sec. Default: 259200000 (3 days)</td></tr><tr><td>sponsor</td><td>String</td><td>sponsor code</td></tr><tr><td>only_create_when_fulfilled</td><td>Boolean</td><td><p>[true]=> order only create when it can be fulfilled</p><p>[false] => order will create even it can not be fulfilled</p><p>Default value: false</p></td></tr><tr><td>max_price_accepted</td><td>Number</td><td>Only create an order when the estimated price is less than this value.</td></tr><tr><td>add_order_incomplete</td><td>Boolean</td><td><p>[true]=> order only create when there has no same parameters order is not complete in order list</p><p>[false] => order will create even there has no same parameters order is not complete in order list</p><p>Default value: false</p></td></tr></tbody></table>

{% tabs %}
{% tab title="200: OK Success" %}
Return the order id if successful

```json
{
    "order_id": string,
    "requester": string, // the address represents the order owner
    "target": string, // the address of the resource that is received 
    "resource_amount": number, // the amount of resource
    "resource_type": string, // the amount type is "ENERGY"
    "remain_amount": number, // the remain amount can matching by system
    "price": number, // price unit is equal to sun
    "duration": number, // rent duration, duration unit is equal to second
    "allow_partial_fill": boolean, //Allow the order to be filled partially or not
    "payout_amount": number, // Total payout of this order
    "fulfilled_percent": number, //The percent that show filling processing. 0-100
}
```

{% endtab %}

{% tab title="400: Bad Request Invalid params" %}

```json
{
    "MISSING_PARAMS": "Missing some params in body",
    "INVALID_PARAMS": "some params is invalid",
    "ORDER_BUY_ENERGY_AMOUNT_TOO_SMALL": "order amount too small. Cannot less than 40000",
    "CANNOT_SET_PARTIAL_FULFILLED":"order amount less than 100000 cannot set partial fulfilled",
    "INTERNAL_ACCOUNT_NOT_FOUND":"internal account not exists",
    "ORDER_BUY_ENERGY_CAN_NOT_CREATE":"Something error occurs when create order, cannot create, please try later",
    "INTERNAL_BALANCE_ACCOUNT_TOO_LOW":"Balance is not enough"
}

```

{% endtab %}

{% tab title="401: Unauthorized Invalid api key" %}

```json
{
    "API_KEY_REQUIRED": "Missing api key in headers",
    "INVALID_API_KEY":"api key not correct"
}
```

{% endtab %}

{% tab title="429: Too Many Requests Rate limit reached" %}

```json
{
    "RATE_LIMIT": "Rate limit reached",
}
```

{% endtab %}
{% endtabs %}

*Example*&#x20;

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

```json
{
    "resource_type": "ENERGY",
    "buy_energy_type": "FAST",
    "amount": 100000,
    "allow_partial_fill": true,
    "target_address": "TKVSaJQDWeKFSEXmA44pjxduGTxy999999",
    "duration_millisec":86400000, //Optional
    "sponsor": <YOUR SPONSOR REF CODE> //Optional
    "only_create_when_fulfilled": false,
    "max_price_accepted": 100,
    "add_order_incomplete": false
}
```

{% endtab %}

{% tab title="Headers" %}

```json
{
  "apikey": <YOUR_API_KEY>
}
```

{% endtab %}

{% tab title="Success Response" %}

```json
{
      "order_id": "651d2306e55c073f6ca0992e",
      "requester": "TKVSaJQDWeKFSEXmA44pjxduGTxy999999",
      "target": "TKVSaJQDWeKFSEXmA44pjxduGTxy999999",
      "resource_amount": 100000,
      "resource_type": "ENERGY",
      "remain_amount": 0,
      "price": 67.5,
      "duration": 3600,
      "allow_partial_fill": true,
      "payout_amount": 6750000,
      "fulfilled_percent": 100
}
```

{% endtab %}
{% endtabs %}

*Example Code*

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

```javascript
const BuyEnergy = async (api_key, target_address, amount, duration_ms) => {
    const url = `https://api.tronsave.io/v0/internal-buy-energy`
    //see more at https://docs.tronsave.io/buy-energy-on-telegram/using-api-key-to/buy-energy
    const body = {
        "resource_type": "ENERGY",
        "buy_energy_type": "MEDIUM", //price in sun or "SLOW"|"MEDIUM"|"FAST"
        "amount": amount, //Amount of resource want to buy
        "allow_partial_fill": true,
        "target_address": target_address,
        "duration_millisec": duration_ms, //order duration in milli sec. Default: 259200000 (3 days)
        "only_create_when_fulfilled": false,
        "max_price_accepted": 100,
        "add_order_incomplete": false
    }
    const data = await fetch(url, {
        method: "POST",
        headers: {
            'apikey': api_key,
            "content-type": "application/json",
        },
        body: JSON.stringify(body)
    })
    const response = await data.json()
    /**
     * Example response 
     * @link  https://docs.tronsave.io/buy-energy-on-telegram/using-api-key-to/buy-energy
   {
      "order_id": "651d2306e55c073f6ca0992e",
      "requester": "TKVSaJQDWeKFSEXmA44pjxduGTxy999999",
      "target": "TKVSaJQDWeKFSEXmA44pjxduGTxy999999",
      "resource_amount": 100000,
      "resource_type": "ENERGY",
      "remain_amount": 0,
      "price": 67.5,
      "duration": 3600,
      "allow_partial_fill": true,
      "payout_amount": 6750000,
      "fulfilled_percent": 100
}
     */
    return response
}
```

{% endtab %}

{% tab title="cURL" %}

```powershell
curl --location 'https://api.tronsave.io/v0/internal-buy-energy' \
--header 'apikey: {{apikey}}' \
--data '{
    "resource_type": "ENERGY",
    "amount": 40000,
    "buy_energy_type": "MEDIUM", 
    "duration_millisec": 3600000, 
    "target_address": "{{yourAddress}}", 
    "allow_partial_fill": false, 
    "only_create_when_fulfilled": false,
    "max_price_accepted": 100,
    "add_order_incomplete": false
}
'
```

{% endtab %}
{% endtabs %}

Get detailed orders[ here](https://docs.tronsave.io/developer/buy-resources-v2/use-api-key/get-one-order-details)
