Verify a Robinhood Network RPC Endpoint with Node.js

Connect to a Robinhood Network JSON-RPC endpoint, check its chain ID, and retrieve its latest block number.

To verify a Robinhood Network RPC endpoint, send eth_chainId and eth_blockNumber requests, then compare the returned chain ID with the one published for your intended network. The Node.js script below performs both checks without requiring an npm package. RPC access lets you request blockchain data; it is not the same as indexing that data for application queries.

Prepare the endpoint and expected chain ID

Check the OnFinality Robinhood network page for current access information. Use an HTTPS JSON-RPC endpoint you are authorized to access; if the page does not provide one, obtain it from your chosen provider.

Independently find the chain ID for the specific Robinhood network environment you intend to use in its authoritative documentation. Enter it as a hexadecimal quantity, including the 0x prefix. Do not assume a test environment and a production environment have the same chain ID.

  • Keep endpoint URLs and embedded API credentials out of source control and shared logs.
  • These environment variables apply to the current shell. Replace both placeholders before running the script.
bash
export RPC_URL='<paste-your-https-rpc-endpoint>'
export EXPECTED_CHAIN_ID='<paste-the-verified-hex-chain-id>'

If your provider requires additional authentication headers rather than credentials in the URL, adapt the request headers in the script using that provider's instructions.

Create the RPC check

Save the following as check-rpc.js. It requires Node.js 18 or later and uses its built-in fetch. Each request has a 10-second timeout; that is a client-side limit for this check, not a provider performance claim.

  • The script validates the JSON-RPC response and stops if the returned chain ID does not match your independently verified value.
  • Errors avoid printing the endpoint URL or the provider's response message, which could contain sensitive information.
JavaScript · Node.js
const rpcUrl = process.env.RPC_URL;
const expectedChainId = process.env.EXPECTED_CHAIN_ID;
const quantityPattern = /^0x(?:0|[1-9a-f][0-9a-f]*)$/i;

async function rpc(method, id) {
  let response;
  try {
    response = await fetch(rpcUrl, {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({ jsonrpc: '2.0', id, method, params: [] }),
      signal: AbortSignal.timeout(10000)
    });
  } catch (error) {
    if (error.name === 'TimeoutError' || error.name === 'AbortError') {
      throw new Error(`${method} timed out after 10 seconds`);
    }
    throw new Error(`${method} could not reach the endpoint`);
  }

  if (!response.ok) {
    throw new Error(`${method} returned HTTP ${response.status}`);
  }

  let body;
  try {
    body = await response.json();
  } catch {
    throw new Error(`${method} returned invalid JSON`);
  }

  if (!body || body.jsonrpc !== '2.0' || body.id !== id) {
    throw new Error(`${method} returned an unexpected JSON-RPC response`);
  }
  if (body.error) {
    const code = Number.isInteger(body.error.code) ? body.error.code : 'unknown';
    throw new Error(`${method} returned JSON-RPC error code ${code}`);
  }
  if (typeof body.result !== 'string' || !quantityPattern.test(body.result)) {
    throw new Error(`${method} returned an invalid hexadecimal quantity`);
  }
  return body.result;
}

async function main() {
  if (!rpcUrl || !expectedChainId || !quantityPattern.test(expectedChainId)) {
    throw new Error('Set RPC_URL and a valid hexadecimal EXPECTED_CHAIN_ID');
  }
  let url;
  try {
    url = new URL(rpcUrl);
  } catch {
    throw new Error('RPC_URL must be a valid HTTPS URL');
  }
  if (url.protocol !== 'https:') {
    throw new Error('RPC_URL must use HTTPS');
  }

  const chainId = await rpc('eth_chainId', 1);
  if (BigInt(chainId) !== BigInt(expectedChainId)) {
    throw new Error('Chain ID mismatch: check the endpoint and intended network');
  }
  const latestBlock = await rpc('eth_blockNumber', 2);
  console.log(JSON.stringify({ chainId, latestBlock }, null, 2));
}

main().catch((error) => {
  console.error(`RPC check failed: ${error.message}`);
  process.exitCode = 1;
});

The chain ID check verifies that the endpoint reports the expected identifier; it does not independently prove the identity or trustworthiness of the endpoint.

Run the check and interpret the result

In the same shell where you set the variables, run the script:

  • On success, expect a JSON object with chainId and latestBlock fields. Both are hexadecimal strings; the block number reflects the endpoint's response at the time of the request.
  • For an HTTP error, check the endpoint, credentials, and provider instructions. For a JSON-RPC error, check whether the endpoint accepts the requested method.
  • For a timeout or connection failure, check your network and endpoint configuration. For a chain ID mismatch, stop and verify the intended environment and its published chain ID rather than changing the expected value blindly.
bash
node check-rpc.js

A successful response confirms that these two requests worked at that moment; it does not establish historical data availability, uptime, or indexing support.

If your application needs searchable historical events or aggregated data, an indexer processes blockchain data into a queryable store; the RPC check alone does not create that index. As a next step, explore OnFinality Learn for more RPC guidance.