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:
mkdir get-num-block-txns
cd get-num-block-txnsYou can use any HTTP library of your choosing. In this guide, we will make use of the Axios library for HTTP requests.
npm install axiosconst options = {
method: "POST",
url: "https://eth-mainnet.g.alchemy.com/v2/{your-api-key}",
headers: { accept: "application/json", "content-type": "application/json" },
data: {
id: 1,
jsonrpc: "2.0",
method: "eth_getBlockTransactionCountByNumber",
params: "finalized",
},
};
axios
.request(options)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.error(error);
});This is an axios call, where a POST request is made to the Alchemy API and the data sent contains the particular endpoint called
method: "eth_getBlockTransactionCountByNumber",And the param which specifies, returning information on the most recent finalized Block:
params: "finalized",node index.jsYou will see the following response logged to the console:
{ jsonrpc: '2.0', id: 1, result: '0xb3' }This response is in HEX. To view it in decimal, you can add a simple convert function to the code:
function hexToDec(hex) {
return parseInt(hex, 16);
}Convert the result response from Hexademical to Decimal. Target the result by updating the response in the then block.
result = response.data.result;
console.log(hexToDec(result));The result will be returned as a number in the console:
179Once you complete this tutorial, let us know how your experience was or if you have any feedback by tagging us on Twitter @Alchemy! 🎉