switchChain
Function to switch blockchain networks with Web3Auth.
Usage
await web3auth.switchChain({ chainId: '0xaa36a7' })
Parameters
- Table
- Function Definition
| Parameter | Description | Type | Required |
|---|---|---|---|
{ chainId: string } | Chain ID of the chain to switch to, for example, { chainId: "0xaa36a7" } | Object | Yes |
switchChain(params: { chainId: string }): Promise<void>
The chainId must match a chain in web3auth.coreOptions.chains (from the dashboard or the
chains option). Otherwise, the SDK throws an Invalid chainId error. External wallets that
support a single chain namespace can't switch between namespaces (for example, from EVM to
Solana).
Example
switchChain.js
async function onConnected() {
const chains = web3auth.coreOptions.chains ?? []
const chainId = web3auth.currentChainId
// Update UI with chainId and available chains
}
// Initialize Web3Auth first
await web3auth.init()
// Switch to Sepolia Testnet
try {
await web3auth.switchChain({ chainId: '0xaa36a7' })
await onConnected()
console.log('Successfully switched to Sepolia Testnet')
} catch (error) {
console.error('Error switching chain:', error)
}