> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dynamic.xyz/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Switching Active Network

> [!WARNING]
>
> **Breaking change in SDK version 0.4.0**: `switchActiveNetwork` no longer automatically calls `addNetwork` when the network is not added to the wallet. You must now handle `NetworkNotAddedError` and explicitly call `addNetwork` if needed.

The active network id is the id that the Dynamic SDK uses to identify the active network of a wallet account.

For EVM wallets, it will be the same as the chain id (e.g: 1, 137, 56, etc).
For other chains, it will be an internal id used in Dynamic.

To switch the active network, you can use the `switchActiveNetwork` function.

## Usage

> [tab] JavaScript
>
> ```javascript theme={"system"}
> import { switchActiveNetwork } from '@dynamic-labs-sdk/client';
>
> const switchNetwork = async () => {
>   await switchActiveNetwork({ walletAccount, networkId: '1' });
> };
> ```

> [tab] React
>
> ```tsx theme={"system"}
> import { type WalletAccount } from '@dynamic-labs-sdk/client';
> import { useSwitchActiveNetwork } from '@dynamic-labs-sdk/react-hooks';
>
> function NetworkSwitchButton({ walletAccount, networkId }: { walletAccount: WalletAccount; networkId: string }) {
>   const { mutate: switchNetwork, isPending } = useSwitchActiveNetwork();
>
>   return (
>     <button
>       onClick={() => switchNetwork({ walletAccount, networkId })}
>       disabled={isPending}
>     >
>       {isPending ? 'Switching…' : 'Switch network'}
>     </button>
>   );
> }
> ```

## Error Handling

The `switchActiveNetwork` function can throw the following errors:

### NetworkSwitchingUnavailableError

If the wallet provider does not support network switching, the function will throw a `NetworkSwitchingUnavailableError` error.

You can check if the wallet provider supports network switching by calling the `isProgrammaticNetworkSwitchAvailable` function.

### NetworkNotAddedError

If the network is not added to the wallet, the function will throw a `NetworkNotAddedError` error. This error includes the network data in the `networkData` property, which can be passed directly to the `addNetwork` function.

```javascript theme={"system"}
import { switchActiveNetwork, addNetwork, NetworkNotAddedError, NetworkAddingUnavailableError } from '@dynamic-labs-sdk/client';

const switchNetwork = async () => {
  try {
    await switchActiveNetwork({ walletAccount, networkId: '137' });
  } catch (error) {
    if (error instanceof NetworkNotAddedError) {
      // The network is not added to the wallet
      // You can add it using the addNetwork function
      try {
        await addNetwork({ 
          walletAccount, 
          networkData: error.networkData 
        });
        // Retry switching after adding the network
        await switchActiveNetwork({ walletAccount, networkId: '137' });
      } catch (addError) {
        if (addError instanceof NetworkAddingUnavailableError) {
          console.error('This wallet does not support adding networks');
          // Prompt user to manually add the network in their wallet
        }
      }
    }
  }
}
```

## Related functions

* [Adding a Network](https://docs.dynamic.xyz/docs/javascript/reference/wallets/add-network.md)
* [Getting the Wallet Account Given an Address and Chain](https://docs.dynamic.xyz/docs/javascript/reference/wallets/get-wallet-account-from-address.md)
* [Getting Active Network](https://docs.dynamic.xyz/docs/javascript/reference/wallets/get-active-network.md)
* [Getting Networks Data](https://docs.dynamic.xyz/docs/javascript/reference/wallets/get-networks-data.md)
* [Connecting and Verifying a Wallet](https://docs.dynamic.xyz/docs/javascript/reference/wallets/connect-and-verify-wallet.md)
* [Wallet Provider Events](https://docs.dynamic.xyz/docs/javascript/reference/wallets/wallet-provider-events.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.