How to get blocks
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
In order to get blocks, Blocklens provides you a getBlocks endpoint to do so.
import Blocklens from "blocklens-sdk";
const blocklens = new Blocklens();
blocklens.init({ apiKey: "YOUR_API_KEY" });
const runApis = async () => {
const response = await blocklens.ETH_MAINNET.getBlocks();
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.eyJpZCI6MTgyNjY5NjksInBhZ2UiOjEsImlhdCI6MTY5NzA2MTU0M30.x3sYiqi5TUyZ2OMy8JUt2uTxp908srOMb0c--iNlHrw",
"page": 1,
"limit": 20,
"total_items": 1,
"data": [
{
"size": 42388,
"difficulty": "0",
"total_difficulty": "58750003716598352816469",
"block": 18266988,
"hash": "0x7fa3d2c64c15a89b9a6c7a4261d6a25e7f3ba2e6992184a80f73a2ade5f61ff0",
"parent_hash": "0xb85fa9dc8e994d27c51afef86857b1d6270388f6209990dcf6e6686d9ab5ff8a",
"nonce": "0x0000000000000000",
"sha3_uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"logs_bloom": "0x60a904018910800850207960a4031af100381800681012084029800046900c8132710158413123c1fa991a284011192c0f210002a906ad480544c008216ce183641a184d4c04af2c6b93620d866100250382404600600a8001104d84ac7200813e28082eba00d1e584159080288419f10218a7688a8c1710a24705703089000c0a021254da34b218046a504200004096d840000bb342120c60a023c202336c029a2001c4310462b8e8ba52f720520ea4241d620b452992a015b4494d240a08125004809a0251040024182e240c30a6891220415c023f6494420d00026a9120000934f00849681896400572a10b001942340891120d5062c2002918a11a001c",
"transactions_root": "0xf7aa5a3d3140611897998f08c8c9b3a206ce0a1bf480350c4b1bfc355394770f",
"state_root": "0xf791785d2ad1412cc4ba458523d5b12fd73d1613c20ddc1439e31fa42b37e2ea",
"receipts_root": "0x615d62be0ca346cf5bd185f2c82f86910f0e97353328aefd82039f6e75ca384f",
"miner": "0x388C818CA8B9251b393131C08a736A67ccB19297",
"extra_data": "0xd883010c00846765746888676f312e32302e33856c696e7578",
"gas_limit": "30000000",
"gas_used": "7758076",
"timestamp": 1696299599,
"base_fee_per_gas": "6633207173"
}
]
}
Congratulations 🥳 you just found the get blocks 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.