eth_chainId - Polygon
The eth_chainId method returns the current chain ID used for signing replay-protected transactions (EIP-155). This method is essential for applications that need to verify they are connected to the correct network and for properly signing transactions.
Parameters
- None
Request
cURL
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"
}'JavaScript (Axios)
javascriptimport axios from 'axios';
const url = 'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/';
const payload = {
jsonrpc: '2.0',
method: 'eth_chainId',
params: [],
id: 'getblock.io'
};
axios.post(url, payload, {
headers: { 'Content-Type': 'application/json' }
})
.then(response => {
const chainId = parseInt(response.data.result, 16);
console.log('Chain ID:', chainId);
})
.catch(error => console.error(error));Python (requests)
pythonimport requests
url = "https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/"
payload = {
"jsonrpc": "2.0",
"method": "eth_chainId",
"params": [],
"id": "getblock.io"
}
response = requests.post(url, headers={"Content-Type": "application/json"}, json=payload)
result = response.json()
chain_id = int(result["result"], 16)
print(f"Chain ID: {chain_id}")Rust (reqwest)
rustuse reqwest::header;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = reqwest::Client::new();
let response = client
.post("https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/")
.header(header::CONTENT_TYPE, "application/json")
.body(r#"{
"jsonrpc": "2.0",
"method": "eth_chainId",
"params": [],
"id": "getblock.io"
}"#)
.send()
.await?;
println!("{}", response.text().await?);
Ok(())
}Response
json{
"jsonrpc": "2.0",
"id": "getblock.io",
"result": "0x89"
}Response Parameters
| Field | Type | Description |
|---|---|---|
| jsonrpc | string | JSON-RPC version (2.0) |
| id | string | Request identifier |
| result | string | Chain ID in hexadecimal format (0x89 = 137 for Polygon Mainnet) |
Use Case
The eth_chainId method is essential for:
- Network verification: Confirm connection to the correct network before operations
- Transaction signing: Include correct chain ID for EIP-155 replay protection
- Multi-chain applications: Dynamically detect and switch between networks
- Wallet integration: Verify network configuration matches user expectations
- Security: Prevent cross-chain replay attacks
Example: Network Verification
JavaScript (example)
javascriptconst POLYGON_MAINNET_CHAIN_ID = 137;
const POLYGON_AMOY_CHAIN_ID = 80002;
async function verifyNetwork(provider) {
const chainId = await provider.send('eth_chainId', []);
const numericChainId = parseInt(chainId, 16);
if (numericChainId === POLYGON_MAINNET_CHAIN_ID) {
console.log('Connected to Polygon Mainnet');
return 'mainnet';
} else if (numericChainId === POLYGON_AMOY_CHAIN_ID) {
console.log('Connected to Polygon Amoy Testnet');
return 'testnet';
} else {
throw new Error(`Unexpected chain ID: ${numericChainId}`);
}
}Error Handling
| Status Code | Error Message | Cause |
|---|---|---|
| 403 | Forbidden | Missing or invalid ACCESS-TOKEN |
| -32600 | Invalid Request | Malformed request body |
Web3 Integration
Ethers.js
javascriptimport { ethers } from 'ethers';
const provider = new ethers.JsonRpcProvider('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');
// Using network property
const network = await provider.getNetwork();
console.log('Chain ID:', network.chainId);
// Using raw RPC call
const chainIdHex = await provider.send('eth_chainId', []);
console.log('Chain ID (hex):', chainIdHex);Viem
javascriptimport { createPublicClient, http } from 'viem';
import { polygon } from 'viem/chains';
const client = createPublicClient({
chain: polygon,
transport: http('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/')
});
const chainId = await client.getChainId();
console.log('Chain ID:', chainId);Web3.js
javascriptimport { Web3 } from 'web3';
const web3 = new Web3('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');
const chainId = await web3.eth.getChainId();
console.log('Chain ID:', chainId);