> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://v3.viem.sh/api/mcp` to find what you need.

# Switch Chain \[chains.switch]

Switches the target chain in a wallet.

## Usage

This example switches the target chain in a wallet.

:::code-group
```ts twoslash [example.ts]
import { optimism } from 'viem/chains'
import { client } from './viem.config'

await client.chains.switch({ id: optimism.id })
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
import { Client, custom, walletActions } from 'viem'
import { mainnet } from 'viem/chains'
import 'viem/window'

export const client = Client.create({
  chain: mainnet,
  transport: custom(window.ethereum!),
}).extend(walletActions())
```
:::

### Standalone Action

Call `Actions.chains.switch` directly by passing the Client as the first argument.

:::code-group
```ts twoslash [example.ts]
import { Actions } from 'viem'
import { optimism } from 'viem/chains'
import { client } from './viem.config'

await Actions.chains.switch(client, { id: optimism.id })
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
import { Client, custom } from 'viem'
import { mainnet } from 'viem/chains'
import 'viem/window'

export const client = Client.create({
  chain: mainnet,
  transport: custom(window.ethereum!),
})
```
:::

## Return Value

`void`

No value is returned.

## Parameters

### id

* **Type:** `number`

The ID of the chain to switch to.

```ts twoslash
import { Actions } from 'viem'
import { optimism } from 'viem/chains'
// [!include ~/snippets/docs/reference-client.ts:setup]
// ---cut---
await Actions.chains.switch(client, {
  id: optimism.id, // [!code focus]
})
```

## Errors

| Error | Description |
| --- | --- |
| `Provider.SwitchChainError` | The wallet does not recognize the requested chain. |
