If your Python backend sends USDT (TRC-20) — a bot, an exchange, a payments service — you’ll want to buy TRON energy in Python automatically instead of by hand. This is a complete, runnable example using the TronSave API: it checks your balance, places an energy order, and polls until the energy is delivered.
Before you start
- Get an API key from the TronSave dashboard (it spends from your internal TronSave balance — never expose it client-side).
- Install the HTTP client:
pip install requests. Prefer a typed wrapper? Use the official SDK instead:pip install tronsave— see the SDK overview.
The endpoints
The script uses four v2 endpoints, authenticated with an apikey header:
GET /v2/user-info— your internal account balance.GET /v2/order-book— available liquidity and prices.POST /v2/buy-resource— place the order, returns anorderId.GET /v2/order/{orderId}— poll untilfulfilledPercenthits 100.
Full Python example
import time, requests
API_KEY = "your_api_key"
TRONSAVE_API_URL = "https://api.tronsave.io" # testnet: https://api-dev.tronsave.io
RECEIVER_ADDRESS = "your_receiver_address"
BUY_AMOUNT = 65000 # ~1 USDT (TRC-20) transfer
DURATION = 3600 # rental duration, in seconds (1 hour)
MAX_PRICE_ACCEPTED = 100 # max price in SUN per unit
RESOURCE_TYPE = "ENERGY" # ENERGY or BANDWIDTH
HEADERS = {"apikey": API_KEY}
def get_account_info():
return requests.get(f"{TRONSAVE_API_URL}/v2/user-info", headers=HEADERS).json()
def buy_resource():
body = {
"resourceType": RESOURCE_TYPE,
"unitPrice": "MEDIUM", # a price in SUN, or SLOW / MEDIUM / FAST
"resourceAmount": BUY_AMOUNT,
"receiver": RECEIVER_ADDRESS,
"durationSec": DURATION,
"options": {"allowPartialFill": True, "maxPriceAccepted": MAX_PRICE_ACCEPTED},
}
return requests.post(f"{TRONSAVE_API_URL}/v2/buy-resource",
headers={**HEADERS, "Content-Type": "application/json"},
json=body).json()
def get_order(order_id):
return requests.get(f"{TRONSAVE_API_URL}/v2/order/{order_id}", headers=HEADERS).json()
def main():
balance = int(get_account_info()["data"]["balance"])
if balance < MAX_PRICE_ACCEPTED * BUY_AMOUNT:
raise SystemExit("Insufficient TronSave balance")
order = buy_resource()
if order["error"]:
raise SystemExit(f"Buy failed: {order['message']}")
order_id = order["data"]["orderId"]
while True: # poll until the energy is delivered
time.sleep(3)
detail = get_order(order_id)["data"]
if detail["fulfilledPercent"] == 100 or detail["remainAmount"] == 0:
print("Energy delivered - order fulfilled")
break
print("Waiting for fill...")
if __name__ == "__main__":
main()
How it works
- Set your
API_KEY,RECEIVER_ADDRESS, and how much energy to buy (65,000 covers one USDT transfer; ~130,000 for a first-time recipient). buy_resource()posts the order.unitPriceaccepts a price in SUN or the presetsSLOW/MEDIUM/FAST; setmaxPriceAcceptedhigh enough to cover current order-book prices.- It then polls
get_order()every 3 seconds until the delegation is fulfilled — usually a few seconds.
Testnet vs mainnet
Develop against testnet with https://api-dev.tronsave.io (Nile, no real TRX), then switch to https://api.tronsave.io for production.
Working in another language? The same flow is available as an SDK for Node.js, Rust, Java and PHP, and there’s a general API automation guide. Full reference lives in the TronSave docs.
FAQ
Do I need TronWeb?
Not for the API-key flow above — plain requests is enough. TronWeb is only needed if you sign and pay directly with a private key.
How much energy per USDT transfer?
About 65,000 (≈130,000 to a wallet that has never held USDT).
Where does payment come from?
Your prepaid TronSave balance, topped up on the dashboard.
Build it: grab an API key on the TronSave market and read the developer docs.
