How to Get the Latest Block on Ethereum

Don't know where to start? This guide will walk you through writing a simple web3 script to get the latest block number from the Ethereum mainnet using Alchemy.


API Endpoint

This tutorial uses the getBlockNumber endpoint.

This guide assumes you've gone through the getting started steps and have an Alchemy account!

1. From your command line, create a new project directory and cd into it:

mkdir web3-example
cd web3-example

2. Install the Alchemy SDK if you have not already

You can use any web3 library of your choosing, however there are tons of benefits to using the Alchemy SDK.

npm install alchemy-sdk
yarn add alchemy-sdk

3. Create a file named index.js and add the following contents:

// Setup: npm install @alch/alchemy-sdk
const { Network, Alchemy } = require("alchemy-sdk");

// Optional Config object, but defaults to demo api-key and eth-mainnet.
const settings = {
  apiKey: "demo", // Replace with your Alchemy API Key.
  network: Network.ETH_MAINNET, // Replace with your network.

const alchemy = new Alchemy(settings);

async function main() {
  const latestBlock = await alchemy.core.getBlockNumber();
  console.log("The latest block number is", latestBlock);




You should ultimately replace demo with your Alchemy HTTP API key.

Unfamiliar with the async stuff? Check out this Medium post.

4. Run it using node

node index.js
  1. You should now see the latest block number output in your console!
The latest block number is 11043912

Woo! Congrats! You just wrote your first web3 script using Alchemy 🎉

Once you complete this tutorial, let us know how your experience was or if you have any feedback by tagging us on Twitter @alchemyplatform!

Not sure what to do next? Build upon your skills learned in this tutorial by checking out our beginner's tutorial for sending Ethereum transactions using Web3 and Alchemy.