Getting Started
Overview
viem is a TypeScript interface for Ethereum that provides low-level stateless primitives for interacting with Ethereum. viem is focused on developer experience, stability, bundle size, and performance:
- Developer experience Automatic type safety and inference, comprehensive documentation, composable APIs.
- Stability Test suite runs against forked Ethereum networks, complete test coverage.
- Bundle size Tree-shakable lightweight modules.
- Performance Optimized encoding/parsing, async tasks only when necessary, consistently #1 in benchmarks.
You can learn more about the rationale behind the project in the Why viem section.
Installation
bash
npm i viem
bash
pnpm i viem
bash
yarn add viem
Quick Start
1. Set up your Client & Transport
Firstly, set up your Client with a desired Transport & Chain.
tsx
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
const client = createPublicClient({
chain: mainnet,
transport: http(),
})
INFO
In a production app, it is highly recommended to pass through your authenticated RPC provider URL (Alchemy, Infura, Ankr, etc). If no URL is provided, viem will default to a public RPC provider. Read more.
2. Consume Actions
Now that you have a Client set up, you can now interact with Ethereum and consume Actions!
tsx
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
const client = createPublicClient({
chain: mainnet,
transport: http(),
})
const blockNumber = await client.getBlockNumber()