v2 — Buy with Private Key
Full working example of buying Energy or Bandwidth via the TronSave v2 API by signing the payment transaction with a wallet private key.
This is a complete, runnable example of the signed-transaction buy flow: estimate the cost, build and sign a TRX payment transaction with your wallet's private key, then submit it to create the order. No prepaid TronSave balance is required — you pay per order directly from your wallet.
The flow has three steps:
Estimate — call
/v2/estimate-buy-resourceto get the unit price and the TRX required.Sign — build a
sendTrxtransaction to the TronSave receiver address and sign it with your private key.Create order — submit the signed transaction to
/v2/buy-resource.
Never commit a real private key. Load PRIVATE_KEY from an environment variable or secret store before running this in production.
Requirements
The JavaScript example targets TronWeb version 5.3.2:
npm i [email protected] @noble/[email protected]Read more in the TronWeb 5.3.2 release notes.
Configuration
The example uses Mainnet values by default. To run against the Nile testnet, swap the values noted inline:
TRONSAVE_RECEIVER_ADDRESS
TWZEhq5JuUVvGtutNgnRBATbF8BnHGyn4S
TATT1UzHRikft98bRFqApFTsaSw73ycfoS
TRON_FULL_NODE
https://api.trongrid.io
https://api.nileex.io
TRONSAVE_API_URL
https://api.tronsave.io
https://api-dev.tronsave.io
See Environments for more on switching networks.
Full example
The get_signed_transaction helper above is a placeholder. To actually sign the TRX payment in Python, build and sign a TransferContract with a TRON library such as tronpy, or call the TronSave Get Signed Transaction API.
The getSignedTransaction helper above is a placeholder. To actually sign the TRX payment, use a TRON PHP SDK to build and sign a TransferContract, or call the TronSave Get Signed Transaction API.
Only the JavaScript example signs the transaction end-to-end (via TronWeb). For the other languages, the signing step is left as a stub — build and sign a TransferContract (sendTrx) to the TronSave receiver address with a TRON SDK for that language, or call the TronSave Get Signed Transaction API and pass its result as signedTx.
How it works
1
POST /v2/estimate-buy-resource
Returns unitPrice, estimateTrx, durationSec, and availableResource.
2
(wallet / SDK)
Sign a sendTrx of estimateTrx SUN to TRONSAVE_RECEIVER_ADDRESS.
3
POST /v2/buy-resource
Submits signedTx and creates the order; returns an orderId.
A few notes from the example:
unitPrice: "MEDIUM"in the estimate request selects a tier; the estimate response returns the concrete numericunitPriceyou pass to/v2/buy-resource.availableResource >= BUY_AMOUNTmeans the order can be fully filled. WithallowPartialFill: true, an order can still be created when less is available.estimateTrxis denominated in SUN (1 TRX = 1,000,000 SUN) and is the amount you sign in the payment transaction.
Next steps
Last updated