- React
- React Native
- NodeJS
- NodeJS (server-auth)
- Python
- Rust
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 thetoViemAccount method and the useWallets hook from the React SDK.Report incorrect code
Copy
Ask AI
import {toViemAccount, useWallets} from '@privy-io/react-auth';
ConnectedWallet object for your user’s wallet to the method, which will return a LocalAccount instance.Report incorrect code
Copy
Ask AI
const {wallets} = useWallets();
const wallet = wallets.find((wallet) => (wallet.address === 'your-desired-address'));
const account = await toViemAccount({wallet});
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 theviem/chains package and import the createWalletClient method and custom transport from viem:Report incorrect code
Copy
Ask AI
import {createWalletClient, custom} from 'viem';
// Replace `sepolia` with your desired network
import {sepolia} from 'viem/chains';
wallets array and switch its network to the chain you imported, using the wallet’s switchChain method:Report incorrect code
Copy
Ask AI
const {wallets} = useWallets();
const wallet = wallets[0]; // Replace this with your desired wallet
await wallet.switchChain(sepolia.id);
getEthereumProvider method and pass it to viem’s createWalletClient method like so:Report incorrect code
Copy
Ask AI
const provider = await wallet.getEthereumProvider();
const walletClient = createWalletClient({
account: wallet.address as Hex,
chain: sepolia,
transport: custom(provider),
});
Wagmi
Privy is fully compatible withwagmi. Please see our wagmi guide for setting up the integration.Ethers
Ethers v5
Report incorrect code
Copy
Ask AI
const privyProvider = await wallet.getEthereumProvider();
const provider = new ethers.providers.Web3Provider(privyProvider);
Ethers v6
Report incorrect code
Copy
Ask AI
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 thewallets array and switch it to your desired network, using the wallet’s switchChain method:Report incorrect code
Copy
Ask AI
const {wallets} = useWallets();
const wallet = wallets[0]; // Replace this with your desired wallet
await wallet.switchChain(sepolia.id);
getEthereumProvider method and pass it to Web3js’s Web3 constructor like so:Report incorrect code
Copy
Ask AI
const provider = await wallet.getEthereumProvider();
const web3 = new Web3(provider);
Third-party libraries may require additional shims to be used in a React Native environment.
Integrating with viem
First, import the necessary methods, objects, and networks from viem:Report incorrect code
Copy
Ask AI
import {createWalletClient, custom} from 'viem';
// Replace 'mainnet' with your desired network
import {mainnet} from 'viem/chains';
Report incorrect code
Copy
Ask AI
const provider = await wallet.getProvider();
await provider.request({
method: 'wallet_switchEthereumChain',
// Replace '0x1' with the chain ID of your desired network
params: [{chainId: '0x1'}],
});
Report incorrect code
Copy
Ask AI
const walletClient = createWalletClient({
// Replace this with your desired network that you imported from viem
chain: mainnet,
transport: custom(provider),
});
signMessage, signTypedData, and sendTransaction!Integrating with ethers
First, import ethers:Report incorrect code
Copy
Ask AI
import {ethers} from 'ethers';
Report incorrect code
Copy
Ask AI
await provider.request({
method: 'wallet_switchEthereumChain',
// Replace '0x1' with the chain ID of your desired network
params: [{chainId: '0x1'}],
});
Report incorrect code
Copy
Ask AI
const ethersProvider = new ethers.providers.Web3Provider(provider);
const ethersSigner = ethersProvider.getSigner();
signMessage and sendTransaction.Integrating with web3.js
First, import web3:Report incorrect code
Copy
Ask AI
import {Web3} from 'web3';
Report incorrect code
Copy
Ask AI
await wallet.provider.request({
method: 'wallet_switchEthereumChain',
// Replace '0x1' with the chain ID of your desired network
params: [{chainId: '0x1'}],
});
Report incorrect code
Copy
Ask AI
const web3 = new Web3(wallet.getEthereumProvider());
Viem
viem is a popular TypeScript library on EVM for executing onchain actions with wallets. Privy’s 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:Report incorrect code
Copy
Ask AI
npm i viem@latest
createViemAccount method to initialize an instance of a viem Account for an EVM wallet. As a parameter to this method, pass an object with the following:| Field | Type | Description |
|---|---|---|
privy | PrivyClient | Instance of the Privy client for your app. |
walletId | string | ID of the wallet. |
address | 0x${string} | Ethereum address of the wallet. |
Account like so:Report incorrect code
Copy
Ask AI
import {PrivyClient} from '@privy-io/node';
import {createViemAccount} from '@privy-io/node/viem';
// Initialize your Privy client
const privy = new PrivyClient(...);
// Create a viem account instance for a wallet
const account = await createViemAccount(privy, {
walletId: 'insert-wallet-id',
address: 'insert-address'
});
If your wallet requires an authorization context,
you should pass it to the
createViemAccount method like so:Report incorrect code
Copy
Ask AI
const serverWalletAccount = await createViemAccount(privy, {
walletId: 'insert-wallet-id',
address: 'insert-address',
authorizationContext: {
authorization_private_keys: ['your authorization private key']
}
});
Account, you can then initialize a viem WalletClient to sign messages and execute transactions with the wallet like so:Report incorrect code
Copy
Ask AI
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')
});
The
@privy-io/server-auth library is deprecated. We recommend integrating @privy-io/node for
the latest features and support.Viem
viem is a popular TypeScript library on EVM for executing onchain actions with wallets. Privy’s 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:Report incorrect code
Copy
Ask AI
npm i viem@latest
createViemAccount method to initialize an instance of a viem Account for an EVM wallet. As a parameter to this method, pass an object with the following:| Field | Type | Description |
|---|---|---|
walletId | string | ID of the wallet. |
address | 0x${string} | Ethereum address of the wallet. |
privy | PrivyClient | Instance of the Privy client for your app. |
Account like so:Report incorrect code
Copy
Ask AI
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
});
Account, you can then initialize a viem WalletClient to sign messages and execute transactions with the wallet like so:Report incorrect code
Copy
Ask AI
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')
});
Ethers
Ethers is a popular TypeScript library for interacting with the Ethereum blockchain. Privy’s wallets on EVM natively integrate with ethers, allowing you to use the library’s interfaces for signing messages, signing typed data, sending transactions, and more.To integrate withethers, first install version 6^ of the library as a peer dependency:Report incorrect code
Copy
Ask AI
npm i ethers@latest
createEthersSigner method to initialize an instance of an ethers Wallet for an EVM wallet. As a parameter to this method, pass an object with the following:| Field | Type | Description |
|---|---|---|
walletId | string | ID of the wallet. |
address | 0x${string} | Ethereum address of the wallet. |
provider | ethers.JsonRpcProvider | Instance of the ethers provider. |
privyClient | PrivyClient | Instance of the Privy client for your app. |
Report incorrect code
Copy
Ask AI
import {ethers, TransactionRequest} from 'ethers';
import {PrivyClient} from '@privy-io/server-auth';
import {createEthersSigner} from '@privy-io/server-auth/ethers';
// Initialize your Privy client
const privyClient = new PrivyClient(...);
// Initialize your ethers provider
const provider = new ethers.JsonRpcProvider('https://base.llamarpc.com');
// Get your wallet
const walletId = 'insert-wallet-id';
const wallet = await privyClient.walletApi.getWallet({id: walletId});
const address = wallet.address;
// Create an ethers signer
const signer = createEthersSigner({
walletId,
address,
provider,
privyClient,
});
Report incorrect code
Copy
Ask AI
const transaction: TransactionRequest = {
to: TO_ADDRESS,
value: 100,
chainId: 8453,
};
const signedMessage = await signer.signMessage('foobar');
const signedTransaction = await signer.signTransaction(transaction);
const result = await signer.sendTransaction(transaction);
Integrating with eth-account
Privy EVM wallets natively integrate with the Python eth-account library, allowing you to use the library’s interfaces for signing messages, signing typed data, sending transactions, and more.To integrate with eth-account, first install the library as a dependency:Report incorrect code
Copy
Ask AI
pip install privy-eth-account
create_eth_account function to initialize an instance of an Account for an EVM wallet. As parameters to this function, pass:| Parameter | Type | Description |
|---|---|---|
client | PrivyHTTPClient | Instance of the Privy HTTP client for your app. |
address | str | Ethereum address of the wallet. |
wallet_id | str | ID of the wallet. |
eth-account:Report incorrect code
Copy
Ask AI
from privy_eth_account import create_eth_account, PrivyHTTPClient
from eth_account.messages import encode_typed_data, encode_defunct
# Initialize your Privy client
client = PrivyHTTPClient(
app_id="YOUR_APP_ID",
app_secret="YOUR_APP_SECRET",
authorization_key="YOUR_AUTHORIZATION_KEY"
)
# Create an account instance for a wallet
wallet_id = "insert-wallet-id"
wallet_address = "insert-wallet-address"
account = create_eth_account(client, wallet_address, wallet_id)
Sign a message
Report incorrect code
Copy
Ask AI
# Signing a message (personal_sign)
message = encode_defunct(text="Hello, Privy!")
signed_message = account.sign_message(message)
print(f"Signed message: {signed_message}")
Sign a transaction
Report incorrect code
Copy
Ask AI
# Signing a transaction
transaction = {
"to": "0x123...789",
"value": 100,
"chain_id": 8453
}
signed_tx = account.sign_transaction(transaction)
print(f"Signed transaction: {signed_tx}")
Signing EIP-712 Typed Data
Report incorrect code
Copy
Ask AI
# Example EIP-712 typed data
# Structure your typed data as a full message
full_message = {
"domain": {
"name": "My App",
"version": "1",
"chainId": 8453,
"verifyingContract": "0xCc9c3D98163F4F6Af884e259132e15D6d27A5c57",
"salt": "pepper"
},
"types": {
'EIP712Domain': [
{'name': 'name', 'type': 'string'},
{'name': 'version', 'type': 'string'},
{'name': 'chainId', 'type': 'uint256'},
{'name': 'verifyingContract', 'type': 'address'},
{'name': 'salt', 'type': 'string'}
],
'Person': [
{'name': 'name', 'type': 'string'},
{'name': 'wallet', 'type': 'address'}
],
'Mail': [
{'name': 'from', 'type': 'Person'},
{'name': 'to', 'type': 'Person'},
{'name': 'contents', 'type': 'string'}
]
},
"message": {
"from": {
"name": "Alice",
"wallet": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"
},
"to": {
"name": "Bob",
"wallet": "0xCc9c3D98163F4F6Af884e259132e15D6d27A5c57"
},
"contents": "Hello, Bob!"
},
"primaryType": "Mail"
}
# Sign the typed data using the full_message parameter
signed_typed_data = account.sign_typed_data(full_message=full_message)
print(f"Signed typed data: {signed_typed_data}")
Alloy
alloy is a comprehensive Rust library for Ethereum that provides type-safe, high-performance blockchain interactions. Privy’s Rust SDK integrates seamlessly with alloy, allowing you to leverage alloy’s powerful types and utilities while using Privy for wallet management and signing.Coming soon.

