⚠️Blast API (blastapi.io) ends Oct 31. Migrate to Dwellir and skip Alchemy's expensive compute units.
Switch Today →
Skip to main content

state_getKeys - JSON-RPC Method

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.

Parameters

ParameterTypeRequiredDescription
prefixstringYesHex-encoded storage key prefix to match
blockHashstringNoBlock hash to query at. If omitted, uses the latest block

Returns

FieldTypeDescription
resultarrayArray of hex-encoded storage keys matching the prefix

Request Example

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

Response Example

{
"jsonrpc": "2.0",
"result": [
"0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9de1e86a9a8c739864cf3cc5ec2bea59fd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d",
"0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da94f9aea1afa791265fae359272badc1cf8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48"
],
"id": 1
}

Code Examples

import requests
import json
from substrateinterface import SubstrateInterface

def get_storage_keys(prefix, block_hash=None):
url = "https://api-moonriver.n.dwellir.com/YOUR_API_KEY"
headers = {
"Content-Type": "application/json"
}

params = [prefix, block_hash] if block_hash else [prefix]

payload = {
"jsonrpc": "2.0",
"method": "state_getKeys",
"params": params,
"id": 1
}

response = requests.post(url, headers=headers, data=json.dumps(payload))
return response.json()["result"]

# Example: Get all validator preferences keys
def get_validator_keys():
# Staking.Validators storage prefix
prefix = "0x5f3e4907f716ac89b6347d15ececedca9320c2dc4f5d7af5b320b04e2d1a3ff3"
keys = get_storage_keys(prefix)

print(f"Found {len(keys)} validator preference entries")

for key in keys:
# Extract validator account from key
validator_account = key[-64:]
print(f"Validator: 0x{validator_account}")

return keys

# Example: Query all keys under a module
def get_module_keys(module_prefix):
keys = get_storage_keys(module_prefix)

# Group keys by storage item
storage_items = {}
for key in keys:
# Storage keys typically have a fixed prefix per item
item_prefix = key[:66] # First 33 bytes (66 hex chars)
if item_prefix not in storage_items:
storage_items[item_prefix] = []
storage_items[item_prefix].append(key)

return storage_items

Common Storage Prefixes

ModuleStorage ItemPrefix (example)
SystemAccount0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9
BalancesTotalIssuance0xc2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80
StakingValidators0x5f3e4907f716ac89b6347d15ececedca9320c2dc4f5d7af5b320b04e2d1a3ff3
SessionNextKeys0xcec5070d609dd3497f72bde07fc96ba0e0cdd062e6eaf24295ad4ccfc41d4609

Batch Query Example

// 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