v0 — Buy with API Key

Full v0 example — buy Energy with an API Key, from order book check to fulfillment polling, in JavaScript, Java, Ruby, and PHP.

This is a complete, working example that buys Energy through an API Key. It checks the order book, verifies your Internal Account balance, creates the order, then polls the order until it is fulfilled.

Get an API Key

You need an API Key tied to a TronSave Internal Account before running this example. See Authentication for the two ways to generate one (on the website or via Telegram).

Requirements

The JavaScript variant uses TronWeb. Install the exact versions:

TronWeb 5.3.2 is required. Read more in the TronWeb 5.3.2 release notes.

Configuration

Replace the placeholder constants at the top of each file:

Constant
Description

API_KEY

Your TronSave API Key.

TRONSAVE_API_URL

https://api.tronsave.io for mainnet, or https://api-dev.tronsave.io for testnet.

RECEIVER_ADDRESS

The address that will receive the Energy.

BUY_AMOUNT

Amount of Energy to buy.

DURATION

Order duration in milliseconds. Default in the example: 3600 * 1000 (1 hour).

MAX_PRICE_ACCEPTED

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

Full example

How it works

  1. Check the order bookGET /v0/order-book returns available Energy grouped by price. A price of -1 represents Energy available below the lowest tiered price. Use this to choose a MAX_PRICE_ACCEPTED that can fulfill your BUY_AMOUNT.

  2. Check your balanceGET /v0/user-info returns your Internal Account balance (in SUN). The example only proceeds if balance >= MAX_PRICE_ACCEPTED * BUY_AMOUNT.

  3. Create the orderPOST /v0/internal-buy-energy places the order and returns an order_id.

  4. Poll for fulfillmentGET /v0/orders/{order_id} is polled every 3 seconds until fulfilled_percent reaches 100 (or remain_amount is 0).

Next steps

Last updated