POST
/
0a9d79d93fb2f4a4b1e04695da2b77a7
curl --request POST \
  --url https://nd-422-757-666.p2pify.com/0a9d79d93fb2f4a4b1e04695da2b77a7 \
  --header 'Content-Type: application/json' \
  --data '{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "eth_chainId",
  "params": []
}'
{
  "jsonrpc": "<string>",
  "id": 123,
  "result": "<string>"
}

Ethereum API method that returns the current chain ID. Chain ID is used to sign replay-protected transactions and generally verify if a network is the desired one. It was introduced in EIP-155.

Get you own node endpoint today

Start for free and get your app to production levels immediately. No credit card required.

You can sign up with your GitHub, X, Google, or Microsoft account.

Parameters

  • none

Response

  • quantity — EIP-155 Chain ID

eth_chainId code examples

Learn more about the ChainstackProvider in ethers.js: ethers ChainstackProvider Documentation.

const ethers = require("ethers");



// Create a ChainstackProvider instance for Ethereum mainnet

const chainstack = new ethers.ChainstackProvider("mainnet");



const chainId = async () => {

  // This will return the value in Hex

  const chainId = await chainstack.send("eth_chainId");

  console.log(`Hex Chain ID: ${chainId}`);

};



chainId();

Use case

One possible use case for the eth_chainId method in Ethereum is to determine which chain a user is connected to when using a DApp with the MetaMask browser extension. This can be useful to provide a customized user experience based on the specific chain that the user is interacting with.

Here is an example of how the eth_chainId method might be used in this context with MetaMask. Keep in mind that the Chain ID is returned as a hexadecimal value:

JavaScript
// Check which network is selected on MetaMask

async function checkChain() {

  

    // Declare the desired chain ID to match

    const desiredChainId = '0x1'; // Ethereum mainnet chain ID

  

    // Retrieve the current Chain ID selected by the user 

    ethereum.request({

        method: 'eth_chainId'

    }).then(chainId => {

      

        // compare the actual chain ID it to the declared chain ID 

        if (chainId !== desiredChainId) {

          

            console.log(`You are currently on the wrong network. Please switch to the mainnet.`);

            // Call the promtSwitch function

            promtSwitch()

          

        } else {

            console.log("This is the correct network")

        }

    })

}

// Prompt user to switch to a determined network based on the chain ID

async function promtSwitch() {

    await window.ethereum.request({

        method: 'wallet_switchEthereumChain',

        params: [{

            chainId: '0x1' // chainId must be in HEX with 0x in front

        }],

    });

}

In this case, the checkChain function is called to get the current chain ID using the Ethereum object provided by MetaMask. The chain ID is then checked to determine if the user is connected to the Ethereum mainnet and calls the promtSwitch function to prompt the user to switch chain in case the chain ID returned does not match the desiredChainId constant.

Body

application/json
id
integer
default:1
jsonrpc
string
default:2.0
method
string
default:eth_chainId
params
any[]

Response

200 - application/json
The network Chain ID
jsonrpc
string
id
integer
result
string