Docs

state_getKeys - JSON-RPC Method

Get storage keys matching a prefix in Kusama via JSON-RPC. Query multiple storage entries efficiently.

Description

Returns storage keys that match a given prefix. This JSON-RPC method is useful for discovering all storage entries under a specific module or querying multiple related storage items. Be cautious with broad prefixes as they may return large result sets.

Request Example

JSON
{
  "jsonrpc": "2.0",
  "method": "state_getKeys",
  "params": [
    "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9"
  ],
  "id": 1
}

Code Examples

Common Storage Prefixes

ModuleStorage ItemPrefix (example)
SystemAccount0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9
BalancesTotalIssuance0xc2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80
StakingValidators0x5f3e4907f716ac89b6347d15ececedca9320c2dc4f5d7af5b320b04e2d1a3ff3
SessionNextKeys0xcec5070d609dd3497f72bde07fc96ba0e0cdd062e6eaf24295ad4ccfc41d4609

Batch Query Example

JavaScript
// Efficiently query multiple storage values
async function batchQueryStorage(api, keys) {
  // Get all values in a single call
  const values = await api.rpc.state.queryStorageAt(keys);
  
  const results = {};
  keys.forEach((key, index) => {
    results[key.toString()] = values[index];
  });
  
  return results;
}

// Example usage
const keys = await getStorageKeys(accountPrefix);
const values = await batchQueryStorage(api, keys.slice(0, 10));
console.log('Batch query results:', values);

Use Cases

  1. Account Discovery: Find all accounts with balances
  2. Validator Enumeration: List all validators in the network
  3. Storage Analysis: Analyze storage usage by module
  4. Migration Scripts: Iterate over storage for upgrades
  5. Indexing: Build indexes of on-chain data

Notes

  • Large prefixes may return many keys - use pagination when available
  • Keys are returned in lexicographical order
  • The prefix must be hex-encoded
  • Consider using state_getKeysPaged for large datasets
  • Storage keys include both the storage prefix and the key data