Verify a Starknet RPC Node with Node.js
Use Node.js 18+ to check that a Starknet RPC endpoint responds to chain ID and block number requests, and handle common RPC failures.
To check whether a Starknet RPC endpoint is reachable and responding, call starknet_chainId and starknet_blockNumber with JSON-RPC. This verifies RPC access; it does not index Starknet data or provide indexed queries. You need an RPC endpoint URL from your node provider. For network-specific context, see the Starknet node RPC Assistant.
Set your RPC endpoint
Keep the endpoint in an environment variable so you can switch URLs without changing the script. Set STARKNET_RPC_URL to the endpoint supplied by your provider; the example does not assume a particular endpoint URL.
- Use an endpoint for the Starknet network you intend to inspect.
- Treat any credentials embedded in an endpoint URL as secrets.
export STARKNET_RPC_URL='YOUR_STARKNET_RPC_ENDPOINT_URL'Replace the placeholder with your provider's endpoint before running the script.
Request the chain ID and current block number
Node.js 18 and later includes fetch, so no package installation is needed. The script checks HTTP status, parses the response, and reports JSON-RPC errors separately from transport or response-format errors.
- Both methods use an empty parameter array in this example.
- A successful response contains a
resultfor each method; the values depend on the endpoint and when you run the request.
const endpoint = process.env.STARKNET_RPC_URL;
if (!endpoint) {
console.error('Set STARKNET_RPC_URL to your Starknet RPC endpoint URL.');
process.exit(1);
}
async function rpc(method) {
let response;
try {
response = await fetch(endpoint, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, params: [] }),
signal: AbortSignal.timeout(15_000),
});
} catch (error) {
throw new Error(`Request failed for ${method}: ${error.message}`);
}
if (!response.ok) {
throw new Error(`${method} returned HTTP ${response.status}`);
}
let body;
try {
body = await response.json();
} catch {
throw new Error(`${method} returned a response that was not valid JSON`);
}
if (body.error) {
throw new Error(`${method} RPC error ${body.error.code}: ${body.error.message}`);
}
if (!Object.hasOwn(body, 'result')) {
throw new Error(`${method} response has no result field`);
}
return body.result;
}
try {
const [chainId, blockNumber] = await Promise.all([
rpc('starknet_chainId'),
rpc('starknet_blockNumber'),
]);
console.log(JSON.stringify({ chainId, blockNumber }, null, 2));
} catch (error) {
console.error(error.message);
process.exit(1);
}Save the code as check-starknet.mjs and run node check-starknet.mjs after setting the environment variable.
Expected result: a JSON object with chainId and blockNumber fields returned by the endpoint. Their values are live data, so they will vary. A timeout, HTTP error, malformed response, or JSON-RPC error prints a message and exits with a nonzero status.
Know what this check proves
A successful response shows that this endpoint answered these two RPC methods at the time of the request. To compare behavior over time or across endpoints, record the response and request time yourself; this example makes no performance or availability claim.
RPC access lets an application request data or submit supported RPC calls. Indexing is a separate process that collects and organizes blockchain data for queries. If your task requires indexed data, explore SubQuery Learn guides for indexing and querying concepts. For more Starknet node setup context, continue with the RPC Assistant.
- Use RPC methods for direct node requests.
- Use an indexer when your application needs data organized for repeated or application-specific queries.
{
"chainId": "value returned by starknet_chainId",
"blockNumber": "value returned by starknet_blockNumber"
}The example shows the response shape only; it does not predict chain ID or block number values.