viem is a popular TypeScript library on EVM for executing onchain actions with wallets. Privy’s server wallets on EVM natively integrate with viem, allowing you to use the library’s interfaces for signing messages, signing typed data, sending transactions, and more.

To integrate with viem, first install version 2^ of the library as a peer dependency:

npm i viem@latest

Then, use Privy’s createViemAccount method to initialize an instance of a viem Account for an EVM server wallet. As a parameter to this method, pass an object with the following:

FieldTypeDescription
walletIdstringID of the wallet.
address0x${string}Ethereum address of the wallet.
privyPrivyClientInstance of the Privy client for your app.

As an example, you can initialize an Account like so:

import {PrivyClient} from '@privy-io/server-auth';
import {createViemAccount} from '@privy-io/server-auth/viem';

// Initialize your Privy client
const privy = new PrivyClient(...);
// Create a viem account instance for a wallet
const account = await createViemAccount({
  walletId: 'insert-wallet-id',
  address: 'insert-address',
  privy
});

From the returned Account, you can then initialize a viem WalletClient to sign messages and execute transactions with the wallet like so:

import {createWalletClient, http, parseEther} from 'viem';
import {base} from 'viem/chains';

const client = createWalletClient({
  account, // `Account` instance from above
  chain: base, // Replace with your desired network
  transport: http()
});

const hash = await client.sendTransaction({
  to: '0x59D3eB21Dd06A211C89d1caBE252676e2F3F2218',
  value: parseEther('0.001')
});

Was this page helpful?