Skip to content

Json_rpc

JSON-RPC is the primary protocol used by Ethereum and EVM-compatible blockchains for remote procedure calls. It provides a simple, lightweight mechanism for interacting with blockchain nodes.


JSON-RPC is a stateless, remote procedure call protocol that:

  • Uses JSON for data encoding
  • Works over HTTP or WebSocket
  • Follows request-response model
  • Is language-agnostic
{
"jsonrpc": "2.0",
"method": "method_name",
"params": ["param1", "param2"],
"id": 1
}
{
"jsonrpc": "2.0",
"result": "returned_value",
"id": 1
}
{
"jsonrpc": "2.0",
"error": {
"code": -32600,
"message": "Invalid Request"
},
"id": 1
}

┌─────────────────────────────────────────────────────────────────┐
│ BLOCK METHODS │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_blockNumber │ │
│ │ │ │
│ │ Returns the latest block number │ │
│ │ │ │
│ │ Request: │ │
│ │ { "jsonrpc": "2.0", "method": "eth_blockNumber", │ │
│ │ "params": [], "id": 1 } │ │
│ │ │ │
│ │ Response: │ │
│ │ { "jsonrpc": "2.0", "result": "0x10d4f1e", "id": 1 } │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_getBlockByNumber / eth_getBlockByHash │ │
│ │ │ │
│ │ Returns block data by number or hash │ │
│ │ │ │
│ │ Parameters: │ │
│ │ - Block number or hash │ │
│ │ - Boolean (true = include full transaction objects) │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_getBlockTransactionCountByNumber │ │
│ │ eth_getBlockTransactionCountByHash │ │
│ │ │ │
│ │ Returns number of transactions in a block │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ TRANSACTION METHODS │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_sendRawTransaction │ │
│ │ │ │
│ │ Submits a signed transaction to the network │ │
│ │ │ │
│ │ Parameters: [signed_transaction_hex] │ │
│ │ │ │
│ │ Returns: transaction_hash │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_getTransactionByHash │ │
│ │ │ │
│ │ Returns transaction by its hash │ │
│ │ │ │
│ │ Parameters: [transaction_hash] │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_getTransactionReceipt │ │
│ │ │ │
│ │ Returns transaction receipt after execution │ │
│ │ Includes: status, gasUsed, logs, contractAddress │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_call / eth_estimateGas │ │
│ │ │ │
│ │ eth_call: Read-only contract call (view/pure functions) │ │
│ │ eth_estimateGas: Estimate gas needed for transaction │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ STATE METHODS │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_getBalance │ │
│ │ │ │
│ │ Returns account balance at specified block │ │
│ │ │ │
│ │ Parameters: [address, block_number] │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_getTransactionCount │ │
│ │ │ │
│ │ Returns number of transactions sent from an address │ │
│ │ (Also known as nonce) │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_getCode │ │
│ │ │ │
│ │ Returns bytecode at an address (for smart contracts) │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_getStorageAt │ │
│ │ │ │
│ │ Returns storage value at specific slot │ │
│ │ │ │
│ │ Parameters: [address, slot_position, block_number] │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ CONTRACT METHODS │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_call │ │
│ │ │ │
│ │ Executes contract function without creating transaction │ │
│ │ Used for view/pure functions and reading state │ │
│ │ │ │
│ │ Parameters: │ │
│ │ { │ │
│ │ to: "0x...", // Contract address │ │
│ │ data: "0x..." // Function selector + encoded args│ │
│ │ } │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_estimateGas │ │
│ │ │ │
│ │ Estimates gas required for transaction │ │
│ │ Useful for gas optimization and UX │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ eth_getLogs │ │
│ │ │ │
│ │ Returns logs matching filter criteria │ │
│ │ Used for event searching and indexing │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘

ProviderNetworkURL
InfuraEthereum Mainnethttps://mainnet.infura.io/v3/{PROJECT_ID}
AlchemyEthereum Mainnethttps://eth-mainnet.g.alchemy.com/v2/{API_KEY}
AnkrEthereum Mainnethttps://rpc.ankr.com/eth
QuickNodeEthereum Mainnethttps://{QUICKNODE_ENDPOINT}.quiknode.pro/{TOKEN}
Public RPCEthereum Mainnethttps://ethereum.publicnode.com

