Skip to content

eth_getCode

Returns code at a given address. Used to check if an address is a smart contract.

  1. DATA (20 bytes) — Address to get code from.
  2. QUANTITY|TAG — Block number, or "latest", "earliest", "pending".

DATA — The code from the given address. Returns 0x for EOA (non-contract) accounts.

Terminal window
# Check USDC contract
curl -X POST https://eth-mainnet.blockreq.com/v1/rpc/public \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_getCode",
"params": ["0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "latest"],
"id": 1
}'
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x6080604052..."
}

If the result is "0x", the address is an EOA (externally owned account), not a contract.

import { JsonRpcProvider } from "ethers";
const provider = new JsonRpcProvider("https://eth-mainnet.blockreq.com/v1/rpc/YOUR_API_KEY");
const code = await provider.getCode("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
console.log("Is contract:", code !== "0x");
from web3 import Web3
w3 = Web3(Web3.HTTPProvider("https://eth-mainnet.blockreq.com/v1/rpc/YOUR_API_KEY"))
code = w3.eth.get_code("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")
print(f"Is contract: {len(code) > 0}")
MethodCost
eth_getCode10 RU