eth_chainId - Ethereum

This method returns the currently configured chain ID as a hex-encoded integer. Chain ID is defined by EIP-155 and is the value applications sign into transactions to prevent replay across chains. Ethereum mainnet returns 0x1, Sepolia returns 0xaa36a7, and Hoodi returns 0x88bb0.

Parameters

This method takes no parameters.

Request

bashcurl --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"
}'

example.js

javascriptconst axios = require('axios');

const response = await axios.post('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/', {
    jsonrpc: '2.0',
    method: 'eth_chainId',
    params: [],
    id: 'getblock.io'
}, {
    headers: { 'Content-Type': 'application/json' }
});

console.log(response.data.result);

example.py

pythonimport requests

response = requests.post(
    'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/',
    headers={'Content-Type': 'application/json'},
    json={
        'jsonrpc': '2.0',
        'method': 'eth_chainId',
        'params': [],
        'id': 'getblock.io'
    }
)

print(response.json())

example.rs

rustuse reqwest::Client;
use serde_json::{json, Value};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();
    
    let response = client
        .post("https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/")
        .header("Content-Type", "application/json")
        .json(&json!({
            "jsonrpc": "2.0",
            "method": "eth_chainId",
            "params": [],
            "id": "getblock.io"
        }))
        .send()
        .await?
        .json::<Value>()
        .await?;
    
    println!("Result: {}", response["result"]);
    Ok(())
}

Response

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

Response Parameters

ParameterTypeDescription
jsonrpcstringJSON-RPC protocol version ("2.0")
idstringRequest identifier matching the request
resultstringHex-encoded chain ID (0x1 = Ethereum mainnet, 0xaa36a7 = Sepolia, 0x88bb0 = Hoodi)

Use Cases

  • Transaction Signing: Wallets and signers include the chain ID in signed transactions per EIP-155 to prevent replay attacks across chains
  • Chain-Selection Logic: dApps confirm the connected endpoint matches the intended network before initiating a write
  • Multi-Chain Router Behavior: Cross-chain aggregators verify the target chain before sending calldata to the correct contract
  • Wallet UX: Prompt users to switch networks when the connected chain ID doesn't match the dApp's expected chain

Error Handling

Error CodeMessageDescription
-32603Internal errorNode failed to return its configured chain ID

Web3 Integration

ethers-example.js

javascriptimport { ethers } from 'ethers';

const provider = new ethers.JsonRpcProvider('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');

const network = await provider.getNetwork();
console.log('Chain ID:', network.chainId.toString());  // "1" for Ethereum mainnet

viem-example.js

javascriptimport { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';

const client = createPublicClient({
    chain: mainnet,
    transport: http('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/'),
});

const chainId = await client.getChainId();
console.log('Chain ID:', chainId);  // 1 for Ethereum mainnet