Skip to main content

Overview

Magic’s EVM chain switching feature allows users to seamlessly switch between different EVM-compatible blockchain networks without requiring separate wallet instances. This feature supports Ethereum, Polygon, Base, Arbitrum, Optimism, and custom EVM networks.
EVM chain switching is available on web SDK version 31.0.0+ and React Native SDKs version 32.0.0+.

Setup

To enable EVM chain switching, you need to configure multiple networks when initializing the Magic SDK:
Legacy Network Parameter: When using the EVM Extension, you no longer need to pass the network parameter to the Magic constructor. The old approach new Magic('API_KEY', { network: 'sepolia' }) is not required with the EVM Extension.
Built-in Networks: Ethereum mainnet (chainId: 1) and Sepolia testnet (chainId: 11155111) are included out of the box and don’t need to be configured. You can switch to them using magic.evm.switchEVMChain(1) or magic.evm.switchEVMChain(11155111) regardless of your extension configuration.
JavaScript
import { Magic } from 'magic-sdk';
import { EVMExtension } from '@magic-ext/evm';

const customPolygonOptions = {
  rpcUrl: 'https://polygon-rpc.com/', // Polygon RPC URL
  chainId: 137, // Polygon chain id
  default: true, // Set as default network
};

const customOptimismOptions = {
  rpcUrl: 'https://mainnet.optimism.io',
  chainId: 10,
};

const magic = new Magic('YOUR_API_KEY', {
  extensions: [
    new EVMExtension([customPolygonOptions, customOptimismOptions]),
  ],
});

Switching Chains

Use the switchChain method to switch between configured EVM networks:
JavaScript
// Switch to Ethereum mainnet (chainId: 1)
await magic.evm.switchChain(1);

// Switch to Sepolia testnet (chainId: 11155111)
await magic.evm.switchChain(11155111);

// Switch to Optimism network (chainId: 10)
await magic.evm.switchChain(10);

// Verify the network switch
const provider = new ethers.BrowserProvider(magic.rpcProvider);
const network = await provider.getNetwork();
console.log(network.chainId); // => 1, 11155111, or 10

Network Configuration Options

When configuring EVM networks, you can specify the following options:
ParameterTypeDescription
rpcUrlStringRPC URL for the blockchain network
chainIdNumberChain ID for the network
defaultBooleanSet this network as the default (optional)

Complete Example

Here’s a complete example showing how to set up and use EVM chain switching:
JavaScript
import { Magic } from 'magic-sdk';
import { EVMExtension } from '@magic-ext/evm';
import { ethers } from 'ethers';

const magic = new Magic('YOUR_API_KEY', {
  extensions: [
    new EVMExtension([
      {
        rpcUrl: 'https://polygon-rpc.com/',
        chainId: 137,
        default: true,
      },
      {
        rpcUrl: 'https://mainnet.optimism.io',
        chainId: 10,
      },
    ]),
  ],
});

// Switch EVM network
await magic.evm.switchChain(10);

// Verify the network switch
const provider = new ethers.BrowserProvider(magic.rpcProvider);
const network = await provider.getNetwork();
console.log(network.chainId); // => 10

Resources

I