Skip to main content

How to get a block by 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 hash

In order to get a block by hash, Blocklens provides you a getBlockByHash endpoint to do so.

Here you'll need a parameter: block_hash.

Once you've obtained the block_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.APTOS_MAINNET.getBlockByHash("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:

{
"block_height": "53316416",
"block_hash": "0x7e687b87fe8e8d2b8af953035e6aee2b570555720d9dd1344f2dfbd0f9dae93c",
"block_time": "1970-01-01 08:00:00.000",
"timestamp": "1683416298003828",
"first_version": "136910820",
"last_version": "136910820",
"epoch": "136910820",
"round": 0,
"total_transactions": 0,
"proposer": "0x48179547b95487f77214c12e2561d53531122a91e5f79ee9be968ce8185bc444"
}

Congratulations 🥳 you just found the get a block by 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.