Connect to the TRON network
Connect to a TRON node through a public HTTP endpoint or your own self-hosted full node, using TronWeb (JavaScript) or Trident (Java).
Prerequisites
When developing on TRON, the first step is to establish a connection to the network. Several infrastructure providers offer the nodes and APIs your applications need to send transactions, query on-chain data, and interact with smart contracts. This page covers the standard connection patterns.
HTTP endpoints from TronGrid
TronGrid is a hosted node service for TRON. The HTTP endpoints below cover Mainnet and the public testnets:
| Network | Full node HTTP endpoint |
|---|---|
| Mainnet | https://api.trongrid.io |
| Shasta testnet | https://api.shasta.trongrid.io |
| Nile testnet | https://nile.trongrid.io |
| Local node | http://127.0.0.1:8090 or http://localhost:8090 |
Connecting from JavaScript (TronWeb)
TronWeb is the primary JavaScript SDK for TRON. It works in browsers, Node.js, and IoT environments, and exposes TRON's transaction-building, signing, and broadcasting flow. For full reference, see the TronWeb documentation.
const { TronWeb } = require('tronweb');
const privateKey = process.env.TRON_PRIVATE_KEY;
const apiKey = process.env.TRON_PRO_API_KEY;
if (privateKey && !/^[0-9a-fA-F]{64}$/.test(privateKey)) {
throw new Error('Set TRON_PRIVATE_KEY to a 64-character hexadecimal private key');
}
const options = {
fullHost: 'https://api.shasta.trongrid.io',
// Mainnet: https://api.trongrid.io
// Nile testnet: https://nile.trongrid.io
headers: apiKey ? { 'TRON-PRO-API-KEY': apiKey } : {}
};
if (privateKey) options.privateKey = privateKey;
const tronWeb = new TronWeb(options);The example reads the optional signing key and TronGrid API key from environment variables. Read-only calls do not require a private key. For signing, browser DApps should request authorization from a wallet such as TronLink; backend services should use a secrets manager or isolated signer. Do not place a private key in source code or commit it to a repository. For Mainnet access, obtain a key from the TronGrid dashboard.
Connecting from Java (Trident)
Trident is a lightweight Java SDK for TRON. It supports offline address generation, offline transaction signing, and full-node HTTP and gRPC APIs. The following example creates one client based on TRON_NETWORK and defaults to Shasta. A mainnet connection also requires TRON_PRO_API_KEY. For full reference, see the Trident documentation.
import org.tron.trident.core.ApiWrapper;
import java.util.Locale;
String privateKey = System.getenv("TRON_PRIVATE_KEY");
if (privateKey == null || !privateKey.matches("[0-9a-fA-F]{64}")) {
throw new IllegalArgumentException("TRON_PRIVATE_KEY must contain 64 hexadecimal characters");
}
String network = System.getenv("TRON_NETWORK");
if (network == null || network.trim().isEmpty()) {
network = "shasta";
}
ApiWrapper client;
switch (network.trim().toLowerCase(Locale.ROOT)) {
case "shasta":
client = ApiWrapper.ofShasta(privateKey);
break;
case "nile":
client = ApiWrapper.ofNile(privateKey);
break;
case "mainnet": {
String apiKey = System.getenv("TRON_PRO_API_KEY");
if (apiKey == null || apiKey.trim().isEmpty()) {
throw new IllegalArgumentException("TRON_PRO_API_KEY is required for mainnet");
}
client = ApiWrapper.ofMainnet(privateKey, apiKey);
break;
}
case "custom": {
String fullNode = System.getenv("TRON_FULLNODE_GRPC");
String solidityNode = System.getenv("TRON_SOLIDITY_GRPC");
if (fullNode == null || fullNode.trim().isEmpty()
|| solidityNode == null || solidityNode.trim().isEmpty()) {
throw new IllegalArgumentException(
"TRON_FULLNODE_GRPC and TRON_SOLIDITY_GRPC are required for a custom network"
);
}
client = new ApiWrapper(fullNode, solidityNode, privateKey);
break;
}
default:
throw new IllegalArgumentException("Unsupported TRON_NETWORK: " + network);
}Public nodes vs self-hosted nodes
Public endpoints provide a quick way to begin development, but they are subject to rate limits, availability constraints, and provider-specific indexing. Production applications can use a managed node service, operate a self-hosted node, or combine both approaches. For endpoint and rate-limit details, see Networks.
Self-hosting provides more control over synchronization, indexing, and network configuration, while adding operational overhead. To begin, see Nodes and clients and Deploy a Fullnode or Super Representative node.
Related resources
- Getting testnet tokens — request free TRX before sending your first transaction
- Send your first transaction — end-to-end TRX transfer using the TronWeb endpoint above
- Sign and broadcast via raw HTTP — what TronWeb does under the hood
Updated 5 days ago