For the complete documentation index, see llms.txt. This page is also available as Markdown.

eth_chainId - Optimism

Example code for the eth_chainId json-rpc method. Сomplete guide on how to use eth_chainId json-rpc in GetBlock.io Web3 documentation.

This method returns the chain ID of the connected network. This is useful to confirm if you are connected to Optimism or not.

Parameters

  • None

Request Sample

curl --location --request POST 'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "jsonrpc": "2.0",
    "method": "eth_chainId",
    "params": [],
    "id": "getblock.io"
}'
const axios = require('axios');

const url = "https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/";
const headers = { "Content-Type": "application/json" };

const payload = {
    jsonrpc: "2.0",
    method: "eth_chainId",
    params: [],
    id: "getblock.io"
};

axios.post(url, payload, { headers })
    .then(response => {
        if (response.status === 200) {
            console.log("eth_chainId result:", response.data.result);
        } else {
            console.error("Error:", response.status, response.statusText);
        }
    })
    .catch(error => {
        console.error("Error:", error.response ? error.response.data : error.message);
    });

Response

A successful response returns the following:

{
    "jsonrpc": "2.0",
    "id": "getblock.io",
    "result": "0xa"
}

Response Parameters

  • id: A unique request identifier, matching the id sent in the request body.

  • jsonrpc: Specifies the use of JSON-RPC version 2.0.

  • result: The chain ID as a hexadecimal string. 0xa equals 10 (Optimism Mainnet).

Use Case

The eth_chainId method is commonly used for:

  • Transaction signing

  • Network verification

  • Wallet configuration

  • Multi-chain application support

Web3 Integration

Last updated

Was this helpful?