CookBook
The TronWeb Cookbook is a collection of practical, example‑driven guides and code snippets that show how to use TronWeb. With it, you can quickly:
- Find a working snippet for a task you’re trying to implement.
- Copy, adapt, and run code immediately.
- Learn best practices.
For developing and testing
- Choose an http api network from here. For example, if you want to use Nile Testnet, you can use
https://api.nileex.io. But you can also use a local network, see here. - Connect to the network you chose.
- Write your code. You may need to use TRX in your code. For testnet, go to faucet respectively to get the TRX for testing. For local network, you can use
tre_setAccountBalanceto activate the account and get TRX. - Live code editor:
Using Nile Testnet.
Using local network, this code might not work in browser because local network uses http protocol while the site is hosted on https.
Contracts manipulation
Supposing you have the below contract and you want to deploy it and trigger it, you can do like follow.
pragma solidity ^0.4.18;
contract funcABIV2 {
uint256 public check;
constructor(uint256 _check) public {
check = _check;
}
function setCheck(uint256 _check) public {
check = _check;
}
}
You can get its abi and bytecode by compiling it using TronIDE.
Deploy the contract and trigger it:
Build/sign/broadcast transactions
TronWeb has supported 36 transaction types. You can go here to see all transaction types. Here we use sendTrx transaction as an example to see how to build/sign/broadcast a transaction:
Events subscribing
You may need to subscribe events to get the real-time status of a transaction. Here is an example to do it: