Verify a Gnosis RPC endpoint with Node.js

Check that an RPC endpoint identifies Gnosis Chain and returns a recent block number using standard JSON-RPC requests.

To verify a Gnosis RPC connection, call eth_chainId and confirm that it returns 0x64, then call eth_blockNumber to check that the endpoint can read the current block height. The Node.js script below performs both checks without installing a package.

Get an RPC endpoint

Obtain a Gnosis Chain RPC URL from your provider and keep any API key in that URL private. The OnFinality Gnosis network page is a starting point for finding network-specific access information.

You need Node.js 18 or later. Set the URL as an environment variable rather than putting it in source code or committing it to version control.

  • The expected Gnosis Chain ID is 100 in decimal, or 0x64 in hexadecimal.
  • An RPC URL provides access to node methods; it is not itself an indexed application database.

Check the chain ID and block height

Save the following as check-gnosis-rpc.js. Set GNOSIS_RPC_URL in your shell to your actual endpoint URL, then run node check-gnosis-rpc.js.

    JavaScript · Node.js
    const url = process.env.GNOSIS_RPC_URL;
    
    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}`);
      }
    
      let body;
      try {
        body = await response.json();
      } catch {
        throw new Error(`${method}: response was not valid JSON`);
      }
      if (!body || 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-fA-F]+$/.test(body.result)) {
        throw new Error(`${method}: expected a hexadecimal quantity`);
      }
      return body.result;
    }
    
    async function main() {
      if (!url) throw new Error('Set GNOSIS_RPC_URL before running this script');
    
      const chainId = await rpc('eth_chainId', 1);
      if (BigInt(chainId) !== 100n) {
        throw new Error(`Wrong chain ID: ${chainId}; expected 0x64`);
      }
      const block = await rpc('eth_blockNumber', 2);
      console.log(`Gnosis chain ID verified: ${chainId}`);
      console.log(`Reported latest block: ${BigInt(block).toString()}`);
    }
    
    main().catch((error) => {
      // Avoid printing the URL, which may contain credentials.
      console.error(`RPC check failed: ${error.message}`);
      process.exitCode = 1;
    });

    On success, the script prints a verified chain ID and a decimal block number. The block number changes as the chain advances; the script does not assert a particular height.

    A wrong chain ID means the endpoint is not identifying itself as Gnosis Chain. An HTTP or JSON-RPC error may indicate an invalid credential, a restricted method, or a provider-side problem. A timeout or network error warrants checking the URL and connectivity. The script exits with a nonzero status on failure.

    Interpret the result and choose the next step

    A successful response confirms what the endpoint reports at that moment; it does not prove that the node is fully synchronized. To investigate freshness, repeat the block-height check and compare its result with an independent trusted Gnosis source at roughly the same time.

    Use RPC calls for direct node access, such as reading a block height or submitting a transaction. For application queries over processed historical events, use an indexing and query layer: it ingests chain data and makes the resulting dataset available for application-specific queries.

    For more RPC guidance, visit the OnFinality RPC Assistant.