How to get transaction by block number or 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 transaction by block number or hash
In order to get transaction by block number or hash, Blocklens provides you a getTransactionByBlockNumberOrHash endpoint to do so.
Here you'll need a parameter: block_number_or_hash
.
Once you've obtained the block_number_or_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.ETH_MAINNET.getTransactionByBlockNumberOrHash("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:
[
{
"hash": "0x758e89d2f9544b804b888452105f634807c79c8bd550aea05a7116901d17cb1f",
"from": "0xc0525dFF4C0956301D4242908263eC1012a6F405",
"to": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
"receipt_contract_address": null,
"receipt_cumulative_gas_used": "1691751",
"receipt_gas_used": "142094",
"input": "0xb6f9de9500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000c0525dff4c0956301d4242908263ec1012a6f405000000000000000000000000000000000000000000000000000000006517a4f90000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000f4a3072e68f23ee4a815f3df7b16b1b7a79117f9",
"value": "50000000000000000",
"nonce": "732",
"gas_price": "21374499230",
"gas": "250455",
"timestamp": 1696048259,
"block_hash": "0x0517e1369f5159852973c47dc335d28b67ccffb238333542f880abc3fe3ab505",
"block_number": 18246150,
"tx_index": 8,
"status": true,
"effective_gas_price": "21374499230",
"logs_bloom": "0x002000000000000000000000800000000000000400000000000100000000000000000000000000080800000000000000020000000800000000000400000000000000000000800000000000080000402000000000000000020000000080000000000000000000000000000000000000000000000000000000000000101000000000000000000000000040000000000100001000010000000800000040000000000000800000000000000008000000000000000000000000002000000000000000000000020000000000020000000000000000000000000010000000000000200000002000000000000000000000000000000000000000004000000000000000",
"logs": [[{}], [{}], [{}]],
"internal_transactions": []
}
]
Congratulations 🥳 you just found the get transaction by block number or 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.