Inspect Bittensor Metagraph Storage Through a Subtensor RPC
Use a Bittensor Subtensor RPC endpoint and runtime metadata to discover metagraph-related storage before writing a reader or indexer.
The safest first step is to verify RPC access and inspect the connected runtime rather than assume a metagraph method or storage layout. This Node.js 18+ example connects to the Bittensor network endpoint you provide, prints the chain and runtime version, and lists storage entries whose names suggest metagraph data. Start with the Bittensor network page when choosing an RPC provider or endpoint.
Prepare an RPC connection
An RPC endpoint gives your application access to live chain methods and runtime metadata. It does not by itself provide historical, relational queries over every metagraph change; that is an indexing problem and usually requires collecting and storing the relevant events or state over time.
Install the Polkadot API client, then set SUBTENSOR_RPC_URL to a WebSocket RPC URL supplied by your chosen provider. Do not hard-code an endpoint into application code or assume that every network exposes the same custom methods.
- Use a WebSocket URL accepted by @polkadot/api.
- Keep credentials out of source control if the endpoint requires them.
- The runtime metadata, not a copied example, is the authority for the current storage names and types.
// Save as inspect-metagraph.mjs
// Install first: npm install @polkadot/api
import { ApiPromise, WsProvider } from '@polkadot/api';
const endpoint = process.env.SUBTENSOR_RPC_URL;
if (!endpoint) {
console.error('Missing SUBTENSOR_RPC_URL. Set it to your Bittensor WebSocket RPC URL.');
process.exit(2);
}
let api;
try {
const provider = new WsProvider(endpoint);
api = await ApiPromise.create({ provider });
await api.isReady;
const runtime = await api.rpc.state.getRuntimeVersion();
const matches = [];
for (const pallet of api.runtimeMetadata.asLatest.pallets) {
const palletName = pallet.name.toString();
if (!pallet.storage.isSome) continue;
for (const item of pallet.storage.unwrap().items) {
const storageName = item.name.toString();
if (/meta|weight|neuron|uid/i.test(`${palletName}.${storageName}`)) {
matches.push(`${palletName}.${storageName}`);
}
}
}
console.log(JSON.stringify({
chain: (await api.rpc.system.chain()).toString(),
specVersion: runtime.specVersion.toString(),
matchingStorageEntries: matches
}, null, 2));
} catch (error) {
console.error(`RPC or metadata inspection failed: ${error.message}`);
console.error('Check the URL, network access, credentials, and endpoint protocol.');
process.exitCode = 1;
} finally {
if (api) await api.disconnect();
}Run it with SUBTENSOR_RPC_URL='wss://your-endpoint' node inspect-metagraph.mjs, replacing the value with an endpoint you control or have permission to use.
A successful result is a JSON object with chain, specVersion, and matchingStorageEntries fields. The entry names and values depend on the live runtime; an empty match list is a useful signal that you must inspect metadata more broadly rather than infer a storage schema.
If the command fails before printing JSON, treat that as an RPC connectivity or compatibility problem, not as evidence that the metagraph is empty.
Turn metadata into a safe read
The discovery output tells you which pallet and storage names exist on the connected runtime. Before reading a value, inspect the corresponding metadata type and determine whether it is a plain value, a map, or a double map; the key shape matters when calling api.query.
For example, a storage item may require a hotkey, netuid, UID, or another runtime-specific key. Do not copy a call from another Bittensor runtime without checking the metadata and current chain behavior.
- Use
api.query.<Pallet>.<Storage>(key)only after confirming the generated API name and key type. - Decode compact or SCALE-encoded values through the API rather than treating raw bytes as application data.
- Record the block hash or block number with a read when reproducibility matters; a live RPC read can change between requests.
// Add this after the metadata discovery in inspect-metagraph.mjs
// Replace PALLET and STORAGE only after confirming their names in the output.
// const value = await api.query.PALLET.STORAGE(KEY);
// console.log(value.toHuman());The commented call is intentionally not populated with a fabricated pallet, storage item, or key. Bittensor runtime upgrades can change those details, so use the metadata from your connected endpoint as the source of truth.
If you need a repeatable historical view of weights, emissions, or neuron membership, capture the relevant state or events at known blocks and persist them in an index rather than repeatedly querying only the latest state.
Know when RPC is not enough
RPC is appropriate for on-demand reads, runtime metadata, and chain state at or near the current head. An indexer is appropriate when an application needs searchable history, joins across entities, pagination designed for a product UI, or reliable reconstruction of changes over many blocks.
A metagraph dashboard commonly needs more than one current storage read: it may combine subnet identifiers, neuron identities, weights, activity, and emissions. Design those reads around the actual runtime schema and define how missing, pruned, or changing data should be handled.
- Measure request latency, error rate, and response size from your own deployment if they affect the application; do not assume a provider or endpoint has a particular performance profile.
- Cache immutable block-level results where suitable, but do not present cached data as the current metagraph without recording its block context.
- Use retries with backoff for transient transport failures, while avoiding blind retries for invalid keys or rejected RPC methods.
{
"readContext": {
"endpoint": "configured outside source code",
"blockHash": "capture from the read when historical reproducibility is required"
},
"dataPath": {
"rpc": "on-demand chain reads and metadata",
"indexer": "persisted historical state and application queries"
}
}This distinction prevents a common design error: treating successful RPC access as if it were already a queryable metagraph database.
Validate the final data model against the live runtime and test failure cases such as unavailable endpoints, changed metadata, missing keys, and stale block references.
Continue with network-specific tooling
Once the metadata inspection succeeds, document the exact storage items and keys your application needs, then add narrowly scoped reads or an indexing pipeline. Keep the endpoint configurable so the same code can be tested against another permitted RPC connection.
For broader SubQuery implementation guidance, continue through OnFinality Learn. If you need help reasoning about RPC requests interactively, use the OnFinality RPC Assistant as a next step, then verify any generated request against the runtime metadata you actually connected to.
- Treat generated examples as starting points, not as proof that a method exists on the current Bittensor runtime.
- Keep the chain, runtime version, block context, and schema assumptions alongside indexed records.
- Promote a read into an indexer only when your product requires history or query patterns that direct RPC calls cannot provide.
SUBTENSOR_RPC_URL='wss://your-endpoint' node inspect-metagraph.mjsReplace the placeholder with a real, authorized endpoint. The command should either print runtime-derived JSON or return a non-zero exit code with an actionable error.