Check RPC access before querying Bittensor subnet 123

Use Node.js to verify a Bittensor RPC endpoint and retrieve a finalized block hash before building a subnet 123 query.

Before querying Bittensor subnet 123, verify that your configured RPC endpoint responds and returns a finalized block hash. The Node.js script below performs that connectivity check; it does not verify that subnet 123 exists or retrieve its data.

Configure a Bittensor RPC endpoint

Use Node.js 18 or later and set BITTENSOR_RPC_URL to an HTTP or HTTPS JSON-RPC endpoint for the Bittensor network you intend to query. Obtain your endpoint and any required credentials from your RPC provider; the OnFinality Bittensor network page is one place to start.

Keep credential-bearing URLs out of source control. This check sends system_chain and chain_getFinalizedHead requests; make sure your provider permits those methods.

  • Confirm whether your endpoint targets the network you intend to inspect.
  • If your provider requires authentication outside the URL, adapt the request headers to its instructions.

Request the chain name and finalized head

Save the following as check-bittensor-rpc.js, set BITTENSOR_RPC_URL in your shell, and run node check-bittensor-rpc.js. No packages are required.

    JavaScript · Node.js
    const url = process.env.BITTENSOR_RPC_URL;
    
    async function rpc(method, id) {
      const response = await fetch(url, {
        method: 'POST',
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({ jsonrpc: '2.0', id, method, params: [] }),
        signal: AbortSignal.timeout(10000)
      });
    
      if (!response.ok) {
        throw new Error(`${method}: HTTP ${response.status}`);
      }
    
      const body = await response.json();
      if (body.error) {
        throw new Error(`${method}: RPC error ${body.error.code}: ${body.error.message}`);
      }
      if (body.jsonrpc !== '2.0' || body.id !== id || !Object.hasOwn(body, 'result')) {
        throw new Error(`${method}: invalid JSON-RPC response`);
      }
      return body.result;
    }
    
    async function main() {
      if (!url) throw new Error('Set BITTENSOR_RPC_URL to your HTTP(S) RPC endpoint');
      const parsed = new URL(url);
      if (!['http:', 'https:'].includes(parsed.protocol)) {
        throw new Error('BITTENSOR_RPC_URL must use HTTP or HTTPS');
      }
    
      const chain = await rpc('system_chain', 1);
      const finalizedHead = await rpc('chain_getFinalizedHead', 2);
      if (typeof chain !== 'string' || !chain) throw new Error('Invalid chain name');
      if (typeof finalizedHead !== 'string' || !/^0x[0-9a-fA-F]{64}$/.test(finalizedHead)) {
        throw new Error('Invalid finalized block hash');
      }
      console.log(`Chain: ${chain}`);
      console.log(`Finalized block hash: ${finalizedHead}`);
    }
    
    main().catch((error) => {
      console.error(`RPC check failed: ${error.message}`);
      process.exitCode = 1;
    });

    A successful run prints a chain name and a 0x-prefixed finalized block hash. Compare the chain name with your intended network; a successful response alone does not prove that you selected the right one.

    If the request fails, check the URL, credentials, network access, and provider method permissions. An HTTP error indicates that the server rejected or could not process the request; a JSON-RPC error indicates that the server returned a method-level error. A timeout means no response arrived within the script's 10-second limit.

    Avoid sharing diagnostic output containing credentials. If you need latency data, time repeated requests from your own deployment environment rather than treating this connectivity check as a benchmark.

    Move from RPC access to subnet data

    RPC access lets an application request chain information, but the check above neither resolves subnet 123 nor indexes its history. To inspect that subnet, use current Bittensor documentation or runtime metadata to identify the applicable storage entries, parameter encoding, and network-specific subnet identifier before making a query.

    For recurring historical queries, decide which chain data to ingest and how to index it; an RPC response alone is not an indexed query service. As a next step, consult the OnFinality RPC Assistant when exploring RPC methods, and verify any suggested method against the target chain and endpoint.

    • If you need a consistent snapshot, retain the finalized block hash and use it in subsequent calls where the method supports a block parameter.
    • If you need historical state, confirm that your endpoint can serve the block and state required by your application.