chain_getBlockHash
Description#
Returns the block hash for a given block number. Without parameters, it returns the hash of the latest block. This is useful when you store block numbers but need hashes to query storage or block data.
Parameters#
| Parameter | Type | Required | Description |
|---|---|---|---|
blockNumber | string | number | No | Hex-encoded or decimal block number. If omitted, the latest block hash is returned |
Returns#
| Field | Type | Description |
|---|---|---|
result | string | Hex-encoded block hash |
Request Example#
{
"jsonrpc": "2.0",
"method": "chain_getBlockHash",
"params": [9453714],
"id": 1
}
Response Example#
{
"jsonrpc": "2.0",
"result": "0xd4b3a0e3b88b7c25215926bbf7c733f5c31e69c0c8c4b4a86d6ec3f3f086e95d",
"id": 1
}
Code Examples#
- Python
- JavaScript
payload = {
"jsonrpc": "2.0",
"method": "chain_getBlockHash",
"params": ["0x9055f2"],
"id": 1
}
resp = requests.post(
"https://api-bifrost-polkadot.n.dwellir.com/YOUR_API_KEY",
headers={"Content-Type": "application/json"},
data=json.dumps(payload)
)
print(resp.json()["result"])
Rust#
let hash = api.rpc().block_hash(Some(9_053_714u32.into())).await?;
println!("Block hash: {}", hash.expect("hash"));
const hash = await api.rpc.chain.getBlockHash(9053714n);
console.log('Block hash:', hash.toHex());
Tips#
- Use
chain_getFinalizedHeadto obtain the latest finalized hash, then feed that hash intochain_getBlockfor deterministic data. - When interacting with storage, always pin queries to a specific block hash to avoid race conditions during runtime upgrades.