Sign and Broadcast Transaction
To complete any operation on Tron, sign the unsigned transaction provided by the Staking API and broadcast it to the network.
1. Prepare the unsigned transaction
Obtain the unsignedTransaction object provided by a transaction builder or the relevant API endpoint, e.g., /staking/delegate, /staking/vote, /staking/undelegate.
2. Sign the transaction
Use the tronweb library to sign the transaction with your private key.
// sign-tron-tx.mjs
import { TronWeb } from 'tronweb';
import dotenv from 'dotenv';
dotenv.config();
const tronFullHostUrl = 'https://nile.trongrid.io';
const tronPrivateKey = process.env.TRON_PRIVATE_KEY;
const unsignedTx = {
"visible": false,
"txID": "c07e64f57ebb6ae191404b1c693aa46f80789aceacbfd8cc17a7c85c0c8c5559",
"raw_data_hex": "0a02ee182208eb04b8cce7cc51534090f5d7edfa325a6a080412660a30747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e566f74655769746e657373436f6e747261637412320a1541da53d324a354aa5924c7117caff3ead97910a5e112190a1541351b95e29c2514b7b8183cbc81a8e08c68e8833b100370b0a0d4edfa32",
"raw_data": {
// ... full raw_data from API response ...
}
};
async function main() {
if (!tronPrivateKey) throw new Error('TRON_PRIVATE_KEY is not set');
const tronWeb = new TronWeb({
fullHost: tronFullHostUrl,
headers: {
'TRON-PRO-API-KEY': process.env.TRONGRID_API_KEY,
},
privateKey: tronPrivateKey,
});
const signedTx = await tronWeb.trx.sign(unsignedTx);
// Hex-serialize for sending to API (as required)
const hexSignedTx = Buffer.from(JSON.stringify(signedTx)).toString('hex');
console.log('✅ Signed transaction hex:', `0x${hexSignedTx}`);
}
main().catch((e) => {
console.error('Signing failed:', e.message);
process.exit(1);
});Upon successful execution, the script prints the signed 0x-prefixed transaction in hexadecimal format, ready for broadcasting.
3. Broadcast the signed transaction
Send a POST request to /api/v1/tron/{network}/transaction/send.
Example request:
curl --request POST \
--url 'https://api-test.p2p.org/api/v1/tron/testnet-nile/transaction/send' \
--header 'accept: application/json' \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"signedTransaction": "0x7b2276697369626c65223a66616c73652c2274784944223a22..."
}'signedTransaction— signed transaction in the hexadecimal which needs to be broadcasted to the network.
Example response:
{
"error": null,
"result": {
"transactionHash": "c8e47301ca1d4a56120e6f13f0ce5f8a680e2fda221b3d8bc423cc355f924a70",
"createdAt": "2025-07-04T09:01:10.000Z",
"network": "testnet-nile",
"senderAddress": "41da53d324a354aa5924c7117caff3ead97910a5e1",
"block": null,
"status": "PENDING",
"gas": {
"bandwidthUsed": 0,
"energyUsed": 0
}
}
}transactionHash— hash of the transaction.createdAt— timestamp of the transaction in the ISO 8601 format.network— identifier of the TRON network (e.g.,mainnet,testnet-nile).senderAddress— address of the transaction sender in the base58 format.block— unique identifier of the block in which the transaction has been included.null, if still pending.status— transaction status:PENDING,FAILEDorCONFIRMED.gas— resources consumed by the transaction depending on their type:bandwidthUsed— amount of bandwidth points consumed by the transaction.energyUsed— amount of energy points consumed by the transaction.
To check the transaction status, use the transaction hash in explorers like Tronscan.
What's next?
- Staking API reference
- Unified API
Updated 15 days ago