How to Get the Number of Transactions in a Block
This guide assumes you’ve gone through the getting started steps and have an Alchemy account!
This tutorial uses the eth_getBlockTransactionCountByNumber endpoint.
Each Block in a Blockchain is assigned a block number. It is the unique sequential number for each Block. Follow the steps below to return all transactions in the current finalized Block on Ethereum:
1. From your command line, create a new project directory and cd into it:
2. Install HTTP client library
You can use any HTTP library of your choosing. In this guide, we will make use of the Axios library for HTTP requests.
3. Create a file named index.js and add the following contents:
This is an axios
call, where a POST request is made to the Alchemy API and the data
sent contains the particular endpoint called
And the param which specifies, returning information on the most recent finalized Block:
4. Run it using node
You will see the following response logged to the console:
This response is in HEX. To view it in decimal, you can add a simple convert function to the code:
5. Converting HEX to Decimal:
Convert the result
response from Hexademical to Decimal. Target the result
by updating the response in the then block.
The result will be returned as a number in the console:
Once you complete this tutorial, let us know how your experience was or if you have any feedback by tagging us on Twitter @Alchemy! 🎉