# SDK 库

**TronsaveSDK** 是一个 TypeScript 库，帮助您轻松与 Tronsave API 交互，以管理 TRON 区块链上的资源（能量/带宽）。\
此 SDK 提供强大且强类型的函数，能够集成到现代后端系统中。

## Node.js SDK

支持 Node.js v18.0.0 或更高版本。

```bash
npm install tronsave-sdk
```

or

```bash
yarn add tronsave-sdk
```

### [View Tronsave SDK npm](https://www.npmjs.com/package/tronsave-sdk)

## 基本用法

*Example:*

{% tabs %}
{% tab title="Node.js" %}

```javascript
import { TronsaveSDK } from "tronsave-sdk";

const main = async () => {
    const apiKey = "your_api_key";
    const sdk = new TronsaveSDK({ network: "testnet", apiKey });
    const userInfo = await sdk.getUserInfo();
    console.log(userInfo);
    // Example: Estimate cost for 32,000 ENERGY units for 1 hour
    const estimate = await sdk.estimateBuyResource({
        receiver: "TAk6jzZqHwNUkUcbvMyAE1YAoUPk7r2T6h",
        resourceType: "ENERGY",
        durationSec: 3600, // 1 hour
        resourceAmount: 32000,
    });
    console.log(estimate);
    if (estimate.estimateTrx > Number(userInfo.balance)) throw new Error("Insufficient balance");
    const { orderId } = await sdk.buyResource({
        receiver: "TAk6jzZqHwNUkUcbvMyAE1YAoUPk7r2T6h",
        resourceType: "ENERGY",
        durationSec: 3600, // 1 hour
        resourceAmount: 32000,
    });
    console.log(`Buy resource success -> orderId: ${orderId}`);
    // Wait for 5 seconds for the order to be filled
    console.log("Waiting for 5 seconds for the order to be filled...");
    await new Promise((resolve) => setTimeout(resolve, 5000));
    const order = await sdk.getOrder(orderId);
    console.log(order);
    if (order.fulfilledPercent < 100) console.log("Order is not filled");
    else console.log("Order is filled");
};

main();
```

{% endtab %}
{% endtabs %}

## **功能**

\
我们所有的 SDK 都提供以下功能：

* **购买资源 (Buy Resource)**：购买资源
* **预估资源购买费用 (Estimate Buy Resource)**：预估资源购买成本
* **扩展请求 (Extend Request)**：扩展资源请求
* **获取可扩展委托人 (Get Extendable Delegates)**：获取可扩展委托人列表
* **获取用户信息 (Get User Info)**：获取用户信息
* **获取订单/订单列表 (Get Order, Get Orders)**：获取订单详情
* **获取订单簿 (Get Order Book)**：获取订单簿


---

# 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/sdk-libraries.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.
