# 获取内部账户订单历史

要使用此功能，您必须拥有 API 密钥。请阅读[**这里**](https://docs.tronsave.io/chinese/developer/get-api-key)以了解如何获取我们的 API 密钥。

## 通过 API 密钥获取历史

<mark style="color:blue;">`GET`</mark> `https://api.tronsave.io/v2/orders`

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

获取许多订单按创建时间排序。默认：返回10个最新订单

#### Query Parameters

<table><thead><tr><th width="181">Name</th><th width="200">Type</th><th>Description</th></tr></thead><tbody><tr><td>page</td><td>Integer</td><td>从0开始. Default: 0</td></tr><tr><td>pageSize</td><td>Integer</td><td>Default: 10</td></tr><tr><td>status</td><td>String</td><td>“Active” 或 “Completed”，默认获取全部</td></tr></tbody></table>

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

```javascript
{
    "error": false,
    "message": "Success",
    "data": {
        "id": string, // 订单 ID
        "requester": string, // 代表订单所有者的地址
        "receiver": string, // 接收资源的地址
        "resourceAmount": number, // 资源数量
        "resourceType": string, // 资源类型，可以是 "ENERGY" 或 "BANDWIDTH"
        "remainAmount": number, // 系统可以匹配的剩余数量
        "price": number, // 价格，单位为 SUN
        "durationSec": number, // 租赁时长，单位为秒
        "status": string, // 是订单的状态，表示为 Active（进行中）或 Completed（已完成）
        "orderType": string, // 订单类型，"NORMAL" 或 "EXTEND"
        "allowPartialFill": boolean, // 是否允许部分匹配
        "payoutAmount": number, // 订单的总支付金额
        "fulfilledPercent": number, // 显示填充进度的百分比，范围 0-100
        "delegates": [ // 所有匹配的委托人
            {
                "delegator": string, // 为目标地址委托资源的地址
                "amount": number, // 委托的资源数量
                "txid": number // 链上交易 ID
            }[]
        ]
    }[],
        "total": number
}

```

{% endtab %}
{% endtabs %}

**例子&#x20;*****(Example)***

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

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

{% endtab %}

{% tab title="Success Response" %}

```javascript
{
    "error": false,
    "message": "Success",
    "data": {
        "data": [
            {
                "id": "6819c7578729a45600f740d3",
                "requester": "TFwUFWr3QV376677Z8VWXxGUAMFSSSSSS",
                "receiver": "TFwUFWr3QV376677Z8VWXxGUAMFSSSSSS",
                "resourceAmount": 32000,
                "resourceType": "ENERGY",
                "remainAmount": 0,
                "orderType": "NORMAL",
                "price": 90,
                "durationSec": 300,
                "allowPartialFill": false,
                "payoutAmount": 2880000,
                "fulfilledPercent": 100,
                "delegates": [
                    {
                        "delegator": "THnnMCe67VMDXoivepiA7ZQSB8888888",
                        "amount": 32000,
                        "txid": "transaction_id_1"
                    }
                ]
            },
            {
                "id": "68198bcd8729a45600f740cf",
                "requester": "TFwUFWr3QV376677Z8VWXxGUAMFSSSSSS",
                "receiver": "TFwUFWr3QV376677Z8VWXxGUAMFSSSSSS",
                "resourceAmount": 1234,
                "resourceType": "BANDWIDTH",
                "remainAmount": 0,
                "orderType": "NORMAL",
                "price": 600,
                "durationSec": 900,
                "allowPartialFill": false,
                "payoutAmount": 740400,
                "fulfilledPercent": 100,
                "delegates": [
                    {
                        "delegator": "TMhiksDwSVjuxdXLwdNQEJpuFCLG77777",
                        "amount": 1234,
                        "txid": "transaction_id_2"
                    }
                ]
            }
        ],
        "total": 2
    }
}
```

{% endtab %}
{% endtabs %}

*Example Code*

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

```javascript
const GetOrderHistory = async (apiKey) => {
    const url = `${TRONSAVE_API_URL}/v2/orders`
    const data = await fetch(url, {
        headers: {
            'apikey': apiKey
        }
    })
    const response = await data.json()
     /**
     * Example response 
        {
            "error": false,
            "message": "Success",
            "data": 
            {
                "total": 2,
                "data": [
                    {
                        "id": "6809b08a14b1cb7c5d195d66",
                        "requester": "TTgMEAhuzPchDAL4pnm2tCNmqXp13AxzAd",
                        "receiver": "TFwUFWr3QV376677Z8VWXxGUAMFSrq1MbM",
                        "resourceAmount": 40000,
                        "resourceType": "ENERGY",
                        "remainAmount": 0,
                        "orderType": "NORMAL",
                        "price": 81.5,
                        "durationSec": 900,
                        "allowPartialFill": false,
                        "payoutAmount": 3260000,
                        "fulfilledPercent": 100,
                        "delegates": [
                            {
                                "delegator": "THnnMCe67VMDXoivepiA7ZQSB8jbgKDodf",
                                "amount": 40000,
                                "txid": "19be98d0183b29575d74999a93154b09b3c7d05051cdbd52c667cd9f0b3cc9b0"
                            }
                        ]
                    },
                    {
                        "id": "6809aaf2e2e17d3c588b467a",
                        "requester": "TTgMEAhuzPchDAL4pnm2tCNmqXp13AxzAd",
                        "receiver": "TFwUFWr3QV376677Z8VWXxGUAMFSrq1MbM",
                        "resourceAmount": 40000,
                        "resourceType": "ENERGY",
                        "remainAmount": 0,
                        "orderType": "NORMAL",
                        "price": 81.5,
                        "durationSec": 900,
                        "allowPartialFill": false,
                        "payoutAmount": 3260000,
                        "fulfilledPercent": 100,
                        "delegates": [
                            {
                                "delegator": "THnnMCe67VMDXoivepiA7ZQSB8jbgKDodf",
                                "amount": 40000,
                                "txid": "447e3fb28ad7580554642d08b9a6b220bc86f667b47edad47f16802594b6b1e3"
                            }
                        ]
                    },
                ]
            }
        }
     */
    return response
}
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php

/**
 * Get order history from the TRONSAVE API
 * 
 * @param string $apiKey API key for authentication
 * @return array{
 *     error: bool,
 *     message: string,
 *     data: array{
 *         total: int,
 *         data: array<array{
 *             id: string,
 *             requester: string,
 *             receiver: string,
 *             resourceAmount: int,
 *             resourceType: string,
 *             remainAmount: int,
 *             orderType: string,
 *             price: float,
 *             durationSec: int,
 *             allowPartialFill: bool,
 *             payoutAmount: int,
 *             fulfilledPercent: int,
 *             delegates: array<array{
 *                 delegator: string,
 *                 amount: int,
 *                 txid: string
 *             }>
 *         }>
 *     }
 * }
 * @throws Exception If API request fails
 */
function getOrderHistory(string $apiKey): array
{
    $url = TRONSAVE_API_URL . "/v2/orders";
    
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            'apikey: ' . $apiKey
        ]
    ]);

    $response = curl_exec($ch);
    
    if (curl_errno($ch)) {
        throw new Exception('Curl error: ' . curl_error($ch));
    }
    
    curl_close($ch);
    
    $result = json_decode($response, true);
    
    if (json_last_error() !== JSON_ERROR_NONE) {
        throw new Exception('JSON decode error: ' . json_last_error_msg());
    }
    
    return $result;
}

// Example usage:
try {
    $apiKey = 'your-api-key';
    $orderHistory = getOrderHistory($apiKey);
    
    // Print total orders
    echo "Total orders: " . $orderHistory['data']['total'] . "\n";
    
    // Print each order detail
    foreach ($orderHistory['data']['data'] as $order) {
        echo "Order ID: " . $order['id'] . "\n";
        echo "Resource Amount: " . $order['resourceAmount'] . "\n";
        echo "Resource Type: " . $order['resourceType'] . "\n";
        echo "Price: " . $order['price'] . "\n";
        echo "Fulfilled: " . $order['fulfilledPercent'] . "%\n";
        echo "-------------------\n";
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
```

{% endtab %}

{% tab title="Python" %}

```python
def get_order_history(api_key: str) -> dict:
    """Get order history from the TRONSAVE API"""
    url = "https://api.tronsave.io/v2/orders"
    response = requests.get(url, headers={'apikey': api_key})
    return response.json()

# Example usage
api_key = 'your-api-key'
result = get_order_history(api_key)

# Print results
if not result.get('error'):
    for order in result['data']['data']:
        print(f"Order ID: {order['id']}")
        print(f"Amount: {order['resourceAmount']}")
        print(f"Price: {order['price']}")
        print(f"Fulfilled: {order['fulfilledPercent']}%")
        print("-------------------")
```

{% endtab %}
{% endtabs %}
