// 01 · 概述
API Overview
TRONQUIRY 提供 REST API 和 WebSocket 两种接入方式,帮助你以程序化的方式查询 Tron 链上的 TRC20 授权记录和 USDT 余额数据。
Base URL:
https://troncha.yytg.me/api/v1
REST API 需要 高级会员 或以上级别。WebSocket 需要 高级会员 或以上。访问 会员套餐 了解详情。
// 02 · 认证
Authentication
所有 API 请求需要通过 API Key 认证。在 API Keys 页面 生成你的专属密钥,通过以下方式传入:
方式一:请求头(推荐)
X-API-Key: ts_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
方式二:POST Body 参数
api_key=ts_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
方式三:Query 参数
https://troncha.yytg.me/api/v1/query?api_key=ts_xxx...
⚠ 请妥善保管 API Key,不要在客户端代码中明文暴露。如密钥泄露,请立即在控制台删除并重新生成。
// 03 · 错误码
Error Codes
| HTTP 状态码 | 错误码 | 说明 |
|---|---|---|
| 401 | UNAUTHORIZED | API Key 缺失或无效 |
| 403 | FORBIDDEN | 会员等级不足,无权访问此接口 |
| 400 | INVALID_ADDRESS | Tron 地址格式不正确 |
| 429 | RATE_LIMITED | 请求频率超出限制 |
| 500 | NODE_ERROR | 链上节点查询失败,可重试 |
错误响应格式
{
"success": false,
"error": "错误描述信息",
"code": "INVALID_ADDRESS"
}
// 04 · REST API
查询授权记录 高级+
POST
/api/v1/query_transactions
查询 TRC20 Approval 授权记录
查询指定 Tron 地址下的所有 TRC20 Approval 授权交易,返回按最新时间去重的授权列表。
请求参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| tronAddress | string | 必填 | 34 位 Tron 地址,以 T 开头 |
| api_key | string | 必填* | API Key(或通过请求头传入) |
cURL 示例
curl -X POST https://troncha.yytg.me/api/v1/query_transactions \ -H "X-API-Key: ts_xxxxxxxxxxxxxxxx" \ -d "tronAddress=TL2BYMyHDVfVGEEiMcu6PSz8Fiv61FrAN6"
✓ 成功响应 200
{
"success": true,
"tron_address": "TL2BY...",
"count": 12,
"transactions": [
{
"block_timestamp": 1720000000000,
"from": "TXxxxxxx...",
"to": "TYxxxxxx...",
"value": "115792089237..."
}
]
}
单地址余额查询 高级+
GET
/api/v1/balance/{address}
查询单个地址 USDT 余额
获取指定 Tron 地址的当前 USDT (TRC20) 余额,单位为 USDT(已处理精度)。
cURL 示例
curl https://troncha.yytg.me/api/v1/balance/TXxxx... \ -H "X-API-Key: ts_xxxxxxxxxxxxxxxx"
✓ 成功响应 200
{
"success": true,
"address": "TXxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"balance": 1234.56,
"unit": "USDT"
}
批量余额查询 高级+
POST
/api/v1/balances/batch
批量查询多地址 USDT 余额
一次请求批量获取多个地址的余额。
cURL 示例
curl -X POST https://troncha.yytg.me/api/v1/balances/batch \ -H "X-API-Key: ts_xxxxxxxxxxxxxxxx" \ -H "Content-Type: application/json" \ -d '{"addresses":["TX...","TY...","TZ..."]}'
✓ 成功响应 200
{
"success": true,
"count": 3,
"balances": {
"TXxxx...": 1234.56,
"TYxxx...": 0.00,
"TZxxx...": 88.12
}
}
// 05 · WebSocket
WS Overview 高级+
通过 WebSocket 建立持久连接,实时接收订阅地址的 USDT 余额变动推送。后端每秒批量查询所有订阅地址,余额发生变动时立即推送通知。每个连接最多订阅 200 个地址。
首次订阅后不推送当前余额,只在余额发生变动时通知,避免冗余数据。
连接与认证 高级+
// WebSocket 端点 wss://troncha.yytg.me/ws/balance // 方式一:网页端已登录,连接后直接可用 // 方式二:API 接入,连接后发送认证消息 { "action": "auth", "api_key": "ts_xxxxxxxxxxxxxxxx" } // 认证成功响应 { "type": "auth_ok", "user": "your_username" }
订阅地址 高级+
// 订阅地址(最多200个) { "action": "subscribe", "addresses": ["TXxxx...", "TYxxx..."] } // 取消订阅 { "action": "unsubscribe", "addresses": ["TXxxx..."] } // 查看当前订阅列表 { "action": "list" }
事件格式 高级+
// 余额变动推送(仅在余额变化时发送) { "type": "balance_change", "timestamp": 1720000000000, "changes": { "TXxxx...": { "old": 100.00, "new": 1234.56 } } } // 订阅确认 { "type": "subscribed", "addresses": ["TXxxx..."], "total": 1 } // 错误消息 { "type": "error", "error": "超出最大订阅数量 200" }
// 06 · 示例代码
Python 示例
import requests API_KEY = "ts_xxxxxxxxxxxxxxxx" BASE_URL = "https://troncha.yytg.me/api/v1" HEADERS = {"X-API-Key": API_KEY} # 查询授权记录 def query_approvals(address): resp = requests.post( f"{BASE_URL}/query_transactions", headers=HEADERS, data={"tronAddress": address} ) return resp.json() # 批量查询余额 def batch_balances(addresses): resp = requests.post( f"{BASE_URL}/balances/batch", headers={"X-API-Key": API_KEY, "Content-Type": "application/json"}, json={"addresses": addresses} ) return resp.json() # WebSocket 余额监控 import websocket, json def on_message(ws, message): data = json.loads(message) if data['type'] == 'balance_change': for addr, change in data['changes'].items(): print(f"{addr}: {change['old']} → {change['new']} USDT") def on_open(ws): ws.send(json.dumps({"action": "auth", "api_key": API_KEY})) ws.send(json.dumps({ "action": "subscribe", "addresses": ["TX...", "TY..."] })) ws = websocket.WebSocketApp( "wss://troncha.yytg.me/ws/balance", on_open=on_open, on_message=on_message ) ws.run_forever()
JavaScript 示例
const API_KEY = 'ts_xxxxxxxxxxxxxxxx'; const BASE_URL = 'https://troncha.yytg.me/api/v1'; // REST API async function batchBalances(addresses) { const res = await fetch(`${BASE_URL}/balances/batch`, { method: 'POST', headers: { 'X-API-Key': API_KEY, 'Content-Type': 'application/json' }, body: JSON.stringify({ addresses }) }); return res.json(); } // WebSocket 余额监控 const ws = new WebSocket('wss://troncha.yytg.me/ws/balance'); ws.onopen = () => { ws.send(JSON.stringify({ action: 'auth', api_key: API_KEY })); ws.send(JSON.stringify({ action: 'subscribe', addresses: ['TX...', 'TY...'] })); }; ws.onmessage = (e) => { const data = JSON.parse(e.data); if (data.type === 'balance_change') { Object.entries(data.changes).forEach(([addr, change]) => { console.log(`${addr}: ${change.old} → ${change.new} USDT`); }); } };