Hi Guys, Do we have any example or tutorial for switchChain with web3auth version 9.7.0 ? I cannot find the docs or tutorial of ver 9.7.0
Thanks — Peter! I hear you: you’re trying to switch chains with Web3Auth v9.7.0 and can’t find an example. I grouped what you’ve given and a quick, practical example you can try, plus what I’d need if it still fails.
Captured details
- SDK: Web3Auth
- SDK version: 9.7.0 (from your message)
- Platform: not specified (I’ll assume web unless you tell me otherwise)
Quick explanation
- Chain switching uses the Ethereum provider RPC methods (wallet_switchEthereumChain and wallet_addEthereumChain). With Web3Auth you get a provider with request(…) so you can call these RPC methods the same way as with other providers — as long as the provider implements them.
Minimal example (web, after web3auth.connect())
try {
// provider is the object returned by await web3auth.connect()
await provider.request({
method: ‘wallet_switchEthereumChain’,
params: [{ chainId: ‘0x89’ }], // example: Polygon mainnet (hex)
});
} catch (switchError) {
// 4902 means the chain is not added in the wallet — add it
if (switchError && switchError.code === 4902) {
await provider.request({
method: ‘wallet_addEthereumChain’,
params: [{
chainId: ‘0x89’,
chainName: ‘Polygon Mainnet’,
nativeCurrency: { name: ‘MATIC’, symbol: ‘MATIC’, decimals: 18 },
rpcUrls: [‘https://polygon-rpc.com/’],
blockExplorerUrls: [‘https://polygonscan.com/’],
}],
});
} else {
// handle other errors
console.error(switchError);
throw switchError;
}
}
Notes and tips
- Make sure you’re calling provider from await web3auth.connect() (not a stale provider).
- Ensure chainId is in hex (0x…); decimal will usually fail.
- Some providers may not implement these RPCs; if provider.request is missing or the method returns “method not found”, the provider likely doesn’t support chain switching.
Useful docs
- MetaMask RPC methods (wallet_switchEthereumChain / wallet_addEthereumChain): Wallet API introduction | MetaMask developer documentation
- Web3Auth docs / SDK: Introduction | Documentation | Web3Auth (for provider initialization & connect flow)
If this still fails, please share only what’s needed:
- Your web3auth init + connect snippet (the minimal code you use to get provider)
- The exact error message from the browser console (or a screenshot)
I won’t ask for more than that — with those two items I can pinpoint whether it’s a provider capability issue, a param format issue, or something else. Want to paste your init/connect code now?
Note: This response was generated by an AI bot and may require further verification. The MetaMask Community Team has tracked this issue and will be responding as soon as possible.
Additionally, we would recommend you to join our biweekly Office Hours to get this resolved even faster. MetaMask Developer Office Hours