eth_chainId - Optimism
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
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"
}'bashconst 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);
});pythonimport requests
import json
url = "https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/"
headers = {
"Content-Type": "application/json"
}
payload = {
"jsonrpc": "2.0",
"method": "eth_chainId",
"params": [],
"id": "getblock.io"
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
if response.status_code == 200:
result = response.json().get("result")
print("eth_chainId result:", result)
else:
print("Error:", response.status_code, response.text)rsuse reqwest::Client;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let payload = json!({
"jsonrpc": "2.0",
"method": "eth_chainId",
"params": [],
"id": "getblock.io"
});
let response = client
.post("https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/")
.header("Content-Type", "application/json")
.json(&payload)
.send()
.await?;
let result: serde_json::Value = response.json().await?;
println!("{:#?}", result);
Ok(())
}Response
A successful response returns the following:
json{
"jsonrpc": "2.0",
"id": "getblock.io",
"result": "0xa"
}Response Parameters
id: A unique request identifier, matching theidsent in the request body.jsonrpc: Specifies the use of JSON-RPC version 2.0.result: The chain ID as a hexadecimal string.0xaequals 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
Ethers.js
javascriptconst { ethers } = require('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); // 56n for BSC Mainnetjsimport { createPublicClient, http } from 'viem';
import { optimism } from 'viem/chains';
const client = createPublicClient({
chain: optimism,
transport: http('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/')
});
const chainId = await client.getChainId();
console.log('Chain ID:', chainId);