state_getKeys
Returns all storage keys that match a given prefix. This is useful when you need to discover dynamic child keys, such as zkSBT registries or user credential entries.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
keyPrefix | string | Yes | Hex-encoded storage key prefix (usually the module/storage hash preimage). |
at | string | No | Optional block hash to query historical state. |
Returns
Field | Type | Description |
---|---|---|
result | array | Hex-encoded full storage keys that match the prefix. |
Request Example
{
"jsonrpc": "2.0",
"method": "state_getKeys",
"params": [
"0x3a636f6465"
],
"id": 1
}
Response:
{
"jsonrpc": "2.0",
"result": [
"0x3a636f6465"
],
"id": 1
}
Generate Prefixes with @polkadot/api
import { ApiPromise, WsProvider } from '@polkadot/api';
const provider = new WsProvider('wss://api-manta-atlantic-mainnet.n.dwellir.com/YOUR_API_KEY');
const api = await ApiPromise.create({ provider });
// Example: derive prefix for zkSBT schemas storage
const prefix = api.query.zksbt.schemas.keyPrefix();
const keys = await api.rpc.state.getKeys(prefix.toHex());
console.log('Found', keys.length, 'schema keys');
Usage Notes
state_getKeys
can be expensive on large prefixes; preferstate_getKeysPaged
for pagination.- The prefix must already include any hashing strategy (e.g.,
Twox128
+Twox64Concat
). Use the metadata helpers from@polkadot/api
to avoid manual hashing. - Combine with
state_getStorage
to fetch the values behind discovered keys.