开发者文档
Tron 能量租赁 API 参考:REST 端点与集成
TronEnergy 能量委托的完整 REST API 文档。POST /delegate 端点、HMAC 身份验证、请求/响应格式、错误代码和速率限制。
基础 URL:
https://api.tronnrg.com
身份验证: 无需验证。所有端点均为公开。
速率限制: 每 IP 每秒 20 个请求
支付地址: TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx (仅限 API,不适用于手动租赁) 工作原理
三个步骤。无需 API 密钥、无需注册、无需连接钱包。所有权通过密码学证明。
- 发送 TRX — 向支付地址发送 4 TRX(或更多)。4 TRX = 65,000 能量。8 TRX = 130,000。线性计价。
- 签名 — 使用发送 TRX 的钱包签署消息
{tx_hash}:{delegate_to}。这可证明您授权了能量委托。 - 领取 — 使用
POST /delegate和tx_hash、delegate_to及signature领取能量。能量将在约 3 秒内到达。
支付地址
TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx
仅限 API 支付地址。 此地址用于通过 API 进行程序化集成。请勿用于手动能量租赁。手动租赁地址不同,可在 上找到。tronnrg.com.
向此地址发送 TRX。您的支付交易哈希是触发能量委托的凭证。每个哈希只能使用一次。
| 发送的 TRX | 委托的能量 | 使用场景 |
|---|---|---|
| 4 TRX | 65,000 | 向现有钱包进行标准 USDT 转账(最小订单) |
| 8 TRX | 130,000 | 向首次收款人转账 USDT |
| 16 TRX | 260,000 | 一个订单内进行四笔标准转账 |
| 40 TRX | 650,000 | 十笔标准转账 |
| 100 TRX | 1,625,000 | 约 25 笔标准转账 — 小型平台常见 |
| 1,000 TRX | 16,250,000 | 最大订单,约 250 笔标准转账 |
| 任何介于两者之间的金额 | trx × 16,250 | 完全线性计价。无分级、无套餐、无折扣。 |
公式:
energy = trxSent × 16,250. 限额: 最低 4 TRX(65,000 能量),最高 1,000 TRX(16,250,000 能量)。两者都在 API 级别强制执行 — 低于最低金额的订单会被拒绝并显示 below_minimum 并退款;超过最高金额的订单在委托前被拒绝。
POST /delegate
POST/delegate
申请能量委托。您必须已经向链上支付地址发送了TRX。请提供交易哈希、收款地址和证明您是发送者的签名。
| 参数 | 类型 | 描述 | |
|---|---|---|---|
| tx_hash | string | 必需 | TRX支付的64字符十六进制哈希 |
| delegate_to | string | 必需 | 接收能量的Tron地址 |
| signature | string | 必需 | tronWeb.trx.signMessageV2()签名:tronWeb.trx.signMessageV2()。证明您是支付发送者。 |
curl -X POST https://api.tronnrg.com/delegate \
-H "Content-Type: application/json" \
-d '{"tx_hash":"TX_HASH","delegate_to":"TWallet","signature":"SIG"}'
const result = await fetch('https://api.tronnrg.com/delegate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
tx_hash: 'YOUR_TX_HASH',
delegate_to: 'TWalletAddress',
signature: 'YOUR_SIGNATURE',
}),
}).then(r => r.json());
if (result.error) {
console.error(result.error, result.message);
} else {
console.log('Delegated:', result.energy, 'energy');
console.log('Ref:', result.ref);
}
import requests
response = requests.post('https://api.tronnrg.com/delegate', json={
'tx_hash': 'YOUR_TX_HASH',
'delegate_to': 'TWalletAddress',
'signature': 'YOUR_SIGNATURE',
})
result = response.json()
if 'error' in result:
print(f"Error: {result['error']} - {result['message']}")
else:
print(f"Delegated: {result['energy']} energy")
print(f"Delegation tx: {result['delegations'][0]['tx']}")
print(f"Ref: {result['ref']}")
$ch = curl_init('https://api.tronnrg.com/delegate');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode([
'tx_hash' => 'YOUR_TX_HASH',
'delegate_to' => 'TWalletAddress',
'signature' => 'YOUR_SIGNATURE',
]),
]);
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
if (isset($result['error'])) {
echo "Error: " . $result['message'];
} else {
echo "Delegated: " . $result['energy'] . " energy";
}
var client = new HttpClient();
var content = new StringContent(
JsonSerializer.Serialize(new {
tx_hash = "YOUR_TX_HASH",
delegate_to = "TWalletAddress",
signature = "YOUR_SIGNATURE"
}),
Encoding.UTF8, "application/json"
);
var response = await client.PostAsync("https://api.tronnrg.com/delegate", content);
var json = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<JsonElement>(json);
if (result.TryGetProperty("error", out var err))
Console.WriteLine($"Error: {err}");
else
Console.WriteLine($"Delegated: {result.GetProperty("energy")} energy");
响应 200
{
"ref": "nrg_d_42",
"delegate_to": "TWalletAddress",
"energy": 65000,
"cost": 4,
"status": "delegated",
"delegations": [
{ "tx": "a1b2c3d4e5f6...your delegation tx hash", "energy": 65000 }
]
}
| 字段 | 类型 | 描述 |
|---|---|---|
| ref | string | TronNRG参考ID。请记录此ID以便支持查询。 |
| energy | number | 委托的总能量 |
| cost | number | 扣除的TRX |
| status | string | 成功时为"delegated" |
| delegations | array | 链上委托交易哈希。每笔tx都可在TronScan上验证。这是您的收据。 |
GET /health
GET/health
用于监控和正常运行时间工具的活性检查。当API进程正常运行时返回200 OK。不检查上游节点或提供商。
响应 200
{ "status": "ok" }
错误代码
每个错误响应都有error(稳定、机器可读)和message(人类可读)。始终在代码中切换error。
| 代码 | HTTP | 含义 |
|---|---|---|
invalid_tx_hash | 400 | 不是64字符的十六进制字符串 |
invalid_address | 400 | 不是有效的Tron地址 |
missing_signature | 400 | 未提供签名 |
invalid_signature | 401 | 签名无法验证 |
signature_mismatch | 403 | 签名人地址与支付发送者不匹配 |
hash_already_used | 409 | 交易哈希已被认领 |
payment_verification_failed | 404 / 400 | 链上支付验证失败。请阅读message字段了解具体原因:交易尚未找到(404,请稍后重试)、收款地址错误、不是TRX转账或低于4 TRX最低额。 |
delegation_failed | 400 / 500 | 提供商无法交付能量。如果失败发生在支付验证后,将自动排队退款。此时请检查refund对象。 |
rate_limited | 429 | 该IP的请求过于频繁。限制为每秒 20 次。 |
server_error | 500 | 发生意外的内部错误。请在几秒钟后重试。 |
const result = await fetch('https://api.tronnrg.com/delegate', { ... })
.then(r => r.json());
if (result.error) {
switch (result.error) {
case 'payment_verification_failed':
// Most common cause: tx not yet indexed. Wait 3s and retry.
// Read result.message for the specific cause.
break;
case 'hash_already_used':
// Already claimed. Don't retry.
break;
case 'signature_mismatch':
// Signer != payment sender. Sign with the same key that sent TRX.
break;
case 'delegation_failed':
// Refund queued automatically if payment was verified.
if (result.refund) console.log('Refund queued:', result.refund);
break;
}
}
result = requests.post('https://api.tronnrg.com/delegate', json=data).json()
if 'error' in result:
if result['error'] == 'payment_verification_failed':
# Most common cause: tx not yet indexed. Wait 3s and retry.
pass
elif result['error'] == 'hash_already_used':
# Already claimed. Don't retry.
pass
elif result['error'] == 'signature_mismatch':
# Signer != payment sender. Sign with the same key.
pass
elif result['error'] == 'delegation_failed':
# Refund queued automatically if payment was verified.
pass
if (isset($result['error'])) {
switch ($result['error']) {
case 'payment_verification_failed':
// Most common cause: tx not yet indexed. Wait 3s and retry.
break;
case 'hash_already_used':
// Already claimed. Don't retry.
break;
case 'signature_mismatch':
// Signer != payment sender. Sign with the same key.
break;
case 'delegation_failed':
// Refund queued automatically if payment was verified.
break;
}
}
退款
如果您的付款验证后委托失败,TRX 退款将自动排队并发送回发送方地址链上。查看错误响应中的退款对象。
退款错误
{
"error": "delegation_failed",
"message": "Energy delegation failed. Your payment will be refunded.",
"ref": "nrg_d_43",
"refund": {
"type": "queued",
"to": "TSenderAddress",
"amount": 4
}
}
完整示例
完整的端到端流程:发送 TRX、签名、领取并重试。复制并运行。
const API = 'https://api.tronnrg.com';
const ADDR = 'TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx';
async function rentEnergy(delegateTo, trxAmount = 4) {
// 1. Send TRX to the payment address
const payment = await tronWeb.trx.sendTransaction(ADDR, trxAmount * 1e6);
// 2. Sign: proves you are the sender
const message = `${payment.txid}:${delegateTo}`;
const signature = await tronWeb.trx.signMessageV2(message);
// 3. Claim delegation (retry if tx not indexed yet)
let result;
for (let i = 0; i < 3; i++) {
result = await fetch(`${API}/delegate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
tx_hash: payment.txid,
delegate_to: delegateTo,
signature,
}),
}).then(r => r.json());
if (!result.error) break;
if (result.error !== 'payment_verification_failed') throw new Error(result.message);
await new Promise(r => setTimeout(r, 3000));
}
if (result.error) throw new Error(result.message);
return result;
}
// Usage — send any amount between 4 and 1,000 TRX
const result = await rentEnergy('TWalletThatNeedsEnergy', 4); // 4 TRX → 65k energy
// rentEnergy(addr, 8) // → 130,000 energy (new-wallet transfer)
// rentEnergy(addr, 40) // → 650,000 energy (10 transfers)
// rentEnergy(addr, 1000) // → 16,250,000 energy (max)
console.log(result.energy); // trxAmount × 16,250
console.log(result.delegations[0].tx); // on-chain tx hash
console.log(result.ref); // "nrg_d_42"
import requests
import time
API = 'https://api.tronnrg.com'
ADDR = 'TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx'
def rent_energy(delegate_to, trx_amount=4):
# 1. Send TRX to ADDR (via your Tron library)
tx_hash = send_trx(ADDR, trx_amount) # your TRX send function
# 2. Sign: proves you are the sender
message = f'{tx_hash}:{delegate_to}'
signature = tron.trx.sign_message_v2(message)
# 3. Claim delegation (retry if tx not indexed yet)
for attempt in range(3):
result = requests.post(f'{API}/delegate', json={
'tx_hash': tx_hash,
'delegate_to': delegate_to,
'signature': signature,
}).json()
if 'error' not in result:
return result
if result['error'] != 'payment_verification_failed':
raise Exception(result['message'])
time.sleep(3)
raise Exception('Transaction not found after retries')
# Usage
result = rent_energy('TWalletThatNeedsEnergy', 4)
print(f"Delegated: {result['energy']} energy")
print(f"Delegation tx: {result['delegations'][0]['tx']}")
print(f"Ref: {result['ref']}")
<?php
$api = 'https://api.tronnrg.com';
$addr = 'TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx';
function rentEnergy($api, $txHash, $delegateTo, $signature) {
// 1. Send TRX to $addr (via iexbase/tron-api)
// $payment = $tron->sendTrx($addr, 4);
// $txHash = $payment['txid'];
// 3. Claim delegation (retry if tx not indexed yet)
for ($i = 0; $i < 3; $i++) {
$ch = curl_init("${api}/delegate");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode([
'tx_hash' => $txHash,
'delegate_to' => $delegateTo,
'signature' => $signature,
]),
]);
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
if (!isset($result['error'])) return $result;
if ($result['error'] !== 'payment_verification_failed')
throw new Exception($result['message']);
sleep(3);
}
throw new Exception('Transaction not found after retries');
}
// Usage
$result = rentEnergy($api, $txHash, 'TWalletThatNeedsEnergy');
echo "Delegated: " . $result['energy'] . " energy\n";
echo "Delegation tx: " . $result['delegations'][0]['tx'] . "\n";
echo "Ref: " . $result['ref'] . "\n";
# 1. Send TRX to TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx
# Pricing is linear at 16,250 energy per TRX.
# Min 4 TRX (65,000 energy), max 1,000 TRX (16.25M energy).
# (use your wallet or tronweb CLI)
# 2. Sign the message {tx_hash}:{delegate_to} (proves you are the sender)
# (use tronWeb.trx.signMessageV2 in your code)
# 3. Claim delegation with tx hash + signature
curl -X POST https://api.tronnrg.com/delegate \
-H "Content-Type: application/json" \
-d '{
"tx_hash": "YOUR_PAYMENT_TX_HASH",
"delegate_to": "TWalletThatNeedsEnergy",
"signature": "YOUR_SIGNATURE"
}'
# Response includes delegations[].tx — the on-chain hash you can verify on TronScan