Docs

chain_getHeader - Moonbeam RPC Method

Get block header on Moonbeam. Lightweight alternative to chain_getBlock for reading block metadata, parent hash, and state root on the cross-chain connected EVM platform on Polkadot with $65M+ TVL and 100+ projects.

Returns the block header for a given hash on Moonbeam. This is a lightweight alternative to chain_getBlock when you only need header metadata without extrinsic data.

Why Moonbeam? Build on the cross-chain connected EVM platform on Polkadot with $65M+ TVL and 100+ projects with full EVM compatibility on Polkadot, native XCM cross-chain messaging, 10K+ TPS, 24% staking APR, and $0.015 transaction costs.

When to Use This Method

chain_getHeader is ideal for cross-chain dApp developers, Polkadot builders, and teams requiring multi-chain interoperability:

  • Lightweight Queries — Get block metadata without downloading full extrinsic data on Moonbeam
  • Chain Synchronization — Track block production and monitor chain progress for cross-chain DeFi, multi-chain dApps, and Ethereum-to-Polkadot bridging via XCM, Axelar, LayerZero, and Wormhole
  • Parent Chain Navigation — Follow parentHash links to traverse the chain backwards
  • State Verification — Use stateRoot and extrinsicsRoot for Merkle proof verification

Code Examples

Common Use Cases

1. Block Time Calculator

Estimate block production rate on Moonbeam:

JavaScript
async function estimateBlockTime(api, sampleSize = 10) {
  const latestHeader = await api.rpc.chain.getHeader();
  const latestNumber = latestHeader.number.toNumber();

  const oldHash = await api.rpc.chain.getBlockHash(latestNumber - sampleSize);
  const oldHeader = await api.rpc.chain.getHeader(oldHash);

  // Use timestamp from block digests or timestamp pallet
  const latestTimestamp = await api.query.timestamp.now();
  const apiAt = await api.at(oldHash);
  const oldTimestamp = await apiAt.query.timestamp.now();

  const timeDiff = latestTimestamp.toNumber() - oldTimestamp.toNumber();
  const avgBlockTime = timeDiff / sampleSize;

  console.log(`Average block time: ${avgBlockTime / 1000}s over ${sampleSize} blocks`);
  return avgBlockTime;
}

2. Chain Traversal

Walk backwards through the Moonbeam chain using parent hashes:

JavaScript
async function walkChain(api, startHash, depth = 5) {
  let currentHash = startHash || (await api.rpc.chain.getBlockHash());
  const headers = [];

  for (let i = 0; i < depth; i++) {
    const header = await api.rpc.chain.getHeader(currentHash);
    headers.push({
      number: header.number.toNumber(),
      hash: currentHash.toString(),
      parentHash: header.parentHash.toHex()
    });
    currentHash = header.parentHash;
  }

  return headers;
}

3. Lightweight Block Monitor

Monitor Moonbeam block production without downloading full blocks:

JavaScript
async function monitorBlocks(api, callback) {
  let lastNumber = 0;

  setInterval(async () => {
    const header = await api.rpc.chain.getHeader();
    const number = header.number.toNumber();

    if (number > lastNumber) {
      console.log(`New block #${number}`);
      callback(header);
      lastNumber = number;
    }
  }, 3000);
}

Error Handling

Error CodeDescriptionSolution
-32602Invalid paramsVerify block hash is valid hex with 0x prefix
-32603Internal errorNode may be syncing — retry with backoff
-32601Method not foundVerify the node supports this RPC method
-32005Rate limit exceededImplement client-side rate limiting