Returns the block number of the most recently mined block.
Don’t have an API key?
Start using this method in your app today.
Description
Returns the block number of the most recently mined block.
Response
Property | Type | Description |
---|---|---|
Promise<number> | number | Integer of the current block number the client is on. |
Example Request and Response
Prerequisite: You will need to install the Alchemy SDK before making requests with it.
The commands for installing it using npm or yarn are given below:
npm install alchemy-sdk
yarn add alchemy-sdk
Request
Here is an example of how to make a getBlockNumber
request using the Alchemy SDK:
// Imports the Alchemy SDK
const { Alchemy, Network } = require("alchemy-sdk");
// Configures the Alchemy SDK
const config = {
apiKey: "alchemy-replit", // Replace with your API key
network: Network.ETH_MAINNET, // Replace with your network
};
// Creates an Alchemy object instance with the config to use for making requests
const alchemy = new Alchemy(config);
const main = async () => {
//The response returns the block number of the most recently mined block
let response = await alchemy.core.getBlockNumber()
//Logging the response to the console
console.log(response)
};
main();
Response
16698044
Code Sandbox
You can test out the getBlockNumber
method using the code sandbox below:
Use Cases
Here are some potential use cases for the getBlockNumber
method:
-
Monitoring: You can use
getBlockNumber
to monitor the progress of the Ethereum network by checking the current block number. This can be useful for tracking the growth of the network and understanding how it is evolving over time. -
Smart Contract Development: If you are developing a smart contract, you may need to know the current block number for certain operations, such as setting timeouts or scheduling transactions. You can use
getBlockNumber
to get the current block number and use it in your contract logic.
Related Methods
Here are the methods related to getBlockNumber
:
- getBlock: Returns the block from the network based on the provided block number or hash.