Verify BNB Smart Chain RPC Access with Node.js
Connect a Node.js script to a BNB Smart Chain JSON-RPC endpoint, confirm its chain ID, and read the latest block number.
Use a BNB Smart Chain JSON-RPC endpoint to confirm that your application can reach the network and read its latest block number. Set the endpoint URL you obtained from your RPC provider, then run this Node.js 18+ script. BNB Smart Chain’s mainnet chain ID is 56, represented as 0x38 in JSON-RPC.
Get a BNB Smart Chain endpoint
Choose a BNB Smart Chain endpoint from your RPC provider and set it as an environment variable. The OnFinality BNB network page is one place to start. Use the endpoint URL provided for your project; this guide does not assume a particular URL.
- Keep any endpoint credentials private. Avoid committing URLs that contain secrets to source control.
- This task checks RPC access: it sends JSON-RPC requests and reads blockchain data. It does not index the chain or provide indexed queries.
export BSC_RPC_URL='YOUR_BNB_SMART_CHAIN_RPC_URL'Replace the placeholder with the endpoint URL from your provider before running the script.
Check the chain and latest block
Save the following as check-bsc.mjs. It requests the chain ID and latest block number, rejects non-success HTTP responses and JSON-RPC errors, and stops if the endpoint reports a different chain ID.
const endpoint = process.env.BSC_RPC_URL;
if (!endpoint) {
throw new Error('Set BSC_RPC_URL to your BNB Smart Chain RPC endpoint.');
}
async function rpc(method, params = []) {
const response = await fetch(endpoint, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }),
signal: AbortSignal.timeout(15_000),
});
if (!response.ok) {
throw new Error(`RPC HTTP error: ${response.status} ${response.statusText}`);
}
const body = await response.json();
if (body.error) {
throw new Error(`RPC error ${body.error.code}: ${body.error.message}`);
}
if (!Object.hasOwn(body, 'result')) {
throw new Error('RPC response did not contain a result.');
}
return body.result;
}
try {
const chainId = await rpc('eth_chainId');
if (chainId.toLowerCase() !== '0x38') {
throw new Error(`Expected BNB Smart Chain mainnet chain ID 0x38; received ${chainId}.`);
}
const blockNumber = await rpc('eth_blockNumber');
if (!/^0x[0-9a-f]+$/i.test(blockNumber)) {
throw new Error(`Unexpected block number result: ${blockNumber}`);
}
console.log(`Connected to BNB Smart Chain mainnet (chain ID ${chainId}).`);
console.log(`Latest block number: ${BigInt(blockNumber).toString()}`);
} catch (error) {
console.error(`Could not verify BNB Smart Chain RPC access: ${error.message}`);
process.exitCode = 1;
}Run it with node check-bsc.mjs. On success, it prints the chain ID and the latest block number returned by the endpoint. Block height changes over time, so the number will vary.
On a timeout or connection error, check that the endpoint URL is correct and reachable. An HTTP error or JSON-RPC error means the request reached a server that did not complete the call; inspect the status or error message and your provider’s endpoint requirements.
Use RPC for live reads; use indexing for queries
This example reads current network data through RPC. If your application needs to search historical events, filter across many blocks, or serve structured application queries, an indexer can collect and organize chain data for querying. RPC access alone does not perform that indexing.
For more BNB Smart Chain details, see the OnFinality BNB network page. To continue exploring developer guides, visit OnFinality Learn or use the RPC Assistant to work with RPC requests.