Privy is fully compatible with popular web3 libraries for interfacing wallets, including viem, wagmi, ethers, and web3js.

Read below to learn how to best integrate Privy alongside these libraries.

Viem

Viem represents connected wallets as either an account object, which can sign with the wallet, or a wallet client object, which can also send transactions from the wallet.

Getting an account

To get an account for a user’s connected wallet, import the toViemAccount method and the useWallets hook from the React SDK.

import {toViemAccount, useWallets} from '@privy-io/react-auth';

Then, pass the ConnectedWallet object for your user’s wallet to the method, which will return a LocalAccount instance.

const {wallets} = useWallets();

const wallet = wallets.find((wallet) => (wallet.address === 'your-desired-address'));
const account = await toViemAccount({wallet});

You can then use the account to sign messages, typed data payloads, and transactions.

Getting a wallet client

To get a viem wallet client for a user’s connected wallet, first import your desired network from the viem/chains package and import the createWalletClient method and custom transport from viem:

import {createWalletClient, custom} from 'viem';
// Replace `sepolia` with your desired network
import {sepolia} from 'viem/chains';

Then, find your desired wallet from the wallets array and switch its network to the chain you imported, using the wallet’s switchChain method:

const {wallets} = useWallets();
const wallet = wallets[0]; // Replace this with your desired wallet
await wallet.switchChain(sepolia.id);

Lastly, get the wallet’s EIP1193 provider using the wallet’s getEthereumProvider method and pass it to viem’s createWalletClient method like so:

const provider = await wallet.getEthereumProvider();
const walletClient = createWalletClient({
    account: wallet.address as Hex,
    chain: sepolia,
    transport: custom(provider),
});

You can then use the wallet client to get information about the wallet or request signatures and transactions.

Wagmi

Privy is fully compatible with wagmi. Please see our wagmi guide for setting up the integration.

Ethers

Ethers v5

const privyProvider = await wallet.getEthereumProvider();
const provider = new ethers.providers.Web3Provider(privyProvider);

Ethers v6

const provider = await wallet.getEthereumProvider();
const ethersProvider = new ethers.BrowserProvider(provider);
const signer = ethersProvider.getSigner();

Web3.js

Web3.js represents connected wallets as a Web3 object, which you can use to get information about the current wallet or the request signatures and transactions.

To get a Web3js provider for a user’s connected wallet, first find your desired wallet from the wallets array and switch it to your desired network, using the wallet’s switchChain method:

const {wallets} = useWallets();
const wallet = wallets[0]; // Replace this with your desired wallet
await wallet.switchChain(sepolia.id);

Then, get the wallet’s EIP1193 provider using the wallet’s getEthereumProvider method and pass it to Web3js’s Web3 constructor like so:

const provider = await wallet.getEthereumProvider();
const web3 = new Web3(provider);

You can then use the Web3 provider to get information about the wallet or request signatures and transactions.

Privy is fully compatible with popular web3 libraries for interfacing wallets, including viem, wagmi, ethers, and web3js.

Read below to learn how to best integrate Privy alongside these libraries.

Viem

Viem represents connected wallets as either an account object, which can sign with the wallet, or a wallet client object, which can also send transactions from the wallet.

Getting an account

To get an account for a user’s connected wallet, import the toViemAccount method and the useWallets hook from the React SDK.

import {toViemAccount, useWallets} from '@privy-io/react-auth';

Then, pass the ConnectedWallet object for your user’s wallet to the method, which will return a LocalAccount instance.

const {wallets} = useWallets();

const wallet = wallets.find((wallet) => (wallet.address === 'your-desired-address'));
const account = await toViemAccount({wallet});

You can then use the account to sign messages, typed data payloads, and transactions.

Getting a wallet client

To get a viem wallet client for a user’s connected wallet, first import your desired network from the viem/chains package and import the createWalletClient method and custom transport from viem:

import {createWalletClient, custom} from 'viem';
// Replace `sepolia` with your desired network
import {sepolia} from 'viem/chains';

Then, find your desired wallet from the wallets array and switch its network to the chain you imported, using the wallet’s switchChain method:

const {wallets} = useWallets();
const wallet = wallets[0]; // Replace this with your desired wallet
await wallet.switchChain(sepolia.id);

Lastly, get the wallet’s EIP1193 provider using the wallet’s getEthereumProvider method and pass it to viem’s createWalletClient method like so:

const provider = await wallet.getEthereumProvider();
const walletClient = createWalletClient({
    account: wallet.address as Hex,
    chain: sepolia,
    transport: custom(provider),
});

You can then use the wallet client to get information about the wallet or request signatures and transactions.

Wagmi

Privy is fully compatible with wagmi. Please see our wagmi guide for setting up the integration.

Ethers

Ethers v5

const privyProvider = await wallet.getEthereumProvider();
const provider = new ethers.providers.Web3Provider(privyProvider);

Ethers v6

const provider = await wallet.getEthereumProvider();
const ethersProvider = new ethers.BrowserProvider(provider);
const signer = ethersProvider.getSigner();

Web3.js

Web3.js represents connected wallets as a Web3 object, which you can use to get information about the current wallet or the request signatures and transactions.

To get a Web3js provider for a user’s connected wallet, first find your desired wallet from the wallets array and switch it to your desired network, using the wallet’s switchChain method:

const {wallets} = useWallets();
const wallet = wallets[0]; // Replace this with your desired wallet
await wallet.switchChain(sepolia.id);

Then, get the wallet’s EIP1193 provider using the wallet’s getEthereumProvider method and pass it to Web3js’s Web3 constructor like so:

const provider = await wallet.getEthereumProvider();
const web3 = new Web3(provider);

You can then use the Web3 provider to get information about the wallet or request signatures and transactions.