SDK 工具集
官方提供两个 SDK 包,完全按照 API 规范自动处理请求签名并支持 Webhook 签名验证。其它编程语言可直接基于 openapi.yaml 契约自动生成客户端代码。
| 语言 | TypeScript | Python |
|---|---|---|
| 包名 | @tenergy/sdk (npm) | tenergy (PyPI) |
| 安装命令 | npm install @tenergy/sdk | pip install tenergy;异步:pip install "tenergy[async]" |
| 依赖 | 无(WebCrypto:Node 22+、Bun、Deno、Workers、浏览器) | 无;异步客户端需要 httpx |
| 调用方式 | async/await | Client(同步)或 AsyncClient(await,asyncio) |
| Webhook 验签方法 | verifyWebhookSignature(secret, timestamp, rawBody, signature) | verify_signature(secret, headers, body, tolerance_seconds=300) |
| 示例项目 | tenergy-examples-ts | tenergy-examples-py |
| 详细参考 | TypeScript SDK 文档 | 本页说明 |
TypeScript
import { TenergyClient } from "@tenergy/sdk";
const client = new TenergyClient({ baseUrl: "https://api-nile.tenergy.me/v1", apiKey: process.env.TENERGY_KEY!, apiSecret: process.env.TENERGY_SECRET! });
const quote = await client.createQuote({ resource: "energy", amount: 65_000, tier: "1h", receiver });
const order = await client.createOrder({ quote_id: quote.id, client_order_id: `qs-${crypto.randomUUID()}` });
const settled = await client.waitForOrder(order.id, { timeoutMs: 120_000 });
Python
import os, uuid
from tenergy import Client
client = Client(api_key=os.environ["TENERGY_KEY"], api_secret=os.environ["TENERGY_SECRET"], base_url="https://api-nile.tenergy.me/v1")
quote = client.create_quote({"resource": "energy", "amount": 65000, "tier": "1h", "receiver": receiver})
order = client.create_order({"quote_id": quote["id"], "client_order_id": f"qs-{uuid.uuid4()}"})
client.wait_for_order(order["id"]) 会在状态变为 active 时返回;或者监听 order.confirmed Webhook 事件。
可直接运行的快速入门项目
两个 quickstart 覆盖完整流程:测试钱包 → 注册 → 充值 → 转账能量估算 → 租用能量 → 不消耗 TRX 的真实代币转账,并在每一步打印日志;遇到任何 API 错误都会格式化输出对应的 slug 与 request id。
| 示例项目 | 运行命令 | 环境要求 |
|---|---|---|
| tenergy-examples-ts | npm install && npm run quickstart | Node.js 22.9+;无需其他配置——示例自动生成测试钱包并在 Nile 注册 |
| tenergy-examples-py | pip install -e . && python quickstart.py | Python 3.9+;无需其他配置——示例自动生成测试钱包并在 Nile 注册 |
注册成功后即可立即获取密钥;仅在实际下单时才需要账户余额。