Using web3js it is possible get network ID using the following method:

// Contract ABI Load
const ABISimpleStorage = require("./build/contracts/SimpleStorage.json");

// Besu Connection Web3 Load
let web3 = new Web3(
  new Web3.providers.WebsocketProvider("ws://")
    const netID = await web3.eth.net.getId();

How we could do same thing in ethers.js?

const ABISimpleStorage = require("./build/contracts/SimpleStorage.json");
let provider = new ethers.providers.JsonRpcProvider();

let contract = new ethers.Contract(


5 Answers 5


This worked for me:

const { chainId } = await provider.getNetwork()
console.log(chainId) // 42
  • is it safe to assume 1 for homestead, 3 for ropsten etc as a constant?
    – dcsan
    Oct 23, 2021 at 8:30
  • 1
    @dcsan yes that would be safe to assume Oct 23, 2021 at 10:34

I was searching this today.

Try provider.getNetwork().chainId.

getNetwork().name gives human readable name like 'homestead'

  • 1
    catch 22 that to get a provider you need to give a network: let provider = await ethers.getDefaultProvider(network, Settings);
    – dcsan
    Oct 23, 2021 at 8:31
  • I've just found this out after trying to port my web3 to ethers and it's made me sad!
    – HubertBlu
    Dec 15, 2021 at 17:24
  • provider.getNetwork() returns a promise
    – juliushuck
    Jan 24, 2022 at 1:48
  • It works ! const network = await provider.getNetwork(); const chainId = network.chainId; Mar 30, 2022 at 16:15

Use the provider.getNetwork() function. It will return a Network type and in it contain the chainId. You can see in the doc here: https://docs.ethers.io/ethers.js/html/api-providers.html#id2

UPDATE: as it turn out i was wrong the chainID and networkID are the same most of the time, not all of the time and there aren’t any api in etherjs so you need to use json-rpc instead. To do that you use this: https://docs.ethers.io/v5/api/providers/jsonrpc-provider/#JsonRpcProvider-send. Where the method is “net_version” and the params is an empty array. It will return like this: https://eth.wiki/json-rpc/API#net_version.

  • Unfortunately, in ethers.js version 5, the chain id returned in provider.getNetwork() differs from the network id. Or in other words, in ethers.js version 4, the network id was returned instead of the chain id. So this does not work anymore. Jul 5, 2020 at 13:12
  • @ Marc Wäckerlin chainId and networkId are the same but with different name that's all: ethereum.stackexchange.com/a/37536/50595
    – haxerl
    Jul 7, 2020 at 1:46
  • 3
    chainId and networkId are not the same.
    – tuler
    Jul 10, 2020 at 19:31
  • catch 22 that to get a provider you need to give a network: let provider = await ethers.getDefaultProvider(network, Settings);
    – dcsan
    Oct 23, 2021 at 8:30

This is a solution for ^6.3.0 version of ethers.js:

let userProvider = new ethers.BrowserProvider(provider);
let objectNetwork = await userProvider.getNetwork();
// returns a bigint
let chainId = parseInt(objectNetwork.chainId.toString());

Ethers v6 update:

As stated by some answers above you can use const chainId = await provider.getNetwork()console.log(chainId) , But this has changed as of ethers v6. In v5, it returned a number but, in v6 a BigInt is returned so if you need to make use of the chainId in your app you need to account for it `

const chainIdBigInt = (await provider.getNetwork()).chainId; //returns BigInt => 1337n for hardhat
const chainId = Number(chainIdBigInt) // convert to interger;
console.log(chainId) // 1337


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.