创建交易
为能量或带宽购买、资源包或地址激活创建新交易。
资源包允许您在单个API请求中同时购买能量和带宽。
POST /v1/transaction/new
请求
请求参数
| 字段 | 类型 | 必需 | 描述 |
|---|---|---|---|
| external_id | string | 否 | 可选的外部交易标识符 |
| service | string | 是 | 服务类型:"energy"、"bandwidth"、"resource_bundle" 或 "activate_address" |
| params | object | 是 | 服务参数对象 |
| params.address | string | 是 | 波场钱包地址(34个字符) |
| params.amount* | integer | 条件必需 | 要购买的能量或带宽数量。当service="energy"或"bandwidth"且未提供params.amounts时必需。 |
| params.energy_amount* | integer | 否 | 已弃用。请使用 params.amount 或 params.amounts.energy。 |
| params.amounts* | object | 条件必需 | 资源数量对象。当service="resource_bundle"时必需。也可用于"energy"和"bandwidth"服务。 |
| params.amounts.energy | integer | 条件必需 | 要购买的能量数量。在amounts中用于"resource_bundle"时必需。 |
| params.amounts.bandwidth | integer | 条件必需 | 要购买的带宽数量。在amounts中用于"resource_bundle"时必需。 |
| params.duration | integer | 是 | 持续时间(小时)。能量、带宽和资源包目前仅支持1小时。 |
| params.activate_address | boolean | 否 | 是否激活地址。能量、带宽和资源包服务的可选项。 |
* 我们建议使用 amounts 对象,这是最通用的方式。API按以下顺序解析资源数量:params.amounts → params.amount → params.energy_amount。
bash
#!/bin/bash
API_TOKEN="your_api_token"
API_SECRET="your_api_secret"
REQUEST_BODY='{
"external_id": "my-external-id-123",
"service": "energy",
"params": {
"address": "TRX_ADDRESS",
"amounts": {
"energy": 65000
},
"duration": 1
}
}'
# 计算签名
SIGNATURE=$(echo -n "${REQUEST_BODY}${API_SECRET}" | sha256sum | cut -d' ' -f1)
# 发送API请求
curl -X POST "https://api.tronzap.com/v1/transaction/new" \
-H "Authorization: Bearer ${API_TOKEN}" \
-H "X-Signature: ${SIGNATURE}" \
-H "Content-Type: application/json" \
-d "${REQUEST_BODY}"javascript
const crypto = require('crypto');
const axios = require('axios');
const apiToken = 'your_api_token';
const apiSecret = 'your_api_secret';
const requestBody = JSON.stringify({
external_id: 'my-external-id-123',
service: 'energy',
params: {
address: 'TRX_ADDRESS',
amounts: {
energy: 65000
},
duration: 1
}
});
// 计算签名
const signature = crypto
.createHash('sha256')
.update(requestBody + apiSecret)
.digest('hex');
// 发送API请求
axios({
method: 'post',
url: 'https://api.tronzap.com/v1/transaction/new',
headers: {
'Authorization': `Bearer ${apiToken}`,
'X-Signature': signature,
'Content-Type': 'application/json'
},
data: requestBody
})
.then(response => console.log(response.data))
.catch(error => console.error(error));php
<?php
$apiToken = 'your_api_token';
$apiSecret = 'your_api_secret';
$requestBody = json_encode([
'external_id' => 'my-external-id-123',
'service' => 'energy',
'params' => [
'address' => 'TRX_ADDRESS',
'amounts' => [
'energy' => 65000
],
'duration' => 1
]
]);
// 计算签名
$signature = hash('sha256', $requestBody . $apiSecret);
// 发送API请求
$ch = curl_init('https://api.tronzap.com/v1/transaction/new');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiToken,
'X-Signature: ' . $signature,
'Content-Type: application/json'
]);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>python
import hashlib
import json
import requests
api_token = 'your_api_token'
api_secret = 'your_api_secret'
request_body = json.dumps({
'external_id': 'my-external-id-123',
'service': 'energy',
'params': {
'address': 'TRX_ADDRESS',
'amounts': {
'energy': 65000
},
'duration': 1
}
})
# 计算签名
signature = hashlib.sha256((request_body + api_secret).encode()).hexdigest()
# 发送API请求
headers = {
'Authorization': f'Bearer {api_token}',
'X-Signature': signature,
'Content-Type': 'application/json'
}
response = requests.post(
'https://api.tronzap.com/v1/transaction/new',
headers=headers,
data=request_body
)
print(response.json())资源包的请求示例
bash
#!/bin/bash
API_TOKEN="your_api_token"
API_SECRET="your_api_secret"
REQUEST_BODY='{
"external_id": "my-external-id-123",
"service": "resource_bundle",
"params": {
"address": "TRX_ADDRESS",
"amounts": {
"energy": 65000,
"bandwidth": 345
},
"duration": 1,
"activate_address": true
}
}'
# 计算签名
SIGNATURE=$(echo -n "${REQUEST_BODY}${API_SECRET}" | sha256sum | cut -d' ' -f1)
# 发送API请求
curl -X POST "https://api.tronzap.com/v1/transaction/new" \
-H "Authorization: Bearer ${API_TOKEN}" \
-H "X-Signature: ${SIGNATURE}" \
-H "Content-Type: application/json" \
-d "${REQUEST_BODY}"javascript
const crypto = require('crypto');
const axios = require('axios');
const apiToken = 'your_api_token';
const apiSecret = 'your_api_secret';
const requestBody = JSON.stringify({
external_id: 'my-external-id-123',
service: 'resource_bundle',
params: {
address: 'TRX_ADDRESS',
amounts: {
energy: 65000,
bandwidth: 5000
},
duration: 1,
activate_address: true
}
});
// 计算签名
const signature = crypto
.createHash('sha256')
.update(requestBody + apiSecret)
.digest('hex');
// 发送API请求
axios({
method: 'post',
url: 'https://api.tronzap.com/v1/transaction/new',
headers: {
'Authorization': `Bearer ${apiToken}`,
'X-Signature': signature,
'Content-Type': 'application/json'
},
data: requestBody
})
.then(response => console.log(response.data))
.catch(error => console.error(error));php
<?php
$apiToken = 'your_api_token';
$apiSecret = 'your_api_secret';
$requestBody = json_encode([
'external_id' => 'my-external-id-123',
'service' => 'resource_bundle',
'params' => [
'address' => 'TRX_ADDRESS',
'amounts' => [
'energy' => 65000,
'bandwidth' => 5000
],
'duration' => 1,
'activate_address' => true
]
]);
// 计算签名
$signature = hash('sha256', $requestBody . $apiSecret);
// 发送API请求
$ch = curl_init('https://api.tronzap.com/v1/transaction/new');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiToken,
'X-Signature: ' . $signature,
'Content-Type: application/json'
]);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>python
import hashlib
import json
import requests
api_token = 'your_api_token'
api_secret = 'your_api_secret'
request_body = json.dumps({
'external_id': 'my-external-id-123',
'service': 'resource_bundle',
'params': {
'address': 'TRX_ADDRESS',
'amounts': {
'energy': 65000,
'bandwidth': 5000
},
'duration': 1,
'activate_address': True
}
})
# 计算签名
signature = hashlib.sha256((request_body + api_secret).encode()).hexdigest()
# 发送API请求
headers = {
'Authorization': f'Bearer {api_token}',
'X-Signature': signature,
'Content-Type': 'application/json'
}
response = requests.post(
'https://api.tronzap.com/v1/transaction/new',
headers=headers,
data=request_body
)
print(response.json())地址激活的请求示例
bash
#!/bin/bash
API_TOKEN="your_api_token"
API_SECRET="your_api_secret"
REQUEST_BODY='{
"external_id": "my-external-id-123",
"service": "activate_address",
"params": {
"address": "TRX_ADDRESS"
}
}'
# 计算签名
SIGNATURE=$(echo -n "${REQUEST_BODY}${API_SECRET}" | sha256sum | cut -d' ' -f1)
# 发送API请求
curl -X POST "https://api.tronzap.com/v1/transaction/new" \
-H "Authorization: Bearer ${API_TOKEN}" \
-H "X-Signature: ${SIGNATURE}" \
-H "Content-Type: application/json" \
-d "${REQUEST_BODY}"javascript
const crypto = require('crypto');
const axios = require('axios');
const apiToken = 'your_api_token';
const apiSecret = 'your_api_secret';
const requestBody = JSON.stringify({
external_id: 'my-external-id-123',
service: 'activate_address',
params: {
address: 'TRX_ADDRESS'
}
});
// 计算签名
const signature = crypto
.createHash('sha256')
.update(requestBody + apiSecret)
.digest('hex');
// 发送API请求
axios({
method: 'post',
url: 'https://api.tronzap.com/v1/transaction/new',
headers: {
'Authorization': `Bearer ${apiToken}`,
'X-Signature': signature,
'Content-Type': 'application/json'
},
data: requestBody
})
.then(response => console.log(response.data))
.catch(error => console.error(error));响应
响应提供已创建交易的详细信息。
响应字段
| 字段 | 类型 | 描述 |
|---|---|---|
| code | integer | 响应代码(0 = 成功) |
| request_id | string | 唯一请求标识符 |
| result | object | 响应数据 |
| result.id | string | 内部交易ID |
| result.external_id | string|null | 外部交易ID(如果提供) |
| result.service | string | 服务类型:"energy"、"bandwidth"、"resource_bundle" 或 "activate_address" |
| result.params | object | 原始请求参数 |
| result.params.activate_address | boolean | 是否请求了地址激活 |
| result.status | string | 交易状态(查看状态列表) |
| result.amount | float | 交易金额 |
| result.created_at | string | 创建时间戳(ISO 8601格式) |
| result.hash | string | 交易哈希(如果已完成) |
响应示例
json
{
"code": 0,
"request_id": "bbf74bcd-fb36-4df8-adc8-25f2bacd087b",
"result": {
"id": "transaction_id",
"external_id": "my-external-id-123",
"service": "energy",
"params": {
"address": "TRX_ADDRESS",
"amounts": {
"energy": 65000
},
"duration": 1,
"activate_address": false
},
"status": "success",
"amount": 8.25,
"created_at": "2024-03-22T12:00:00Z",
"hash": "transaction_hash"
}
}可能的错误
| 错误代码 | Key | 描述 |
|---|---|---|
| 1 | auth | 身份验证错误(令牌或签名不正确) |
| 2 | invalid_service_or_params | 无效的服务或参数 |
| 5 | wallet_not_found | 未找到钱包 |
| 6 | insufficient_funds | 余额不足 |
| 10 | invalid_tron_address | 无效的波场地址 |
| 11 | invalid_energy_amount | 无效的能量数量 |
| 12 | invalid_duration | 无效的持续时间 |
| 50 | invalid_bandwidth_amount | 无效的带宽数量 |
| 24 | address_not_activated | 地址未激活 |
| 25 | address_already_activated | 地址已激活 |
| 35 | service_unavailable | 服务不可用 |
| 500 | internal_server_error | 内部服务器错误 |
