Skip to main content

switchChain

Function to switch blockchain networks with Web3Auth.

Usage

await web3auth.switchChain({ chainId: '0xaa36a7' })

Parameters

ParameterDescriptionTypeRequired
{ chainId: string }chainId of the chain to switch to, e.g { chainId: "0xaa36a7" }ObjectYes

Example

switchChain.js
// Initialize Web3Auth first
// ...

// Switch to Sepolia Testnet
try {
await web3auth.switchChain({ chainId: '0xaa36a7' })
console.log('Successfully switched to Sepolia Testnet')
} catch (error) {
console.error('Error switching chain:', error)
}

// Later, switch to Ethereum Mainnet
try {
await web3auth.switchChain({ chainId: '0x1' })
console.log('Successfully switched to Ethereum Mainnet')
} catch (error) {
console.error('Error switching chain:', error)
}