๐กIntroduction to TRON APIs
The TRON API provides developers with programmatic access to the TRON blockchain. Whether you're building a dApp, integrating TRON payments, or analyzing blockchain data, the TRON API ecosystem offers the tools you need.
TRON APIs are accessible via HTTP/REST and gRPC protocols. The most popular way to interact with TRON is through TronWeb, a JavaScript library that simplifies blockchain interaction.
TRON APIs are the bridge between your application and the TRON blockchain. They handle everything from querying account balances to deploying smart contracts and sending transactions.
๐ฆTronWeb โ The Primary Library
TronWeb is the official JavaScript library for interacting with the TRON blockchain. It provides a simple, intuitive interface for:
- Account management: Create wallets, import private keys, check balances.
- Transactions: Send TRX and TRC20 tokens (like USDT TRC20).
- Smart contracts: Deploy and interact with TRC20 and custom contracts.
- Blockchain queries: Get block data, transaction history, and more.
- Resource management: Check Energy and Bandwidth, stake/unstake TRX.
Install TronWeb via npm: npm install tronweb or use a CDN: https://cdn.jsdelivr.net/npm/tronweb/dist/TronWeb.js
๐TRON RPC Endpoints
TRON provides RPC (Remote Procedure Call) endpoints for direct blockchain interaction. Here are the main categories:
| Category | Endpoint | Description |
|---|---|---|
| Account | /wallet/getaccount | Get account information (balance, permissions, etc.) |
| Transaction | /wallet/createtransaction | Create and send transactions |
| Contract | /wallet/triggersmartcontract | Execute smart contract functions |
| Block | /wallet/getblock | Get block information by number or ID |
| Node | /wallet/getnodeinfo | Get node status and information |
| Resource | /wallet/getaccountresource | Get account Energy and Bandwidth |
TRON provides public nodes at https://api.trongrid.io (mainnet) and https://api.shasta.trongrid.io (testnet). For production applications, consider running your own node.
๐Getting Started with TRON API
Here's a quick example to get you started with TronWeb:
Always test your code on Shasta testnet before deploying to mainnet. You can get free test TRX from the Shasta faucet.
๐ชTRC20 Token API
Interacting with TRC20 tokens (like USDT TRC20) is straightforward with TronWeb. Here's how to work with TRC20 contracts:
- Contract address: USDT TRC20 is TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t
- Read functions: balanceOf(address), decimals(), symbol()
- Write functions: transfer(to, amount), approve(spender, amount)
- Event logs: Transfer, Approval events
USDT TRC20 uses 6 decimal places. To send 100 USDT, multiply by 10^6 (100,000,000). Always check the token's decimal value before sending.
๐Smart Contract API
TRON APIs support full smart contract interaction. Key functions:
- Deploy contract: tronWeb.contract().new(...)
- Call read function: contract.methodName(params).call() โ no fee, view-only
- Execute write function: contract.methodName(params).send() โ requires fee (Energy/TRX)
- Get contract info: tronWeb.trx.getContract(address)
- Event listening: tronWeb.trx.getEventResult(contractAddress, options)
Smart contract functions (write operations) consume Energy. Ensure your account has sufficient Energy or TRX to cover the fee. Staking TRX for Energy is recommended for high-volume applications.
โ๏ธTransaction Building
TRON APIs allow you to build and customize transactions:
- Manual transaction creation: tronWeb.transactionBuilder.sendTrx(...)
- Set fee limit: tronWeb.transactionBuilder.triggerSmartContract(...) with feeLimit parameter
- Sign transaction: tronWeb.trx.sign(tx, privateKey)
- Broadcast transaction: tronWeb.trx.sendRawTransaction(signedTx)
- Check transaction status: tronWeb.trx.getTransactionInfo(txid)
The typical flow is: build โ sign โ broadcast โ confirm. TronWeb handles most of this automatically for standard operations.
๐ผCommon Use Cases
Accept TRX or USDT TRC20 payments. Monitor incoming transactions, confirm blocks, and automate payment processing.
Query blockchain data for analytics, reporting, and monitoring. Track transactions, balances, and contract activity.
Automate recurring transactions, staking, and other on-chain activities using scheduled API calls.
Build full-featured dApps with TronWeb, interacting with smart contracts, managing user wallets, and more.