Json_rpc
Chapter 19: JSON-RPC API Methods
Section titled “Chapter 19: JSON-RPC API Methods”Overview
Section titled “Overview”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.
19.1 What is JSON-RPC?
Section titled “19.1 What is JSON-RPC?”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
Request Format
Section titled “Request Format”{ "jsonrpc": "2.0", "method": "method_name", "params": ["param1", "param2"], "id": 1}Response Format
Section titled “Response Format”{ "jsonrpc": "2.0", "result": "returned_value", "id": 1}Error Response
Section titled “Error Response”{ "jsonrpc": "2.0", "error": { "code": -32600, "message": "Invalid Request" }, "id": 1}19.2 Standard Ethereum JSON-RPC Methods
Section titled “19.2 Standard Ethereum JSON-RPC Methods”Block Information
Section titled “Block Information”┌─────────────────────────────────────────────────────────────────┐│ 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
Section titled “Transaction Methods”┌─────────────────────────────────────────────────────────────────┐│ 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 & Account Methods
Section titled “State & Account Methods”┌─────────────────────────────────────────────────────────────────┐│ 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 Interaction
Section titled “Contract Interaction”┌─────────────────────────────────────────────────────────────────┐│ 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 │ ││ └───────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────┘19.3 Popular JSON-RPC Providers
Section titled “19.3 Popular JSON-RPC Providers”| Provider | Network | URL |
|---|---|---|
| Infura | Ethereum Mainnet | https://mainnet.infura.io/v3/{PROJECT_ID} |
| Alchemy | Ethereum Mainnet | https://eth-mainnet.g.alchemy.com/v2/{API_KEY} |
| Ankr | Ethereum Mainnet | https://rpc.ankr.com/eth |
| QuickNode | Ethereum Mainnet | https://{QUICKNODE_ENDPOINT}.quiknode.pro/{TOKEN} |
| Public RPC | Ethereum Mainnet | https://ethereum.publicnode.com |
19.4 Making JSON-RPC Calls
Section titled “19.4 Making JSON-RPC Calls”Using cURL
Section titled “Using cURL”# Get latest block numbercurl -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 numbercurl -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 balancecurl -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 }'Using JavaScript (ethers.js)
Section titled “Using JavaScript (ethers.js)”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...');}Using Python (web3.py)
Section titled “Using Python (web3.py)”from web3 import Web3
# Connect to providerw3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/YOUR_PROJECT_ID'))
# Get latest block numberblock_number = w3.eth.block_numberprint(f'Latest block: {block_number}')
# Get balancebalance = w3.eth.get_balance('0x742d35Cc6634C0532925a3b844Bc9e7595f0eB1E')print(f'Balance: {w3.from_wei(balance, "ether")} ETH')
# Get blockblock = w3.eth.get_block('latest')print(f'Block: {block}')
# Call contractcontract = 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()19.5 Batch Requests
Section titled “19.5 Batch Requests”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 }]19.6 Error Codes
Section titled “19.6 Error Codes”| Code | Error | Description |
|---|---|---|
| -32700 | Parse Error | Invalid JSON |
| -32600 | Invalid Request | Invalid JSON-RPC request |
| -32601 | Method Not Found | Unknown method |
| -32602 | Invalid Params | Invalid method parameters |
| -32603 | Internal Error | Internal JSON-RPC error |
| -32000 | Execution Error | EVM execution error |
19.7 Interview Questions
Section titled “19.7 Interview Questions”| Question | Answer |
|---|---|
| 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 |
Summary
Section titled “Summary”- 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
Next Chapter
Section titled “Next Chapter”In Chapter 20: Setting Up Public RPC Endpoints, we’ll explore configuring public RPC services.
Last Updated: 2026-02-20