Skip to main content
Switch your user’s embedded wallet to different EVM-compatible networks programmatically. MoonKey makes it easy to change networks for multi-chain applications.

Overview

Switching chains allows your application to interact with different blockchain networks without requiring users to manually change networks. This is particularly useful for:
  • Multi-chain dApps: Support operations across multiple networks
  • Network optimization: Move to cheaper or faster chains for specific operations
  • Feature availability: Access chain-specific protocols and features
  • User experience: Seamlessly switch networks based on app context

React SDK

Basic Usage

To switch the network of an embedded wallet, use the switchChain method from the useSwitchChain hook:

Chain ID Formats

The switchChain method accepts chain IDs in two formats:
Using decimal numbers is recommended for better readability and consistency across your codebase.

Supported Chains

MoonKey supports switching to any EVM-compatible chain that has been configured in your app. Common networks include:
Learn how to configure additional networks in the Configuring EVM Networks guide.

Complete Examples

Multi-Chain Network Selector

Network Switch with Confirmation

Error Handling

Handle common errors when switching chains:

Getting Current Chain

To get the current chain ID of the wallet:

Best Practices

Ensure chains are properly configured in your MoonKeyProvider:
Always inform users when switching networks:
Check chain compatibility before enabling features:
Display human-readable names instead of chain IDs:
Ensure correct network before executing transactions:
Provide fallback options when switching fails:

Troubleshooting

If you get a “chain not configured” error, add the chain to your configuration:
See Configuring EVM Networks for more details.
If the network doesn’t seem to switch:
  1. Check wallet state: Ensure wallet is connected
  2. Wait for promise: Make sure to await the switchChain call
  3. Verify chain support: Check if the chain is configured
  4. Check for errors: Look for error messages in console
Avoid switching chains multiple times in quick succession:
If switching with hex strings isn’t working, use decimal numbers:

Configure EVM Networks

Add and configure custom EVM networks

Send Transaction

Send transactions on different chains

Get User Wallets

Access wallet information and chain details

Viem Chains

Explore available chain configurations

Next Steps