Check an Optimism RPC endpoint with Node.js

Send a JSON-RPC request to an Optimism endpoint, verify the response, and distinguish RPC access from indexed data queries.

To check whether an Optimism RPC endpoint responds, send it an eth_chainId request and confirm the result is 0xa (chain ID 10). Set OPTIMISM_RPC_URL to the endpoint you obtained from your provider; this guide does not assume a particular endpoint URL.

Get an endpoint and set it locally

Choose an endpoint for Optimism from OnFinality’s Optimism network page, or use the endpoint supplied by your RPC provider. Keep any access token private and avoid committing it to source control.

In your shell, set the endpoint in an environment variable. Replace the example value with your actual endpoint URL.

    bash
    export OPTIMISM_RPC_URL='YOUR_OPTIMISM_RPC_ENDPOINT'

    The value above is a placeholder, not a working endpoint.

    Check the RPC response

    Run this with Node.js 18 or newer. It sends one standard JSON-RPC request, checks the HTTP response and JSON-RPC error field, then verifies the returned chain ID.

      JavaScript · Node.js
      const endpoint = process.env.OPTIMISM_RPC_URL;
      
      if (!endpoint) {
        console.error('Set OPTIMISM_RPC_URL to your Optimism RPC endpoint.');
        process.exit(1);
      }
      
      try {
        const response = await fetch(endpoint, {
          method: 'POST',
          headers: { 'content-type': 'application/json' },
          body: JSON.stringify({
            jsonrpc: '2.0',
            method: 'eth_chainId',
            params: [],
            id: 1
          }),
          signal: AbortSignal.timeout(10_000)
        });
      
        if (!response.ok) {
          throw new Error(`HTTP ${response.status} ${response.statusText}`);
        }
      
        const payload = await response.json();
        if (payload.error) {
          throw new Error(`JSON-RPC error ${payload.error.code}: ${payload.error.message}`);
        }
        if (payload.id !== 1 || typeof payload.result !== 'string') {
          throw new Error('Unexpected JSON-RPC response shape.');
        }
      
        if (payload.result.toLowerCase() !== '0xa') {
          throw new Error(`Expected Optimism chain ID 0xa; received ${payload.result}.`);
        }
      
        console.log(`RPC responded with Optimism chain ID ${payload.result}.`);
      } catch (error) {
        console.error(`RPC check failed: ${error.message}`);
        process.exit(1);
      }

      Expected result on success: RPC responded with Optimism chain ID 0xa. A timeout, non-2xx HTTP status, JSON-RPC error, malformed response, or different chain ID exits with an error. Check that the endpoint is correct and reachable, and that any required credentials are present.

      Know what this check confirms

      A successful eth_chainId response confirms that this endpoint handled this RPC request and reported Optimism’s chain ID. It does not establish that every RPC method is available or measure latency, reliability, or throughput.

      RPC access lets an application request blockchain data and submit transactions through supported methods. Indexing and querying are different tasks: an indexer processes chain data into a queryable representation. An RPC endpoint check does not create an index or provide an indexed query API. For more developer guides, visit OnFinality Learn; for endpoint discovery, see the RPC Assistant.