v2 — Buy with API Key

A complete, runnable example that buys Energy or Bandwidth with an API key and polls the order until it is fulfilled, in JavaScript, Python, and PHP.

This is a full working example that buys resources (Energy or Bandwidth) using an API key. It checks the order book and your internal account balance, places a buy order, then polls the order until it is fulfilled.

The flow calls these v2 endpoints:

  • GET /v2/order-book — see available liquidity and prices.

  • GET /v2/user-info — check your internal account balance.

  • POST /v2/buy-resource — place the order and receive an orderId.

  • GET /v2/order/{orderId} — poll the order until fulfilledPercent reaches 100.

Before you start

You need an API key. Generate one in two ways:

Configuration values

Replace the placeholder values at the top of each example before running:

Constant
Meaning

API_KEY

Your TronSave API key.

TRONSAVE_API_URL

API base URL. Use https://api.tronsave.io for mainnet or https://api-dev.tronsave.io for testnet. See Environments.

RECEIVER_ADDRESS

The address that receives the delegated resources.

BUY_AMOUNT

Amount of resource to buy (e.g. 32000).

DURATION

Order duration in seconds (3600 = 1 hour).

MAX_PRICE_ACCEPTED

Maximum price (in SUN per unit) you are willing to pay.

RESOURCE_TYPE

ENERGY or BANDWIDTH.

The JavaScript example requires TronWeb 5.3.2:

Read more in the TronWeb 5.3.2 release notes.

Full example

unitPrice accepts either an explicit price in SUN or one of the presets SLOW, MEDIUM, or FAST. The order is created against the order book, so set MAX_PRICE_ACCEPTED high enough to cover the liquidity you need — the returned order-book data shows the prices at which resources are available.

Next steps

Last updated