Fetch all receipts for an Ethereum block with eth_getBlockReceipts

Use Node.js to request a block's transaction receipts in one JSON-RPC call and handle unsupported methods, missing blocks, and HTTP errors.

To fetch the transaction receipts for an Ethereum block, call eth_getBlockReceipts with a hexadecimal block number and read the JSON-RPC result array. This gives you raw RPC data for that block; it does not create an index or provide a queryable transaction history.

Prepare a block number and RPC endpoint

Use Node.js 18 or later and an Ethereum JSON-RPC endpoint that supports eth_getBlockReceipts. Method availability can vary by endpoint, so test the endpoint you intend to use.

Choose a known block number. The script converts a decimal number to the hexadecimal quantity expected by JSON-RPC. Keep endpoint credentials private, and avoid committing them to source control.

  • Set RPC_URL to your endpoint URL.
  • Set BLOCK_NUMBER to a non-negative decimal block number.

Request and inspect the receipts

Save the following as block-receipts.js, set the two environment variables, and run node block-receipts.js. The script checks HTTP and JSON-RPC errors separately and treats a null result as an unavailable block.

JavaScript · Node.js
const rpcUrl = process.env.RPC_URL;
const blockNumber = process.env.BLOCK_NUMBER;

async function main() {
  if (!rpcUrl || !blockNumber || !/^\d+$/.test(blockNumber)) {
    throw new Error('Set RPC_URL and a non-negative decimal BLOCK_NUMBER.');
  }

  const blockTag = '0x' + BigInt(blockNumber).toString(16);
  const response = await fetch(rpcUrl, {
    method: 'POST',
    headers: { 'content-type': 'application/json' },
    body: JSON.stringify({
      jsonrpc: '2.0',
      id: 1,
      method: 'eth_getBlockReceipts',
      params: [blockTag]
    }),
    signal: AbortSignal.timeout(30000)
  });

  if (!response.ok) {
    throw new Error(`HTTP ${response.status} ${response.statusText}`);
  }

  const body = await response.json();
  if (body.error) {
    throw new Error(`JSON-RPC error ${body.error.code}: ${body.error.message}`);
  }
  if (body.result === null) {
    throw new Error('Block not found or not available at this endpoint.');
  }
  if (!Array.isArray(body.result)) {
    throw new Error('Unexpected JSON-RPC response: result is not an array.');
  }

  console.log(`Block ${blockTag}: ${body.result.length} receipt(s)`);
  console.log(JSON.stringify(body.result, null, 2));
}

main().catch((error) => {
  console.error(`Receipt request failed: ${error.message}`);
  process.exitCode = 1;
});

Expected result: the script prints the block tag, the number of receipts, and a JSON array. Each receipt normally includes fields such as transactionHash, blockNumber, and logs; an empty array is possible for a block with no transactions.

If the endpoint returns a method-not-found error, check its documentation or try an endpoint that explicitly supports the method. For HTTP authorization or rate-limit errors, check your credentials and provider guidance. A timeout or malformed response is reported as a failure rather than mistaken for an empty block.

Verify the data before building a history

For a consistency check, compare the returned receipt count and transaction hashes with the transactions returned by eth_getBlockByNumber for the same block. If you process recent blocks, account for chain reorganizations: record block hashes and define when your application considers data sufficiently final.

This call retrieves receipts for one block at a time. If you need to filter events across many blocks or serve repeatable application queries, ingest and index the relevant data separately; an RPC response alone is not an index. To compare approaches, measure elapsed time and request counts against your own endpoint and workload rather than assuming a speedup.

For help investigating an RPC request, try the OnFinality RPC Assistant. For related background, browse OnFinality Learn.