Automatically populating metamask network settings

The Speedy Nodes section of the Moralis website contains a very cool button (Add to Metamask) that automatically populates the network details in Metamask.


I would love to have this feature available on my website, to make it easy for my users as well. But I have not been able to figure out how you guys have done this. I am wondering if you guys would be wiling to share how this is done.

Ok, I figured it out, based on this metamask documentation :

async function addPolygonTestnetNetwork(){
    try {
        await ethereum.request({
            method: 'wallet_switchEthereumChain',
            params: [{ chainId: '0x13881' }], // Hexadecimal version of 80001, prefixed with 0x
    } catch (error) {
        if (error.code === 4902) {
            try {
                await ethereum.request({
                    method: 'wallet_addEthereumChain',
                    params: [{ 
                        chainId: '0x13881', // Hexadecimal version of 80001, prefixed with 0x
                        chainName: "POLYGON Mumbai",
                        nativeCurrency: {
                            name: "MATIC",
                            symbol: "MATIC",
                            decimals: 18,
                        rpcUrls: [""],
                        blockExplorerUrls: [""],
                        iconUrls: [""],
            } catch (addError){
                console.log('Did not add network');

1 Like

Yup it’s Metamask feature as you discovered. Nice work :+1:

Ok, I’ve read this and Andrew Conje’s verion over at
It’s dumb question time: :thinking:

  • Where’d the ‘ethereum’ come from? :woozy_face:
    Answer: provided by MetaMask (or any other wallet provider? :thinking:) as window.ethereum :star_struck:in the browser’s scope when the wallet is unlocked. :sunglasses:

  • Where’d the 80001 come from? :no_mouth:
    Answer: …? :expressionless:

This is the network Id for the polygon network.

Uh…I thought Polygon’s network ID is 137. Can you spot me your reference?

Oh my bad, I meant Polygon test network.
You can check it out here -

1 Like

Ok, cool. I got it working and attached to a button.
And I can tell if MetaMask is currently using Polygon via window.ethereum.chainId, so I can switch off my “add Polygon to MetaMask” button if it’s obviously not needed.

But…what if it’s not obviously not needed?

How do I detect if MetaMask has a Polygon wallet configured, but isn’t currently connected to it? I can’t find a MetaMask function to return a list of configured networks. And using a server-side function to record if this user has used my “Add Polygon to MetaMask” button won’t work because I’ll get the odd user who’s elsewise configured Polygon in their MetaMask without going through my function. Do I just blindly fire the function and hope MetaMask will reject a duplicate network add without alerting the user to my ignorance? Somehow I don’t think it’s that forgiving.


One more question: I just noticed MetaMask generated an identical address on Polygon as its ETH address. This seems plausible…but suspicious. Same key same address, everything being the same EVM address code…but shouldn’t it roll forward a bit to preserve identities across networks?

Since the Polygon, BSC, ETH, Theta use the same EVM that was built for ethereum, they have the same format of addresses. This means the pair of private-public keys that you’ll have for ethereum will work with any of the above-mentioned networks.

Infact, some users mistakenly send out funds to addresses of another network (for example- from Binance app to their ethereum wallet on BSC network) and they freak out as the funds don’t show up in their Ethereum network wallet. In this case, all you have to do is import your same keys on a different network (in this case BSC) on the wallet and you will immediately get access to those funds.

Hope I could clear it up for you a little.

Thanks. :slight_smile:

Well…yes…and no.

I sent a small test transaction from my Matic balance on CoinBase:

And it never arrived.

I’ve triple-checked my to-address from Coinbase and it’s identical to my MetaMask address on Matic…AND Ethereum. Something is not right with the results of my wallet_addEthereumChain call.

First, I am very suspicious that the Matic address matches my Ethereum address in the same MetaMask wallet. Second: sent transactions don’t arrive. Third: the “buy” button in MetaMask on my Matic account is disabled. Fourth: the account doesn’t fill in the address when it links to Polyscan.

Um…and I don’t know how to un-install the wallet and try again. Subsequent calls to wallet_addEthereumChain just fail. And there isn’t a wallet_remove_EthereumChain to get rid of it.


To delete a network from MetaMask: Settings -> Networks -> Your Network -> Delete.