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
Hey @Peter_Pham
Do you specifically want to use Web3Auth v9.7 only? Iâd recommend to shift to v10 SDKs ideally since v9 will be deprecated in a few months.