How to get a block by block number or hash
Prerequisites
Before getting started, make sure you have the following ready:
- Node v.14+
- NPM
Step 1: Setup Blocklens
First register your Blocklens account and get your Blocklens API Key.
Once you have your Blocklens API Key, install the Blocklens SDK in your project.
npm i blocklens-sdk
Step 2: Get a block by block number or hash
In order to get a block by block number or hash, Blocklens provides you a getBlockByNumberOrHash endpoint to do so.
Here you'll need a parameter: block_number_or_hash
.
Once you've obtained the block_number_or_hash
, you can copy the following code:
import Blocklens from "blocklens-sdk";
const blocklens = new Blocklens();
blocklens.init({ apiKey: "YOUR_API_KEY" });
const runApis = async () => {
const response = await blocklens.ETH_MAINNET.getBlockByNumberOrHash("0x0517e1369f5159852973c47dc335d28b67ccffb238333542f880abc3fe3ab505");
console.log(response);
};
runApis();
Step 3: Run the script
ts-node index.ts
In your terminal, you should see the following JSON response with the data: In your terminal, you should see the following JSON response with the data blocks:
{
"size": 704482,
"difficulty": "0",
"total_difficulty": "58750003716598352816469",
"block": 18246150,
"hash": "0x0517e1369f5159852973c47dc335d28b67ccffb238333542f880abc3fe3ab505",
"parent_hash": "0x20c0e16d65670a4d125be0bafa2af3af3228e5c6cd8f428328c1db2d1ac11a07",
"nonce": "0x0000000000000000",
"sha3_uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"logs_bloom": "0x153f90b26112945c51081064b059127959a35e8d1d028d03162ba5387f30463642ea8bc9036986f8497312ed6180216c2fe3817d9f10fb619035464b112c7f86f124300c45f05d3ae8835a0b694470f5c3130688e8676e5aa1d8dda7c822132011e220591b028681aa879534883c1c5d9bbc446a2608961b980900d6985b584047208f730a0058a34f66116f3723097a0e13322b89698d1c4a6768645190d95833aad64a029a68334ad269e069130582874b6c12441585d82c60100934447be51dc0c1b286144819491aab38546fdd0441590408d621381f07a2790a2d34f9fc04342766448294b423de48e96c3021058475b0586bb4a1440023b94dc020dc",
"transactions_root": "0x0e3eb05a48d37d785f82cad0d6e48ddb644fd36ccdcfbd104534f78c9d90f3a1",
"state_root": "0x25ee359d366b9879f24ff21c303811e737a19b75a81383d672764af42aa4305c",
"receipts_root": "0xf339ad71a8d26c5357699faf05c83e76012335d4850d43c1061822619654971d",
"miner": "0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5",
"extra_data": "0x6265617665726275696c642e6f7267",
"gas_limit": "29999972",
"gas_used": "23291896",
"timestamp": 1696048259,
"base_fee_per_gas": "6374499230",
"transaction_count": 10,
"transactions": [
{
"hash": "0xaf414aea31bc3b02edaff971e4e0e70d83cfc99728f4b74154715cd2c2e8d5e7",
"from": "0xC1b634853Cb333D3aD8663715b08f41A3Aec47cc",
"to": "0x1c479675ad559DC151F6Ec7ed3FbF8ceE79582B6",
"receipt_contract_address": null,
"receipt_cumulative_gas_used": "20709888",
"receipt_gas_used": "1835946",
"input": "0x8f111f3c000000000000000000000000000000000000000000000000000000000005714900000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000010cec4000000000000000000000000e64a54e2533fd126c2e452c5fab544d80e2e4eb50000000000000000000000000000000000000000000000000000000006c8354e0000000000000000000000000000000000000000000000000000000006c8366100000000000000000000000000000000000000000000000000000000000181a9005b22733512614349b30d2000a2a811a455e2472323d838800243",
"value": "0",
"nonce": "305467",
"gas_price": "6424499230",
"gas": "1884460",
"timestamp": 1696048259,
"block_hash": "0x0517e1369f5159852973c47dc335d28b67ccffb238333542f880abc3fe3ab505",
"block_number": 18246150,
"tx_index": 117,
"status": true,
"effective_gas_price": "6424499230",
"logs_bloom": "0x0400000000000000000010000000000040000000000000000000000050000000000000000000040000000000000000000100000012000000100000001004000000001000000000000000000000000000000000000004001000000000000000000000000000000000000000000000000000000400000000000000000008004000000000000000000000000000200000000800100000000000000200000000000000000002000000000000000001000000004000100000008000000000000000000000000000000000401000000000400000000400000000000400100008005020000000000000000000000000200000000000000008208000000020000000c0",
"logs": []
}
]
}
Congratulations 🥳 you just found the get a block by block number or hash on multiple chains with only a few lines of code using the Blocklens Wallet API!
API Reference
If you want to know more details on the endpoint and optional parameters, check out:
Support
If you face any trouble following the tutorial, feel free to reach out to our community engineers in our Discord or Forum to get 24/7 developer support.