Viem: TypeScript Interface for Ethereum

2.48.1 · active · verified Sun Apr 19

Viem is a comprehensive, TypeScript-first interface for interacting with the Ethereum blockchain, designed to simplify dApp development. Currently stable at version 2.48.1, it exhibits a rapid release cadence with frequent patch updates and minor feature additions, reflecting active development. It provides robust abstractions over the JSON-RPC API, first-class APIs for smart contract interaction, and native BigInt support for handling large numbers without external libraries like BigNumber.js. Key differentiators include its deep integration with TypeScript for type inference from ABIs and EIP-712 typed data, strong alignment with official Ethereum terminology, and out-of-the-box support for local development environments such as Anvil, Hardhat, and Ganache. Viem aims to be a lightweight and highly type-safe alternative to other Ethereum libraries, focusing on developer experience and correctness through its strong type system.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a public client, connect to the Ethereum mainnet using an HTTP transport, and fetch the current block number. It highlights Viem's core client creation pattern and basic data fetching.

import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';

const client = createPublicClient({
  chain: mainnet,
  transport: http(process.env.ETHEREUM_RPC_URL ?? 'https://rpc.ankr.com/eth')
});

async function getBlockNumber() {
  try {
    const blockNumber = await client.getBlockNumber();
    console.log(`Current block number: ${blockNumber}`);
    return blockNumber;
  } catch (error) {
    console.error('Failed to get block number:', error);
    throw error;
  }
}

getBlockNumber();

view raw JSON →