How to Choose an RPC Provider by Testing Chain Identity
Run a Node.js check against an EVM RPC endpoint, verify its chain ID, and use the result to evaluate a provider before integrating it.
Choose an RPC provider by testing a real endpoint against your application's requirements before integrating it. Start with the check below: it verifies that an EVM JSON-RPC endpoint responds and identifies itself as the chain you expect. A successful check is a prerequisite, not proof of reliability, speed, or historical data availability.
Prepare an endpoint and expected chain ID
Obtain an HTTPS EVM JSON-RPC endpoint from the provider you want to evaluate. Use the provider's dashboard or documentation rather than guessing the URL. Keep endpoint URLs private when they contain API keys.
Find the expected chain ID in your target network's official documentation. The check accepts a decimal or 0x-prefixed hexadecimal chain ID. Repeat it for each candidate endpoint using the same expected value.
If you need help identifying options to evaluate, use the OnFinality RPC Assistant. Confirm endpoint details and current service terms with each provider.
- Use a non-production credential with only the access needed for testing, if the provider offers one.
- Do not commit the endpoint URL, API key, or shell history containing credentials to a repository.
Run the chain identity check
With Node.js 18 or later installed, set RPC_URL and EXPECTED_CHAIN_ID in your shell, then run the following script. It sends eth_chainId, enforces a 10-second timeout, and exits with a nonzero status when the request or validation fails.
node <<'NODE'
const url = process.env.RPC_URL;
const expected = process.env.EXPECTED_CHAIN_ID;
async function main() {
if (!url || !expected) throw new Error('Set RPC_URL and EXPECTED_CHAIN_ID first.');
if (!/^(?:0x[0-9a-fA-F]+|[0-9]+)$/.test(expected)) {
throw new Error('EXPECTED_CHAIN_ID must be decimal or 0x-prefixed hexadecimal.');
}
const parsedUrl = new URL(url);
if (parsedUrl.protocol !== 'https:') throw new Error('RPC_URL must use HTTPS.');
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 10000);
try {
const response = await fetch(url, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'eth_chainId', params: [] }),
signal: controller.signal
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const body = await response.json();
if (body.error) throw new Error(`JSON-RPC error ${body.error.code}: ${body.error.message}`);
if (body.jsonrpc !== '2.0' || body.id !== 1 ||
typeof body.result !== 'string' || !/^0x[0-9a-fA-F]+$/.test(body.result)) {
throw new Error('Invalid eth_chainId response.');
}
if (BigInt(body.result) !== BigInt(expected)) {
throw new Error(`Chain ID mismatch: received ${body.result}.`);
}
console.log(`PASS: endpoint returned expected chain ID ${body.result}.`);
} finally {
clearTimeout(timer);
}
}
main().catch((error) => {
// Avoid printing fetch error details that might contain a credential-bearing URL.
if (error.name === 'AbortError') console.error('FAIL: request timed out.');
else if (error instanceof TypeError) console.error('FAIL: invalid URL or network request failed.');
else console.error(`FAIL: ${error.message}`);
process.exitCode = 1;
});
NODEExpected result: a PASS line containing the returned hexadecimal chain ID, and exit status 0. The exact ID depends on the network you selected.
A mismatch means the endpoint reported a different chain; check the configured URL and expected ID before proceeding. For an HTTP or JSON-RPC error, check credentials, access restrictions, and provider documentation. A timeout or network failure warrants checking connectivity and retrying; one failure alone does not establish provider reliability.
Evaluate the workload, not just connectivity
After the identity check passes, test the actual RPC methods and data ranges your application needs, including historical blocks or logs if applicable. Record errors, rate-limit responses, and request durations over repeated runs and representative times; compare the observations with your own requirements. Check current quotas, documentation, and support terms directly with each provider.
RPC access lets your application request blockchain data or submit transactions through node methods. Indexing is a separate task: an indexer processes selected chain data into a queryable model for application-specific queries. A successful RPC check does not demonstrate that indexed queries or historical data required by your application are available.
As a next step, explore OnFinality Learn for further context, then document your own test results and selection criteria before choosing a provider.
- Test with realistic request concurrency and only within the provider's published limits.
- Avoid logging credentials or sensitive request data while collecting results.