Bittensor tutorial: verify a Substrate RPC endpoint with Node.js
Use Node.js 18+ to confirm that a Bittensor RPC endpoint responds, identify the connected chain, and read its latest block header without confusing RPC access with indexed data.
Answer first: send standard Substrate JSON-RPC requests for system_chain and chain_getHeader to an RPC URL you control or are authorised to use. This verifies live node access; it does not provide historical indexing, SQL queries, or event search. For network context, see the Bittensor network page.
Prepare an RPC URL
Set BITTENSOR_RPC_URL to the HTTPS or HTTP JSON-RPC endpoint supplied by your node operator. This guide deliberately does not embed a provider-specific endpoint: use the URL and authentication method documented for your access. Do not commit credentials or tokens to source control.
- Node.js 18 or newer is required because the example uses the built-in
fetchAPI. - The requests use standard Substrate JSON-RPC methods, so they test RPC connectivity and node responses rather than an indexing service.
export BITTENSOR_RPC_URL='https://your-authorised-rpc-endpoint.example'
node bittensor-rpc-check.mjsSave the JavaScript example below as bittensor-rpc-check.mjs, then run the command from the same shell. Replace the placeholder with a real endpoint before running it.
Run the chain and header check
The script makes two JSON-RPC calls, validates the response envelope, and prints the chain name and latest header. It fails explicitly for missing configuration, HTTP errors, malformed JSON, JSON-RPC errors, or an incomplete header instead of treating an error response as a successful check.
system_chainidentifies the chain reported by the connected node.chain_getHeaderreturns the latest header known to that node; its block number is commonly encoded as a hexadecimal string.
const url = process.env.BITTENSOR_RPC_URL;
if (!url) {
console.error('Missing BITTENSOR_RPC_URL. Set it to an authorised Substrate JSON-RPC endpoint.');
process.exit(1);
}
async function rpc(method, params = []) {
let response;
try {
response = await fetch(url, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ jsonrpc: '2.0', id: method, method, params })
});
} catch (error) {
throw new Error(`Network request failed for ${method}: ${error.message}`);
}
const text = await response.text();
if (!response.ok) {
throw new Error(`HTTP ${response.status} for ${method}: ${text.slice(0, 300)}`);
}
let body;
try {
body = JSON.parse(text);
} catch {
throw new Error(`The endpoint returned non-JSON data for ${method}`);
}
if (body.error) {
throw new Error(`JSON-RPC error for ${method}: ${body.error.message ?? JSON.stringify(body.error)}`);
}
if (!Object.prototype.hasOwnProperty.call(body, 'result')) {
throw new Error(`Invalid JSON-RPC response for ${method}: missing result`);
}
return body.result;
}
try {
const chain = await rpc('system_chain');
const header = await rpc('chain_getHeader');
if (!header || typeof header.number !== 'string' || typeof header.hash !== 'string') {
throw new Error('The node returned an incomplete chain header');
}
console.log(JSON.stringify({
chain,
latestBlockNumberHex: header.number,
latestBlockHash: header.hash
}, null, 2));
} catch (error) {
console.error(`Bittensor RPC check failed: ${error.message}`);
process.exit(1);
}Expected result: a JSON object containing the chain name, a hexadecimal latestBlockNumberHex, and a latestBlockHash. The actual values depend on the endpoint and when you run the check; this guide does not assume or fabricate them.
If the endpoint requires authentication, a 401 or 403 response means the request reached the service but was not authorised. A timeout, DNS error, or connection refusal indicates a network or endpoint configuration problem.
Know what this check does not query
An RPC node answers protocol requests such as chain identity and block headers. It is not automatically an index of every transfer, extrinsic, event, or historical relationship. For application queries over indexed data, you need an indexing pipeline and a query layer that exposes the data you have chosen to ingest.
The latest header is also not a performance benchmark or a guarantee about freshness. To measure freshness for your own use case, record the returned block number and timestamp it locally, then compare it with a trusted reference or another authorised node over repeated samples.
- Use RPC when your application needs node methods or block data and can handle the node's available retention and access policy.
- Use indexing when you need repeatable filters, historical entity queries, joins, or application-specific data models.
- Check the method and response requirements of the endpoint you actually operate; availability can vary by node configuration and access policy.
For broader concepts and implementation patterns, continue with OnFinality Learn. If you need help formulating or testing RPC requests, use the RPC Assistant.