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
{
"jsonrpc": "2.0",
"method": "state_getKeys",
"params": [
"0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9"
],
"id": 1
}Code Examples
Common Storage Prefixes
| Module | Storage Item | Prefix (example) |
|---|---|---|
| System | Account | 0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9 |
| Balances | TotalIssuance | 0xc2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80 |
| Staking | Validators | 0x5f3e4907f716ac89b6347d15ececedca9320c2dc4f5d7af5b320b04e2d1a3ff3 |
| Session | NextKeys | 0xcec5070d609dd3497f72bde07fc96ba0e0cdd062e6eaf24295ad4ccfc41d4609 |
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
- Account Discovery: Find all accounts with balances
- Validator Enumeration: List all validators in the network
- Storage Analysis: Analyze storage usage by module
- Migration Scripts: Iterate over storage for upgrades
- 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_getKeysPagedfor large datasets - Storage keys include both the storage prefix and the key data
Related Methods
state_getKeysPaged- Get keys with paginationstate_getStorage- Get storage valuestate_getMetadata- Get metadata to decode keys
state_call
Call a runtime API function on Kusama. Execute on-chain computations like account nonce lookups, fee estimation, and custom runtime logic without submitting a transaction.
state_getKeysPaged
Enumerate storage keys with pagination on Kusama. Iterate over storage maps like accounts, validators, and assets efficiently with cursor-based pagination.