Verify an EVM JSON-RPC Endpoint Before You Integrate It

Use Node.js to check an EVM endpoint’s chain ID and latest reported block, then decide whether your application also needs an indexer.

To evaluate a blockchain JSON-RPC solution for an EVM application, start by checking that its endpoint answers requests for the chain you intend to use. The Node.js script below checks the chain ID and reads the latest reported block number; it does not establish uptime, speed, or historical data availability.

Choose an endpoint and the expected chain

Obtain an HTTPS JSON-RPC endpoint from your provider and confirm the intended network’s chain ID from an authoritative source, such as that network’s documentation. Keep endpoint URLs containing API keys out of source control and logs.

You can use the OnFinality RPC Assistant to explore RPC options, then run the same check against any endpoint you are considering. Use an endpoint and chain ID supplied or confirmed for your chosen network.

  • Requirements: Node.js 18 or later, network access to the endpoint, and an expected chain ID in decimal or 0x-prefixed hexadecimal form.
  • A successful chain ID check catches an obvious network mismatch, but it does not prove that the endpoint is trustworthy or synchronized with the rest of the network.

Run the JSON-RPC check

Save this script as rpc-check.js. Set RPC_URL and EXPECTED_CHAIN_ID in your environment, then run node rpc-check.js. The script exits with a nonzero status on a missing setting, timeout, HTTP error, JSON-RPC error, malformed result, or chain mismatch.

    JavaScript · Node.js
    const url = process.env.RPC_URL;
    const expected = process.env.EXPECTED_CHAIN_ID;
    
    async function rpc(method, id) {
      const response = await fetch(url, {
        method: 'POST',
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({ jsonrpc: '2.0', id, method, params: [] }),
        signal: AbortSignal.timeout(10000)
      });
      if (!response.ok) throw new Error(`${method}: HTTP ${response.status}`);
      const body = await response.json();
      if (body.jsonrpc !== '2.0' || body.id !== id) {
        throw new Error(`${method}: invalid JSON-RPC response`);
      }
      if (body.error) throw new Error(`${method}: JSON-RPC error ${body.error.code}`);
      if (typeof body.result !== 'string' || !/^0x[0-9a-f]+$/i.test(body.result)) {
        throw new Error(`${method}: invalid hexadecimal result`);
      }
      return BigInt(body.result);
    }
    
    async function main() {
      if (!url || !expected) throw new Error('Set RPC_URL and EXPECTED_CHAIN_ID');
      if (!/^(?:[0-9]+|0x[0-9a-f]+)$/i.test(expected)) {
        throw new Error('EXPECTED_CHAIN_ID must be decimal or 0x-prefixed hexadecimal');
      }
      const chainId = await rpc('eth_chainId', 1);
      if (chainId !== BigInt(expected)) throw new Error('Chain ID mismatch');
      const block = await rpc('eth_blockNumber', 2);
      console.log(`Chain ID verified: ${chainId}`);
      console.log(`Latest reported block: ${block}`);
    }
    
    main().catch((error) => {
      console.error(`RPC check failed: ${error.message}`);
      process.exitCode = 1;
    });

    Expected result: two lines reporting the verified chain ID and the endpoint’s latest reported block number. The values depend on the selected network and when you run the script.

    If the check fails, verify the endpoint, credentials, network access, and expected chain ID. A timeout or HTTP error may be transient; investigate before treating a single failure as a provider-wide problem.

    Interpret the result and choose the next test

    JSON-RPC gives your application access to node methods, such as reading a block number or submitting a transaction. Indexing is a separate task: an indexer processes chain data into a queryable model, which can make application-specific historical queries practical. Passing this RPC check does not mean an endpoint provides an indexed query API.

    For a broader evaluation, exercise the exact methods and block ranges your application needs. Compare reported block heights with a trusted independent reference, and measure latency and errors over a representative period from your deployment environment. Test historical reads explicitly if you need them; a latest-block response does not establish historical data availability.

    Next, explore the OnFinality Learn resources for more context, or return to the RPC Assistant when evaluating endpoint options.

    • Use RPC directly for supported node operations; consider indexing when the application needs to query processed historical data by its own fields.
    • Record your test conditions and repeat measurements before comparing providers. Do not infer uptime or performance from this single check.