Prerequisites
Before this page: have a connected wallet (see Connecting external wallets). Network operations act on a specific WalletAccount that your app supplies.
What you’ll build
Three things most web3 apps need:
- Show the wallet’s current network.
- Switch the wallet to another supported network.
- Validate the wallet is on the network your action requires — and switch (or ask the user to) when it isn’t.
Showing the active network
getActiveNetworkData returns { networkData } for the wallet’s current network — displayName, iconUrl, nativeCurrency, and the networkId. getNetworksData returns every network configured for your project, across all chains — filter it to the wallet’s chain so the picker only offers networks the wallet can actually switch to.
The active-network query refetches on networkChanged, so the picker reflects switches made anywhere — including from inside the wallet.
Validating before an action
Before a transaction that must run on a specific network, check the active network and switch if needed. Some wallets don’t allow programmatic switching — isProgrammaticNetworkSwitchAvailable tells you whether to switch for the user or ask them to switch in their wallet.
Call your network check right before sending — not just on page load. A user can switch networks in their wallet at any time, so validate at the moment the network actually matters.
Handling errors
See also