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.APTOS_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.eyJpZCI6OTc5MjI4NDQsInBhZ2UiOjEsImZyb21fdGltZSI6IjE2OTYwNDgyNTkiLCJ0b190aW1lIjoiMTY5NjA0ODI1OSIsImlhdCI6MTY5NzA1Njg3NH0.OHeNqDa3QYL4VM-kdtRUnP5Nj7S4_xhNzKmLI_QpWS8",
"page": 1,
"limit": 20,
"total_items": 1,
"data": [
{
"block_height": 97922847,
"block_hash": "0x5cbc98d7ca9a0493bf14606b1adfc4f2e60b995dddafb372b93ba1b330dfa73f",
"timestamp": 1696048259,
"first_version": 279982269,
"last_version": 279982272,
"epoch": 4241,
"round": 11381,
"total_transactions": 4,
"proposer": "0x48179547b95487f77214c12e2561d53531122a91e5f79ee9be968ce8185bc444"
}
]
}

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.