Provider Configuration
TronWeb supports flexible node provider configuration for connecting to different TRON networks and custom nodes.
Basic Configuration
Using fullHost (Recommended)
The simplest configuration — uses the same URL for all services:
import TronWeb from 'tronweb';
const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io',
headers: { 'TRON-PRO-API-KEY': 'your-api-key' },
privateKey: 'your-private-key',
});
Individual Node Configuration
Specify separate endpoints for each service:
const tronWeb = new TronWeb({
fullNode: 'https://api.trongrid.io',
solidityNode: 'https://api.trongrid.io',
eventServer: 'https://api.trongrid.io',
headers: { 'TRON-PRO-API-KEY': 'your-api-key' },
privateKey: 'your-private-key',
});
Network Endpoints
Mainnet
| Provider | URL |
|---|---|
| TronGrid | https://api.trongrid.io |
| Custom node | Your self-hosted Full Node URL |
Nile Testnet
| Provider | URL |
|---|---|
| TronGrid Nile | https://nile.trongrid.io |
Shasta Testnet
| Provider | URL |
|---|---|
| TronGrid Shasta | https://api.shasta.trongrid.io |
Node Types
| Node Type | Property | Purpose |
|---|---|---|
| Full Node | fullNode | Latest (unconfirmed) blockchain state, broadcasting transactions |
| Solidity Node | solidityNode | Confirmed (solidified) blockchain state only |
| Event Server | eventServer | Smart contract event queries |
Changing Providers at Runtime
// Change full node
tronWeb.setFullNode('https://new-fullnode.example.com');
// Change solidity node
tronWeb.setSolidityNode('https://new-soliditynode.example.com');
// Change event server
tronWeb.setEventServer('https://new-eventserver.example.com');
// Change custom headers
tronWeb.setHeader({ 'TRON-PRO-API-KEY': 'new-api-key' });
Custom Headers
Headers are attached to every HTTP request. Use them for API keys and custom metadata:
const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io',
headers: {
'TRON-PRO-API-KEY': 'your-api-key',
'Custom-Header': 'value',
},
});
Using with TronLink
When your dApp runs in a browser with TronLink installed, TronWeb is injected into window.tronWeb:
// Wait for TronLink to inject TronWeb
if (window.tronWeb && window.tronWeb.ready) {
const tronWeb = window.tronWeb;
const address = tronWeb.defaultAddress.base58;
console.log('Connected:', address);
}
// Listen for TronLink account changes
window.addEventListener('message', (event) => {
if (event.data.message?.action === 'setAccount') {
console.log('Account changed:', event.data.message.data.address);
}
});
Multiple TronWeb Instances
You can create multiple instances for different networks:
// Mainnet instance
const mainnet = new TronWeb({ fullHost: 'https://api.trongrid.io' });
// Testnet instance
const testnet = new TronWeb({ fullHost: 'https://nile.trongrid.io' });
// Use both
const mainBalance = await mainnet.trx.getBalance(address);
const testBalance = await testnet.trx.getBalance(address);
TronGrid API Keys
TronGrid provides free API access with rate limits. For production use, register for an API key:
- Visit trongrid.io and create an account
- Create an API key in the dashboard
- Add the key to your TronWeb configuration:
const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io',
headers: { 'TRON-PRO-API-KEY': 'your-api-key' },
});
| Plan | Rate Limit | Notes |
|---|---|---|
| Free | Limited QPS | Sufficient for development |
| Pro | Higher QPS | For production applications |