Skip to content

Handling networks

Privy makes it easy to get and switch the network of a user's connected wallets.

Privy tip
You can easily configure networks for wallets connected to your app.

Get the current network

To get a wallet's currently connected network, first find the corresponding ConnectedWallet object from the wallets array. Then, inspect the chainId field of the object:

tsx
const {wallets} = useWallets();
const wallet = wallets[0]; // Replace this with your desired wallet
const chainId = wallet.chainId;
// 'eip155:1' (as an example for Ethereum mainnet)

This will be a CAIP-2 chain ID.

Switch networks

To switch the network of a connected wallet, first find the corresponding ConnectedWallet object from the wallets array.

tsx
const {wallets} = useWallets();
const wallet = wallets[0]; // Replace this with your desired wallet

Then, call the wallet's switchChain method. As a parameter to the method, pass the chain ID for your desired network as a number or hexadecimal string:

tsx
await wallet.switchChain(7777777);

For embedded wallets, switchChain will update the network of the embedded wallet behind the scenes. For external wallets, switchChain will prompt the user to switch to the target network within the external wallet's client (e.g. their browser extension or 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 has not been configured.
  • The user declines the request to switch their network, if using an external wallet.