Terminal window
# Get latest block number
curl -X POST https://mainnet.infura.io/v3/YOUR_PROJECT_ID \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1
}'
# Get block by number
curl -X POST https://mainnet.infura.io/v3/YOUR_PROJECT_ID \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_getBlockByNumber",
"params": ["0x10d4f1e", false],
"id": 1
}'
# Get account balance
curl -X POST https://mainnet.infura.io/v3/YOUR_PROJECT_ID \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_getBalance",
"params": ["0x742d35Cc6634C0532925a3b844Bc9e7595f0eB1E", "latest"],
"id": 1
}'
const { ethers } = require('ethers');
async function jsonRpcExample() {
// Connect to provider
const provider = new ethers.JsonRpcProvider(
'https://mainnet.infura.io/v3/YOUR_PROJECT_ID'
);
// Get latest block number
const blockNumber = await provider.getBlockNumber();
console.log('Latest block:', blockNumber);
// Get balance
const balance = await provider.getBalance(
'0x742d35Cc6634C0532925a3b844Bc9e7595f0eB1E'
);
console.log('Balance:', ethers.formatEther(balance), 'ETH');
// Get block data
const block = await provider.getBlock(blockNumber);
console.log('Block:', block);
// Call contract (read)
const contract = new ethers.Contract(
'0x...',
['function balanceOf(address) view returns (uint256)'],
provider
);
const tokenBalance = await contract.balanceOf('0x...');
}
from web3 import Web3
# Connect to provider
w3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/YOUR_PROJECT_ID'))
# Get latest block number
block_number = w3.eth.block_number
print(f'Latest block: {block_number}')
# Get balance
balance = w3.eth.get_balance('0x742d35Cc6634C0532925a3b844Bc9e7595f0eB1E')
print(f'Balance: {w3.from_wei(balance, "ether")} ETH')
# Get block
block = w3.eth.get_block('latest')
print(f'Block: {block}')
# Call contract
contract = w3.eth.contract(
address='0x...',
abi=[{'name': 'balanceOf', 'type': 'function',
'inputs': [{'name': 'owner', 'type': 'address'}],
'outputs': [{'type': 'uint256'}],
'stateMutability': 'view'}]
)
balance = contract.functions.balanceOf('0x...').call()

JSON-RPC supports batch requests for efficiency:

[
{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1
},
{
"jsonrpc": "2.0",
"method": "eth_getBalance",
"params": ["0x742d35Cc6634C0532925a3b844Bc9e7595f0eB1E", "latest"],
"id": 2
},
{
"jsonrpc": "2.0",
"method": "eth_gasPrice",
"params": [],
"id": 3
}
]

CodeErrorDescription
-32700Parse ErrorInvalid JSON
-32600Invalid RequestInvalid JSON-RPC request
-32601Method Not FoundUnknown method
-32602Invalid ParamsInvalid method parameters
-32603Internal ErrorInternal JSON-RPC error
-32000Execution ErrorEVM execution error

QuestionAnswer
What is JSON-RPC?Remote procedure call protocol using JSON encoding
What is eth_blockNumber used for?Returns the latest block number
How do you call a smart contract view function?Using eth_call method
How do you submit a signed transaction?Using eth_sendRawTransaction
What is the difference between eth_call and eth_sendTransaction?eth_call is read-only, eth_sendTransaction modifies state
What is a nonce?Transaction count from an address
How do you estimate gas?Using eth_estimateGas method

  • JSON-RPC is the standard protocol for Ethereum interaction
  • Methods include block, transaction, state, and contract operations
  • Supports HTTP and WebSocket connections
  • Batch requests can improve efficiency
  • Popular providers: Infura, Alchemy, Ankr

In Chapter 20: Setting Up Public RPC Endpoints, we’ll explore configuring public RPC services.


Last Updated: 2026-02-20