Verify Avalanche C-Chain RPC Access with Node.js
Check that an Avalanche C-Chain JSON-RPC endpoint responds, reports the expected chain ID, and returns a block number.
Use eth_chainId and eth_blockNumber to verify that an Avalanche C-Chain JSON-RPC endpoint is reachable and responding with valid data. You will need an endpoint URL from your RPC provider; the Avalanche network page is one place to find network information. This check confirms RPC access only. It does not index blockchain data or provide indexed queries.
Set your RPC endpoint
Provide the endpoint URL you received from your RPC provider through an environment variable. Keep credentials in the URL out of source control and avoid sharing logs that contain them.
export AVALANCHE_RPC_URL='YOUR_C_CHAIN_JSON_RPC_ENDPOINT'
node --versionUse Node.js 18 or newer. Replace the placeholder with your provider's endpoint; it is not a usable endpoint as written.
Request the chain ID and latest block
This script sends two standard Ethereum JSON-RPC requests used by Avalanche C-Chain. It checks HTTP status, JSON parsing, JSON-RPC errors, and the returned chain ID before reporting the block number.
const endpoint = process.env.AVALANCHE_RPC_URL;
if (!endpoint) {
console.error('Set AVALANCHE_RPC_URL to your C-Chain JSON-RPC endpoint.');
process.exit(1);
}
async function rpc(method) {
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(10_000),
});
if (!response.ok) {
throw new Error(`${method}: HTTP ${response.status} ${response.statusText}`);
}
let payload;
try {
payload = await response.json();
} catch {
throw new Error(`${method}: response was not valid JSON`);
}
if (payload.error) {
throw new Error(`${method}: RPC error ${payload.error.code}: ${payload.error.message}`);
}
if (payload.jsonrpc !== '2.0' || payload.id !== 1 || payload.result === undefined) {
throw new Error(`${method}: response did not contain a valid JSON-RPC result`);
}
return payload.result;
}
try {
const chainId = await rpc('eth_chainId');
if (chainId.toLowerCase() !== '0xa86a') {
throw new Error(`Unexpected chain ID ${chainId}; expected Avalanche C-Chain mainnet (0xa86a)`);
}
const blockNumber = await rpc('eth_blockNumber');
if (!/^0x[0-9a-f]+$/i.test(blockNumber)) {
throw new Error(`eth_blockNumber returned an invalid hex quantity: ${blockNumber}`);
}
console.log(JSON.stringify({ chainId, blockNumber, blockNumberDecimal: BigInt(blockNumber).toString() }, null, 2));
} catch (error) {
console.error(`RPC check failed: ${error.message}`);
process.exit(1);
}Save this as check-avalanche.mjs and run node check-avalanche.mjs. A successful response has this shape; the block values will vary with the chain state:
``json
{
"chainId": "0xa86a",
"blockNumber": "0x...",
"blockNumberDecimal": "..."
}
``
A timeout, HTTP error, invalid JSON, JSON-RPC error, or unexpected chain ID exits with an error message. Check that the endpoint is correct and accessible, that any required credentials are valid, and that the endpoint is configured for Avalanche C-Chain mainnet.
Know what this check does—and what it does not
A JSON-RPC endpoint lets an application call node methods such as eth_blockNumber. Historical analysis, filtering across blocks, or querying application-specific data may require indexing and a query layer. RPC access alone does not create indexed data or answer those queries.
For a request pattern that uses curl, see the OnFinality RPC Assistant. For more blockchain development guides, continue to OnFinality Learn.