Skip to main content

viem quickstart

This example shows how to use viem with Sei outside a React context: in a Node.js script, a CLI tool, or a backend service. For React dApps, see wagmi and the frontend guide.

Install

These snippets are TypeScript. To run any of them without a separate build step, use tsx (Node 18+):

Public client

A public client handles all read-only operations.
To use a custom RPC endpoint, pass the URL to http():

Reading chain data

This is the first milestone. It does not need a private key.
You are done when you see:
The exact numbers are illustrative. Your block number and balance will differ. Note the trailing n: getBlockNumber() and getBalance() return a bigint, while getTransactionCount() returns a number.

Try it live

Run these read-only calls against Sei Mainnet. You do not need to install anything or use a private key. They are the same reads that the script above performs, sent directly from your browser.

Edit and run

The buttons above send raw JSON-RPC requests. The sandbox below runs the full viem code from this page: imports, a public client, and several reads. You can edit and re-run it live. You do not need to install anything, connect a wallet, or use a key. The sandbox reads Sei Testnet, so its block number and balances do not match the Sei Mainnet reads above:

Wallet client

A wallet client handles signing and broadcasting transactions.
For browser use, replace http() with custom(window.ethereum). Then call walletClient.getAddresses() to get the connected account.

Sending a transaction

This next step requires a funded account. Get testnet SEI from the faucet.

Reading a contract

Writing to a contract

Simulating before writing

Estimating gas

Always use estimateGas instead of hard-coded values. Governance can adjust SSTORE costs on Sei.

Next steps