Skip to main content

Detect a user's network

It's important to keep track of the user's network chain ID because all RPC requests are submitted to the currently connected network.

Use the eth_chainId RPC method to detect the chain ID of the user's current network. Listen to the chainChanged provider event to detect when the user changes networks.

For example, the following code detects a user's network and when the user changes networks:

index.js
const chainId = await window.ethereum.request({ method: "eth_chainId" });

window.ethereum.on("chainChanged", handleChainChanged);

function handleChainChanged(chainId) {
// We recommend reloading the page, unless you must do otherwise.
window.location.reload();
}

Chain IDs

These are the chain IDs of the Ethereum networks that MetaMask supports by default. Consult chainid.network for more.

HexDecimalNetwork
0x11Ethereum main network (Mainnet)
0x55Goerli test network
0xaa36a711155111Sepolia test network
0xe70459140Linea Goerli test network
0x5391337Localhost test networks