Networks
Privy makes it easy to get and switch the network of external wallets users connect to your app. You can find the full list of Privy's supported networks here.
Getting the current network
To get a wallet's current network, simply find the ConnectedWallet
object for your desired wallet from the useWallets
array, and inspect its chainId
property:
// Find your desired wallet
const {wallets} = useWallets();
const wallet = wallets.find((wallet) => (wallet.address === 'your-desired-address'));
// Inspect its chainId
const chainId = wallet.chainId;
console.log(chainId);
// 'eip155:1' (as an example for Ethereum mainnet)
This will be a CAIP-2 formatted chain ID.
Switching networks
If you need the user's wallet to always be on a given network, we recommend calling switchChain
for the user's wallet on page load.
To switch the network of a wallet connected through Privy, first find the ConnectedWallet
object for your desired wallet from the useWallets
array:
const {wallets} = useWallets();
const wallet = wallets.find((wallet) => (wallet.address === 'your-desired-address'));
Then, call the ConnectedWallet
object's switchChain
method, passing your desired chain ID as a number
or as a 0x
-prefixed hexadecimal string:
// These lines are equivalent
await wallet.switchChain(137);
await wallet.switchChain('0x89');
For external wallets, switchChain
will prompt the user to switch to the target network within the external wallet's client (e.g. the MetaMask browser extension or the Rainbow wallet mobile app).
switchChain
returns a Promise that resolves to void
once the wallet has successfully been switched to the target network. The Promise will reject with an error if:
- The target chain is not supported.
- The user declines the request to switch their network, from their external wallet client.
If you're using an external library like ethers
or viem
, you should switch the wallet's network to your desired network before initializing the wallet's client/provider instance for that library (e.g. getEthersProvider
for ethers
and createWalletClient
for viem
).
If you switch a wallet's network after initializing the external library's wallet provider, the provider may have an outdated chain ID.
The MetaMask mobile app currently has a bug that prevents an app from switching a wallet's network. This should be fixed in version 7.4.0 of the mobile app, which is slowly being rolled out.
Supported networks
By default, external wallets can be used on the networks listed below.
You can easily add additional networks to your app per these instructions!
Network | Chain ID | Supported? |
---|---|---|
Arbitrum | 42161 | ✅ |
Arbitrum Goerli | 421613 | ✅ |
Avalanche C-Chain | 43114 | ✅ |
Avalanche Fuji | 43113 | ✅ |
Base | 8453 | ✅ |
Base Goerli | 84531 | ✅ |
Celo | 42220 | ✅ |
Celo Alfajores | 44787 | ✅ |
Ethereum | 1 | ✅ |
Ethereum Goerli | 5 | ✅ |
Ethereum Sepolia | 11155111 | ✅ |
Linea | 59144 | ✅ |
Linea Testnet | 59140 | ✅ |
Optimism | 10 | ✅ |
Optimism Goerli | 420 | ✅ |
Polygon | 137 | ✅ |
Polygon Mumbai | 80001 | ✅ |
Not all wallet clients support all networks; for example, Rainbow Wallet does not support most testnets. In kind, exact network support with Privy will vary on which wallet client is being used.
Adding additional networks
If your app doesn't use one of the default supported networks listed above, you can easily configure Privy to support your app's network! Simply follow the instructions below:
To support an additional network, first check if your desired network has a corresponding JSON representation in the @wagmi/chains
library, a public repository of EVM networks. You can find the full list of networks supported by that library here.
If your network is included in @wagmi/chains
, you can simply import the network and follow the instructions below.
If your network is not included in @wagmi/chains
, you can instead build your own JSON representation of the network, that implements wagmi's Chain
type. You can find the required fields of this type here.
First, install the latest version of the @privy-io/react-auth
and @wagmi/chains
SDKs:
npm i @privy-io/react-auth@latest @wagmi/chains
In particular, your @privy-io/react-auth
dependency must be on version 1.37.0 or later.
Next, import your desired network(s) from @wagmi/chains
:
As an example, here we import the @wagmi/chains
objects for the Aurora mainnet and testnet:
// Replace this with any of the networks listed at https://wagmi.sh/core/chains#supported-chains
import {aurora, auroraTestnet} from '@wagmi/chains';
Lastly, configure your PrivyProvider
with these additional network(s):
The config
property of the PrivyProvider
contains an optional additionalChains
field. In this field, pass an array containing your imported chain objects from @wagmi/chains
, like below:
<PrivyProvider
appId='your-privy-app-id'
config={{
...theRestOfYourConfig,
// Replace the entries in this array with the chains you imported from `@wagmi/chains`
additionalChains: [aurora, auroraTestnet]
}}
>
{/* your app's content */}
</PrivyProvider>
That's it! Your user's wallet can now take on-chain actions on any of Privy's default supported chains, or any other chains you've supplied in additionalChains
.