Set up Alchemy with Viem
π Donβt have an API Key? Start here before setting up your project!
What is Viem?
Viem is a set of modules, which you can use in JavaScript or Typescript, helping you interface with Ethereum or Ethereum Virtual Machine (EVM) chains.
You will use it in this guide, configuring it with your Alchemy API URL, so you can read and write to the blockchain of your choice.
If youβre new to viem, check out our 60 minute video diving into it and the viem docs are a great place to start as well!
Letβs get started!
π§ Prerequisites
-
Node.js
- Required to run JavaScript/TypeScript outside the browser.
- Install Node.js 18 or later (viem requires modern Node features).
- Download from: https://nodejs.org/
-
npm (comes with Node.js)
- Default package manager bundled with Node.
- Automatically installed when you install Node.js.
Project Setup and Installation
From your command line, create a new project directory and install Viem:
Create Script
Create a file named index.js
and add the following contents, replacing demo
with your Alchemy API Key:
You can find the documentation of viemβs getBlockNumber which is configured to call out to eth_blockNumber on the Alchemy API.
Woo! Congrats! You just wrote your first web3 script using Alchemy and sent your first request to your Alchemy API endpoint π
You can visit your request logs to see the full details of the request come through the API!
Next Steps
So, whatβs next for you?
- Want to build an application using the latest and greatest web3 login experience? Check out getting started with our wallets.
- Need the industry best APIs to access blockchain data (tokens, transfers, nfts, prices and more)? Jump on over to our data APIs.
- Want to find all the methods available to you on our node API? Check out the documentation here.
- Need to learn more about crypto? Weβve got you covered at Alchemy University.