How to get blocks from number to number
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 blocks from number to number
In order to get blocks from number to number, Blocklens provides you a getBlockToBlock endpoint to do so.
Here you'll need parameters: from_block
, to_block
.
Once you've obtained all the from_block
, to_block
, 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.getBlockToBlock({"from_block":18246150,"to_block":18246151});
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:
{
"cursor": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTgyNDYxNTAsInBhZ2UiOjEsImZyb21fYmxvY2siOjE4MjQ2MTUwLCJ0b19ibG9jayI6MTgyNDYxNTEsImlhdCI6MTY5NzA4MzkwM30.clHFv27Un_-0pI8hj-mXu94SYPiDstE5pAXt8OQNVgk",
"page": 1,
"limit": 20,
"total_items": 1,
"data": [
{
"size": 214064,
"difficulty": "0",
"total_difficulty": "58750003716598352816469",
"block": 18246151,
"hash": "0x28f7522cc52594826d9853bf25d65ebb382650320ad791cd5434d955a9110d82",
"parent_hash": "0x0517e1369f5159852973c47dc335d28b67ccffb238333542f880abc3fe3ab505",
"nonce": "0x0000000000000000",
"sha3_uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"logs_bloom": "0x4baf089161bd074cd0445ca488543c037036492eff80bd8a198f8136ef1115166e623b49bc4443aa9f1118000ca019dc0ba1e022de8db8c1052105c8af3eb3cb4e004b3adcb4be2b688bcb1ff050ecb48530404c78441b0c48b4ee079a2893818f7e2c25f650028fc914d5d730580add5a111de3696c97921a4db49b181cd9707dd4837f3ce6f10be5e81fbe83208a5c95903069a1082098402138d4e41fc16f430ad76aa13eea42eb8aecf15d92962046e36c8bcae2c4483ce49043251f412759f2523bb2762a7d0942bc023746204e02e130420d3d313fc80db61f6001e20b043137c8c8421be7bac458a0ea81885192b193987b1000c5c9493f0b49207f",
"transactions_root": "0xb6d921bca11dae5054d5e8a5ccf8c146929b7128b5bf1a2ba11464940e3ef120",
"state_root": "0xad655fb1d6d56687e6f91b0ba51b5cef766bb25fc17c492878811436f0d17208",
"receipts_root": "0x47e167928bfa4800b06b51f8cca6564e9d9b9a31c86500dca7ad064246af186e",
"miner": "0x2990555A8139B4b4bbF8ebCBCF9EcCD763727088",
"extra_data": "0x506f776572656420627920626c6f58726f757465",
"gas_limit": "30000000",
"gas_used": "16532443",
"timestamp": 1696048283,
"base_fee_per_gas": "6814972757"
}
]
}
Congratulations 🥳 you just found the get blocks from number to number 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.