Skip to main content

viem Quickstart

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

Install

These snippets are TypeScript. Run any of them with no separate build step using 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 needs no private key.
You’re done when you see:
The exact numbers are illustrative — your block number and balance will differ. Note the trailing n: getBlockNumber() and getBalance() return bigint, while getTransactionCount() returns a number.

Try it live

Run these read-only calls against Sei mainnet right now — no install, no private key. They’re the same reads the script above performs, issued straight from your browser.

Edit and run

The buttons above issue raw JSON-RPC. To run the full viem code from this page — imports, a public client, several reads — edit and re-run it live in the sandbox below. No install, no wallet, no key. Note the sandbox reads testnet (atlantic-2), so its block number and balances won’t match the mainnet reads above:

Wallet Client

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

Sending a Transaction

Next step — this 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 rather than hard-coding values. SSTORE costs on Sei are governance-adjustable.

Next Steps