Alchemy Documentation
Website
Roadmap
Dashboard
Search…
👋
Welcome to Alchemy
🚀
Introduction
Why Use Alchemy?
Getting Started
Simple Web3 Script
Sending Transactions Using Web3
Core Products
Contributing to these Docs
Referral Program
Web3 University
✨
Enhanced APIs
NFT API
Transfers API (Tx History)
Transaction Receipts API
Token API
Parity API
Notify API
Debug API
Trace API
Subscription API (Websockets)
Unstoppable Domains APIs
⛓
Chain APIs
Ethereum API
Polygon API
Arbitrum API
Optimism API
Solana API
Flow Docs
Crypto.org Docs
Feature Support By Chain
📖
Documentation
Changelog
Alchemy Web3.js
Error Reference
Compute Units (CUs)
Throughput (Rate Limits)
Gas Limits for eth_call and eth_estimateGas
🎓
Road to Web3
Welcome
Important Info
Weekly Hackathons
Weekly Learning Challenges
💻
Tutorials
Hello World Smart Contract
How to Code and Deploy a Polygon Smart Contract
How to Create an NFT Tutorial
NFT Minter Tutorial: How to Create a Full Stack DApp
How to Deploy Your Own ERC-20 Token
Integrating Historical Transaction Data into your dApp
How to Track Mined and Pending Ethereum Transactions
📜
Guides
EIP-1559 Resource and Tutorial Hub
How to Use WebSockets
Dashboard Walkthrough
Deep Dive into eth_getLogs
How to Add Alchemy RPC Endpoints to Metamask
Alchemy Set-up for Macs
Choosing a Web3 Network
Running an Eth2.0 Staking Node or Validator with Alchemy
Internal Playbook: Upgrading Ethereum Nodes
Debugging CORS problems for end-users
📚
Resources
FAQ
Support
Blockchain 101
Web3 Glossary
Powered By
GitBook
Simple Web3 Script
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.
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:
1
mkdir web3-example
2
cd web3-example
Copied!
2. Install the Alchemy Web3 dependency if you have not already:
You can use any
web3 library
of your choosing, however there are tons of benefits to using
Alchemy Web3
!
1
npm install @alch/alchemy-web3
Copied!
3. Create a file named
index.js
and add the following contents:
You should ultimately replace
<api-key>
with your Alchemy HTTP API key.
1
async
function
main
()
{
2
const
{
createAlchemyWeb3
}
=
require
(
"@alch/alchemy-web3"
);
3
const
web3
=
createAlchemyWeb3
(
"https://eth-mainnet.alchemyapi.io/v2/<api-key>"
);
4
const
blockNumber
=
await
web3
.
eth
.
getBlockNumber
();
5
console
.
log
(
"The latest block number is "
+
blockNumber
);
6
}
7
main
();
Copied!
Unfamiliar with the async stuff? Check out this
Medium post
.
4. Run it using node
1
node index.js
Copied!
5. You should now see the latest block number output in your console!
1
The latest block number is 11043912
Copied!
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
.
Introduction - Previous
Getting Started
Next
Sending Transactions Using Web3
Last modified
3mo ago
Copy link
Contents
1. From your command line, create a new project directory and cd into it:
2. Install the Alchemy Web3 dependency if you have not already:
3. Create a file named index.js and add the following contents:
4. Run it using node
5. You should now see the latest block number output in your console!