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

eth_getCode - Base

Example code for the eth_getCode JSON-RPC method. Complete guide on how to use eth_getCode JSON-RPC in GetBlock Web3 documentation.

The eth_getCode method returns the bytecode at a given address. This is used to determine if an address is a smart contract and to retrieve the deployed contract code.

Parameters

Parameter
Type
Required
Description

address

string

Yes

20-byte address to check

blockParameter

string

Yes

Block number in hex, or "latest", "earliest", "pending"

Request

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_getCode",
    "params": ["0x4200000000000000000000000000000000000006", "latest"],
    "id": "getblock.io"
}'
const axios = require('axios');

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

const code = response.data.result;
const isContract = code !== '0x';
console.log('Is Contract:', isContract);
console.log('Code Length:', (code.length - 2) / 2, 'bytes');

Response

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

Response Parameters

Parameter
Type
Description

jsonrpc

string

JSON-RPC protocol version ("2.0")

id

string

Request identifier matching the request

result

string

Bytecode at the address (hex). Returns "0x" for EOAs

Use Cases

  • Contract Detection: Verify if address is a smart contract

  • Security Analysis: Examine contract bytecode for vulnerabilities

  • Contract Verification: Compare deployed code with source

  • Proxy Detection: Identify proxy patterns in bytecode

  • Contract Indexing: Catalog deployed contracts

Error Handling

Error Code
Message
Description

-32602

Invalid params

Invalid address format

-32603

Internal error

Node internal failure

Web3 Integration

Last updated

Was this helpful?