Skip to main content

How to get blocks from time to time

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 time to time

In order to get blocks from time to time, Blocklens provides you a getBlocksFromTimeToTime endpoint to do so.

Here you'll need parameters: from_time, to_time.

Once you've obtained all the from_time, to_time, 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.getBlocksFromTimeToTime({"from_time":"1696048259","to_time":"1696048260"});
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.eyJpZCI6MTgyNDYxNTAsInBhZ2UiOjEsImZyb21fdGltZSI6IjE2OTYwNDgyNTkiLCJ0b190aW1lIjoiMTY5NjA0ODI2MCIsImlhdCI6MTY5NzA2MTUxOH0.PId-peHroLpI6R6rNDqXbV9p4NJbUCI4MeYx7ZP_ddY",
"page": 1,
"limit": 20,
"total_items": 1,
"data": [
{
"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"
}
]
}

Congratulations 🥳 you just found the get blocks from time to time 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.