Verify an Ethereum RPC connection with Node.js
Use Node.js 18 or later to check that an Ethereum JSON-RPC endpoint responds and reports the expected chain ID.
Set your Ethereum RPC endpoint in RPC_URL, then call eth_chainId to confirm it responds and identifies the Ethereum mainnet. This checks RPC access; it does not index blockchain data or provide a queryable index.
Get an Ethereum RPC endpoint
Choose an endpoint for the Ethereum network and keep its URL private if it contains a credential. See the Ethereum network page for network information. Set the endpoint in your shell as RPC_URL before running the example.
- Use an endpoint intended for Ethereum mainnet.
- Do not commit credential-bearing endpoint URLs to source control.
Call eth_chainId
Save this as check-rpc.mjs and run it with Node.js 18 or later. The script sends a JSON-RPC request, checks the HTTP response and JSON-RPC result, then verifies that the chain ID is 0x1.
- A successful result confirms this endpoint answered this request and reported Ethereum mainnet's chain ID.
- A network error, non-success HTTP status, malformed response, or different chain ID produces a useful error. The check does not measure latency or prove that every RPC method is available.
const url = process.env.RPC_URL;
if (!url) {
console.error("Set RPC_URL to your Ethereum RPC endpoint.");
process.exit(1);
}
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: AbortSignal.timeout(10_000),
});
if (!response.ok) {
throw new Error(`HTTP ${response.status} ${response.statusText}`);
}
let body;
try {
body = await response.json();
} catch {
throw new Error("Response was not valid JSON.");
}
if (body.error) {
throw new Error(`JSON-RPC error: ${body.error.message ?? JSON.stringify(body.error)}`);
}
if (body.id !== 1 || typeof body.result !== "string") {
throw new Error("Response did not contain the expected JSON-RPC result.");
}
if (body.result.toLowerCase() !== "0x1") {
throw new Error(`Expected Ethereum mainnet chain ID 0x1; received ${body.result}.`);
}
console.log(`Ethereum mainnet RPC responded with chain ID ${body.result}.`);
} catch (error) {
console.error(`RPC check failed: ${error.message}`);
process.exit(1);
}Run it with RPC_URL='https://your-endpoint' node check-rpc.mjs, replacing the example value with your endpoint. The URL shown here is a placeholder, not a provider endpoint.
Expected result on success: Ethereum mainnet RPC responded with chain ID 0x1. The message confirms this request's result; it is not a performance measurement.
Understand what RPC access provides
Ethereum JSON-RPC lets an application request data or actions from a node. This check only calls eth_chainId. To build an application, choose and test the other RPC methods it needs as well.
Indexing and querying are different tasks. An indexer processes blockchain data into a structure designed for application queries; a successful RPC request does not create that index. Explore SubQuery Learn for indexing and query guides, or use the RPC Assistant as a next step when exploring RPC access.