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:

  1. Estimate — call /v2/estimate-buy-resource to get the unit price and the TRX required.

  2. Sign — build a sendTrx transaction to the TronSave receiver address and sign it with your private key.

  3. Create order — submit the signed transaction to /v2/buy-resource.

Requirements

The JavaScript example targets TronWeb version 5.3.2:

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:

Constant
Mainnet
Nile testnet

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

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

Step
Endpoint
Purpose

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 numeric unitPrice you pass to /v2/buy-resource.

  • availableResource >= BUY_AMOUNT means the order can be fully filled. With allowPartialFill: true, an order can still be created when less is available.

  • estimateTrx is denominated in SUN (1 TRX = 1,000,000 SUN) and is the amount you sign in the payment transaction.

Next steps

Last updated