chain_getBlockHash
Description
Retrieves the block hash for a specific block number. When called without parameters, returns the hash of the latest block. This method is commonly used to get block identifiers for historical queries.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
blockNumber | number | No | Block number to get hash for. If omitted, returns latest block hash |
Returns
Field | Type | Description |
---|---|---|
result | string | Hex-encoded block hash |
Request Example
{
"jsonrpc": "2.0",
"method": "chain_getBlockHash",
"params": [1000000],
"id": 1
}
Response Example
{
"jsonrpc": "2.0",
"result": "0x5d83e66b61701da7b6e3e8c9d8e2e57e1391e3e58e677b30e1f391d568c9ca24",
"id": 1
}
Code Examples
JavaScript
const getBlockHash = async (blockNumber = null) => {
const params = blockNumber !== null ? [blockNumber] : [];
const response = await fetch('https://api-polkadot.n.dwellir.com', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'chain_getBlockHash',
params: params,
id: 1
})
});
const data = await response.json();
return data.result;
};
// Get latest block hash
const latestHash = await getBlockHash();
console.log('Latest block hash:', latestHash);
// Get specific block hash
const historicalHash = await getBlockHash(1000000);
console.log('Block 1000000 hash:', historicalHash);
Python
import requests
import json
def get_block_hash(block_number=None):
url = "https://api-polkadot.n.dwellir.com"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
params = [block_number] if block_number is not None else []
payload = {
"jsonrpc": "2.0",
"method": "chain_getBlockHash",
"params": params,
"id": 1
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
return response.json()["result"]
# Get genesis block hash
genesis_hash = get_block_hash(0)
print(f"Genesis block: {genesis_hash}")
Related Methods
chain_getBlock
- Get full block data using hashstate_getStorage
- Query state at blocksystem_health
- Check node status