Skip to content

Handling networks

Read below to learn how to configure supported EVM networks for the Expo SDK and how to switch the embedded wallet's current network.

Configuring networks

Privy embedded wallets can support any EVM-compatible chain. You can configure EVM networks for Privy via the supportedChains property of the PrivyProvider component, per the instructions below.

Configuring viem-supported networks

TIP

If your desired EVM network is supported by the popular viem/chains package, continue with the instructions below. A full list of the package's supported networks is available here.

Otherwise, skip to the Other Networks section.

To configure viem-supported networks for Privy, first, install the viem package. This package contains JSON representations of several EVM networks, which will be used to initialize the Privy SDK.

sh
npm i viem

Next, import your required chains from the viem/chains package:

tsx
// Replace this with any of the networks listed at https://viem.sh/docs/chains/introduction#chains
import {base, baseGoerli, mainnet, goerli, polygon, polygonMumbai} from 'viem/chains';

Lastly, configure the supportedChains prop of your PrivyProvider with an array including your required networks.:

tsx
<PrivyProvider
  appId="your-privy-app-id"
  supportedChains={[base, baseGoerli, mainnet, goerli, polygon, polygonMumbai]}
>
  {/* your app's content */}
</PrivyProvider>

Other Networks

TIP

If your desired EVM network is not supported by viem/chains, you can still use Privy with it per the steps below!

First, import viem and use the package's defineChain method to build a JSON representation of your desired network.

tsx
import {defineChain} from 'viem';

export const myCustomChain = defineChain({
  id: 123456789, // Replace this with your chain's ID
  name: 'My Custom Chain',
  network: 'my-custom-chain',
  nativeCurrency: {
    decimals: 18, // Replace this with the number of decimals for your chain's native token
    name: 'My Native Currency Name',
    symbol: 'My Native Currency Symbol',
  },
  rpcUrls: {
    default: {
      http: ['https://my-custom-chain-https-rpc'],
      webSocket: ['wss://my-custom-chain-websocket-rpc'],
    },
  },
  blockExplorers: {
    default: {name: 'Explorer', url: 'my-custom-chain-block-explorer'},
  },
});

At minimum, you must provide the network's name and chain ID, native currency, RPC URLs, and a blockexplorer URL.

Then, pass the returned object (myCustomChain in the example above) to the supportedChains array of the PrivyProvider, like above.

Overriding a chain's RPC provider

By default, transactions from the embedded wallet will be sent using Privy's default RPC providers. Please note that Privy's default providers are subject to rate limits; these limits are sufficiently generous for developing your integration and moderate amounts of app usage.

As your app's usage scales, we recommend that you setup your own RPC providers (with Infura, Alchemy, QuickNode, Blast, etc.) and configure Privy to use these providers per the instructions below. Setting up your own providers gives you maximum control over RPC throughput and rate limits, and offers you much more visibility into RPC analytics and common errors.

To configure Privy to use a custom RPC provider, first, import the chain you want to override, and import the helper function addRpcUrlOverrideToChain from @privy-io/react-auth to override the RPC provider

ts
import {mainnet} from 'viem/chains';

import {addRpcUrlOverrideToChain} from '@privy-io/react-auth';

const mainnetOverride = addRpcUrlOverrideToChain(mainnet, INSERT_CUSTOM_RPC_URL);

Now, you can add the chain returned by addRpcUrlOverrideToChain (e.g. mainnetOverride) to the supportedChains config prop like before.

tsx
<PrivyProvider
  appId="your-privy-app-id"
  // Replace this with a list of your desired supported chains
  supportedChains={[mainnetOverride, ...otherChains]}
>
  {/* your app's content */}
</PrivyProvider>

Default Configuration

If you do not set, Privy's will default to a supportedChains array with the networks listed here

Switching networks

By default, embedded wallets will be connected to the first network specified in your supportedChains array, and to Ethereum mainnet if no supportedChains are specified.

To switch the embedded wallet to a different network, send a wallet_switchEthereumChain JSON-RPC request to the wallet's EIP-1193 provider. In the request's params, specify your target chainId as a hexadecimal string.

ts
await wallet.provider.request({
  method: 'wallet_switchEthereumChain',
  // Replace '0x5' with the chainId of your target network
  params: [{chainId: '0x5'}],
});

You can also use the eth_chainId request (with no params) to get the current network of the embedded wallet.