Skip to main content
Version: 6.2.0 - 6.2.1

Provider Configuration

TronWeb supports flexible node provider configuration for connecting to different TRON networks and custom nodes.

Basic Configuration

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

ProviderURL
TronGridhttps://api.trongrid.io
Custom nodeYour self-hosted Full Node URL

Nile Testnet

ProviderURL
TronGrid Nilehttps://nile.trongrid.io

Shasta Testnet

ProviderURL
TronGrid Shastahttps://api.shasta.trongrid.io

Node Types

Node TypePropertyPurpose
Full NodefullNodeLatest (unconfirmed) blockchain state, broadcasting transactions
Solidity NodesolidityNodeConfirmed (solidified) blockchain state only
Event ServereventServerSmart 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',
},
});

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:

  1. Visit trongrid.io and create an account
  2. Create an API key in the dashboard
  3. Add the key to your TronWeb configuration:
const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io',
headers: { 'TRON-PRO-API-KEY': 'your-api-key' },
});
PlanRate LimitNotes
FreeLimited QPSSufficient for development
ProHigher QPSFor production applications