Check Solana RPC access from a Node.js project
Send a JSON-RPC request to your Solana endpoint from Node.js 18+, validate the response, and distinguish RPC access from indexing.
To check whether your Solana project can reach its RPC endpoint, call the getHealth JSON-RPC method and inspect the response. Set SOLANA_RPC_URL to the endpoint provided for your project; this guide does not assume or invent an endpoint URL. For network details, see OnFinality’s Solana network page.
Set the RPC endpoint
An RPC endpoint lets an application send requests to a Solana node. Put your project’s endpoint in an environment variable so it does not need to be embedded in source code.
- Use the endpoint assigned to your project and keep any credentials in your environment or secret manager.
- This example requires Node.js 18 or later, which includes the
fetchAPI.
export SOLANA_RPC_URL='PASTE_YOUR_SOLANA_RPC_ENDPOINT_HERE'
node --versionReplace the placeholder with your actual endpoint before running the request.
Call `getHealth` and handle errors
The script sends one JSON-RPC request. It reports HTTP errors, JSON-RPC errors, invalid responses, and request timeouts separately so you can identify common connection problems.
const endpoint = process.env.SOLANA_RPC_URL;
if (!endpoint) {
console.error('Set SOLANA_RPC_URL to your Solana RPC endpoint.');
process.exit(1);
}
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 10_000);
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'getHealth',
}),
signal: controller.signal,
});
if (!response.ok) {
throw new Error(`HTTP ${response.status} ${response.statusText}`);
}
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 || !('result' in body)) {
throw new Error('Unexpected JSON-RPC response shape');
}
console.log('RPC health result:', body.result);
} catch (error) {
if (error.name === 'AbortError') {
console.error('Request timed out after 10 seconds. Check endpoint reachability and try again.');
} else {
console.error(`RPC request failed: ${error.message}`);
}
process.exitCode = 1;
} finally {
clearTimeout(timeout);
}Run it with node --input-type=module followed by the script, or save the code in an .mjs file and run node your-file.mjs.
A healthy endpoint typically returns a JSON-RPC result of "ok". The exact outcome comes from your endpoint; an HTTP error or JSON-RPC error gives a failure to investigate, not a performance measurement.
Know what this check establishes
A successful health response shows that this request reached an RPC endpoint and received a valid response. It does not index the chain, store a project-specific history, or provide a query layer for application data. Those tasks require an indexing and querying workflow.
For a next step, browse OnFinality Learn or review the RPC Assistant to explore RPC options. Measure latency or availability from your own application environment if you need those figures; this single health check does not establish them.