Skip to content
Viem · TypeScript Interface for Ethereum
viem logo
Build reliable apps & libraries with lightweight, composable, and type-safe modules that interface with Ethereum
npm i viem
Modular
Composable modules to build applications & libraries with speed
Lightweight
Tiny bundle size optimized for tree-shaking
See more
Performant
Optimized architecture compared to alternative libraries
Typed APIs
Flexible programmatic APIs with extensive TypeScript typing

Overview

// 1. Import modules.
import { 
function createPublicClient<transport extends Transport, chain extends Chain | undefined = undefined>(parameters: PublicClientConfig<transport, chain>): PublicClient<transport, chain>

Creates a Public Client with a given Transport configured for a Chain.

Docs: https://viem.sh/docs/clients/public

A Public Client is an interface to "public" JSON-RPC API methods such as retrieving block numbers, transactions, reading from smart contracts, etc through Public Actions.

createPublicClient
,
function http(url?: string, config?: HttpTransportConfig): HttpTransport
http
} from 'viem'
import {
const mainnet: { blockExplorers: { readonly default: { readonly name: "Etherscan"; readonly url: "https://etherscan.io"; readonly apiUrl: "https://api.etherscan.io/api"; }; }; contracts: { readonly ensRegistry: { readonly address: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"; }; readonly ensUniversalResolver: { readonly address: "0xc0497E381f536Be9ce14B0dD3817cBcAe57d2F62"; readonly blockCreated: 16966585; }; readonly multicall3: { readonly address: "0xca11bde05977b3631167028862be2a173976ca11"; readonly blockCreated: 14353601; }; }; id: 1; name: "Ethereum"; nativeCurrency: { readonly name: "Ether"; readonly symbol: "ETH"; readonly decimals: 18; }; rpcUrls: { readonly default: { readonly http: readonly ["https://cloudflare-eth.com"]; }; }; sourceId?: number | undefined; testnet?: boolean | undefined; formatters?: undefined; serializers?: import("../../index.js").ChainSerializers<undefined> | undefined; fees?: import("../../index.js").ChainFees<undefined> | undefined; }
mainnet
} from 'viem/chains'
// 2. Set up your client with desired chain & transport. const
const client: { account: undefined; batch?: { multicall?: boolean | { batchSize?: number | undefined; wait?: number | undefined; } | undefined; } | undefined; cacheTime: number; chain: { blockExplorers: { ...; }; ... 9 more ...; fees?: ChainFees<...> | undefined; }; ... 53 more ...; extend: <const client extends { ...; } & Partial<...>>(fn: (client: Client<...>) => client) => Client<...>; }
client
=
createPublicClient<HttpTransport, { blockExplorers: { readonly default: { readonly name: "Etherscan"; readonly url: "https://etherscan.io"; readonly apiUrl: "https://api.etherscan.io/api"; }; }; ... 9 more ...; fees?: ChainFees<...> | undefined; }>(parameters: { ...; }): { ...; }

Creates a Public Client with a given Transport configured for a Chain.

Docs: https://viem.sh/docs/clients/public

A Public Client is an interface to "public" JSON-RPC API methods such as retrieving block numbers, transactions, reading from smart contracts, etc through Public Actions.

createPublicClient
({
chain?: Chain | { blockExplorers: { ...; }; ... 9 more ...; fees?: ChainFees<...> | undefined; } | undefined

Chain for the client.

chain
:
const mainnet: { blockExplorers: { readonly default: { readonly name: "Etherscan"; readonly url: "https://etherscan.io"; readonly apiUrl: "https://api.etherscan.io/api"; }; }; contracts: { readonly ensRegistry: { readonly address: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"; }; readonly ensUniversalResolver: { readonly address: "0xc0497E381f536Be9ce14B0dD3817cBcAe57d2F62"; readonly blockCreated: 16966585; }; readonly multicall3: { readonly address: "0xca11bde05977b3631167028862be2a173976ca11"; readonly blockCreated: 14353601; }; }; id: 1; name: "Ethereum"; nativeCurrency: { readonly name: "Ether"; readonly symbol: "ETH"; readonly decimals: 18; }; rpcUrls: { readonly default: { readonly http: readonly ["https://cloudflare-eth.com"]; }; }; sourceId?: number | undefined; testnet?: boolean | undefined; formatters?: undefined; serializers?: import("../../index.js").ChainSerializers<undefined> | undefined; fees?: import("../../index.js").ChainFees<undefined> | undefined; }
mainnet
,
transport: HttpTransport

The RPC transport

transport
:
function http(url?: string | undefined, config?: HttpTransportConfig | undefined): HttpTransport
http
(),
}) // 3. Consume an action! const
const blockNumber: bigint
blockNumber
= await
const client: { account: undefined; batch?: { multicall?: boolean | { batchSize?: number | undefined; wait?: number | undefined; } | undefined; } | undefined; cacheTime: number; chain: { blockExplorers: { ...; }; ... 9 more ...; fees?: ChainFees<...> | undefined; }; ... 53 more ...; extend: <const client extends { ...; } & Partial<...>>(fn: (client: Client<...>) => client) => Client<...>; }
client
.
getBlockNumber: (args?: GetBlockNumberParameters | undefined) => Promise<bigint>
getBlockNumber
()

Features

viem supports all these features out-of-the-box:

  • Abstractions over the JSON-RPC API to make your life easier
  • First-class APIs for interacting with Smart Contracts
  • Language closely aligned to official Ethereum terminology
  • Import your Browser Extension, WalletConnect or Private Key Wallet
  • Browser native BigInt, instead of large BigNumber libraries
  • Utilities for working with ABIs (encoding/decoding/inspection)
  • TypeScript ready (infer types from ABIs and EIP-712 Typed Data)
  • First-class support for Anvil, Hardhat & Ganache
  • Test suite running against forked Ethereum network

Community

Check out the following places for more wagmi-related content:

Support

Help support future development and make wagmi a sustainable open-source project:

Sponsors

Collaborator
Paradigm
Large Enterprise
WalletConnectStripe
Small Enterprise
FamilyContextPartyDAO
SushiSwapDynamicBitKeep
PrivySprucerollup.id
PancakeSwapCeloRainbow
PimlicoZoraSupa