# 创建订单

## 创建订单

**API Method:**&#x20;

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

速率限制 (Rate limit): 每1秒15个请求。

**请求参数 (Request params):**

<table><thead><tr><th width="207.33333333333331">域</th><th width="99">类型</th><th width="439.6666666666667">描述</th></tr></thead><tbody><tr><td>resourceType</td><td>string</td><td>“ENERGY”或“BANDWIDTH”，默认： “ENERGY”</td></tr><tr><td>unitPrice</td><td>number</td><td>填写价格以SUN为单位</td></tr><tr><td>resourceAmount<mark style="color:red;">*</mark></td><td>number</td><td>资源数量</td></tr><tr><td>receiver<mark style="color:red;">*</mark></td><td>string</td><td>资源接收者的地址。</td></tr><tr><td>durationSec</td><td>number</td><td>购买资源的持续时间，时间单位为秒。默认值：259200（3天）。</td></tr><tr><td>sponsor</td><td>string</td><td>赞助代码</td></tr><tr><td>signedTx</td><td>SignedTransaction</td><td>已签名交易，请注意它是第2步中的JSON对象响应 (<a href="/pages/BNeWfKAe9TBO1jVApaMT">signed_tx ->步骤2</a> )</td></tr><tr><td>options</td><td>Object</td><td><em>optional</em></td></tr><tr><td>options.onlyCreateWhenFulfilled</td><td>Boolean</td><td><p>[true] => 仅在订单能够完成时创建订单</p><p>[false] => 即使订单无法完成也会创建订单</p><p>默认值：false</p></td></tr><tr><td>options.allowPartialFill</td><td>Boolean</td><td>允许订单部分匹配或整体匹配。</td></tr><tr><td>options.preventDuplicateIncompleteOrders</td><td>Boolean</td><td><p>[true]=>仅在不存在具有相同参数的未完成订单时创建订单。</p><p>[false]=>无论是否存在未完成订单，始终创建新订单。</p><p>默认值：false</p></td></tr><tr><td>options.maxPriceAccepted</td><td>number</td><td>仅在预估价格小于该值时创建订单。</td></tr><tr><td>options.minResourceDelegateRequiredAmount</td><td>number</td><td>单个提供者委托的最小能量数量。</td></tr></tbody></table>

**请求参数示例 (Request params example):**

```json
{
    "resourceType": "ENERGY",
    "receiver": "TFFbwz3UpmgaPT4UudwsxbiJf63t777777",
    "durationSec": 3600,
    "resourceAmount": 32000,
    "unitPrice": 80,
    "options": {
        "allowPartialFill": true,
        "onlyCreateWhenFulfilled": true,
        "preventDuplicateIncompleteOrders": false,
        "maxPriceAccepted": 100,
        "minResourceDelegateRequiredAmount": 100000
    },
    "signedTx": 
    {
        "visible": false,
        "txID": "795f8195893e8da2ef2f70fc3a1f2720f9077244c4b7b1c50c99c72fda675a32",
        "raw_data_hex": "0a02b32e2208ce1cb373c238875e4098fbd9a0eb325a68080112640a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412330a1541417ca6f74356a5d4454498e3f4856c945a04ab01121541055756f33f419278d9ea059bd2b21120e6add74818e0e5a40170b8a6d6a0eb32",
        "raw_data": {
            "contract": [
                {
                    "parameter": {
                        "value": {
                            "to_address": "41055756f33f419278d9ea059bd2b21120e6add748",
                            "owner_address": "41417ca6f74356a5d4454498e3f4856c945a04ab01",
                            "amount": 2700000
                        },
                        "type_url": "type.googleapis.com/protocol.TransferContract"
                    },
                    "type": "TransferContract"
                }
            ],
            "ref_block_bytes": "b32e",
            "ref_block_hash": "ce1cb373c238875e",
            "expiration": 1746778095000,
            "timestamp": 1746778035000
        },
        "signature": [
            "xxxxxxxxxxxxxxxxxxx"
        ]
    }
}
```

**回应 (Responses):**

```json
{
    "error": false,
    "message": "Success",
    "data": {
        "orderId": "6818426a65fa8ea36d119d2c"
    }
}
```

**示例代码 (*****Example Code):***

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

```javascript
const CreateOrder = async (resourceAmount, signedTx, receiverAddress, unitPrice, durationSec, options) => {
    const url = TRONSAVE_API_URL + "/v2/buy-resource";
    const body = {
        resourceType: RESOURCE_TYPE,
        resourceAmount,
        unitPrice,
        allowPartialFill: true,
        receiver: receiverAddress,
        durationSec,
        signedTx,
        options
    };
    const data = await fetch(url, {
        method: "POST",
        headers: {
            "content-type": "application/json",
        },
        body: JSON.stringify(body),
    });
    const response = await data.json();
    /**
     * Example response
     * {
     *     "error": false,
     *     "message": "Success",
     *     "data": {
     *         "orderId": "6809fdb7b9ba217a41d726fd"
     *     }
     * }
     */
    return response;
};
```

{% endtab %}

{% tab title="PHP" %}

```php
function createOrder($resourceAmount, $signedTx, $receiverAddress, $unitPrice, $durationSec) {
    $url = TRONSAVE_API_URL . "/v2/buy-resource";
    $body = [
        'resourceType' => RESOURCE_TYPE,
        'resourceAmount' => $resourceAmount,
        'unitPrice' => $unitPrice,
        'allowPartialFill' => true,
        'receiver' => $receiverAddress,
        'durationSec' => $durationSec,
        'signedTx' => $signedTx
    ];

    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => json_encode($body),
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => ['Content-Type: application/json']
    ]);

    $response = curl_exec($ch);
    curl_close($ch);
    return json_decode($response, true);
}
```

{% endtab %}

{% tab title="Python" %}

```python
def create_order(resource_amount: int, signed_tx: Dict[str, Any], 
                receiver_address: str, unit_price: int, duration_sec: int) -> Dict[str, Any]:
    url = f"{TRONSAVE_API_URL}/v2/buy-resource"
    body = {
        'resourceType': RESOURCE_TYPE,
        'resourceAmount': resource_amount,
        'unitPrice': unit_price,
        'allowPartialFill': True,
        'receiver': receiver_address,
        'durationSec': duration_sec,
        'signedTx': signed_tx
    }
    
    response = requests.post(url, json=body)
    return response.json()

```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tronsave.io/chinese/developer/buy-resources-v2/use-signed-transaction/create-order.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
