Production engineering guide for blockchain api design in Web3 and blockchain systems.
Blockchain API Design is the practice of crafting reliable, predictable, and performant interfaces to blockchain networks, enabling developers to interact with on-chain data and state. It matters when applications must query, submit, and react to blockchain events with consistency across diverse networks, clients, and client expectations. Whether building a wallet, a DeFi protocol, or a cross-chain dApp, the API is the contract between the blockchain and the application layer.
To read data from the blockchain, clients use RPC-style APIs to fetch blocks, transactions, contract states, and events. The primary pattern is a JSON-RPC over HTTP, where each request carries a method, parameters, and a unique identifier.
A typical request to get the latest block height:
POST /rpc
Content-Type: application/json
{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"id": 1
}
The response is always a standard JSON-RPC envelope:
{
"jsonrpc": "2.0",
"result": "0x1a2b3c",
"id": 1
}
The result is a hexadecimal string, commonly prefixed with 0x, and parsed using BigInt or uint64 types. A common failure occurs when clients assume result is a number — but 0x1a2b3c is not 1072412 in the JSON, and parsing it as a double loses precision.
To query a specific block by number or hash:
POST /rpc
{
"jsonrpc": "2.0",
"method": "eth_getBlockByNumber",
"params": [
"0x1a2b3c",
true
],
"id": 2
}
The second params argument, true, enables full transaction inclusion. When false, only transaction hashes are returned — a subtle but frequent source of mismatched expectations.
For contract state, the eth_getStorageAt method is used:
POST /rpc
{
"jsonrpc": "2.0",
"method": "eth_getStorageAt",
"params": [
"0x742d35Cc6634C0532925a3b8D4C05C6D4B5a8B83",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x1a2b3c"
],
"id": 3
}
The blockHash parameter can be latest, pending, earliest, or a hex string. Misinterpreting pending as latest leads to inconsistent state views across clients.
Submitting a transaction to the blockchain involves sending a signed transaction payload. The standard method is eth_sendTransaction, with a request body:
{
"jsonrpc": "2.0",
"method": "eth_sendTransaction",
"params": [
{
"from": "0x742d35Cc6634C0532925a3b8D4C05C6D4B5a8B83",
"to": "0x0000000000000000000000000000000000000000",
"value": "0x123456789abcde",
"gas": "0x5208",
"gasPrice": "0x12a05f200",
"data": "0x123456789abcde"
}
],
"id": 4
}
The value, gas, and gasPrice fields are hex-encoded integers. The data field is a 0x-prefixed ABI-encoded calldata. A common failure: using uint256 for value but sending 0x1234 instead of 0x000000000000000000000000000000000000000000000000000000000000001234.
The response returns a transaction hash:
{
"jsonrpc": "2.0",
"result": "0x742d35Cc6634C0532925a3b8D4C05C6D4B5a8B83123456789abcde",
"id": 4
}
Clients must poll for confirmation using eth_getTransactionReceipt:
POST /rpc
{
"jsonrpc": "2.0",
"method": "eth_getTransactionReceipt",
"params": [
"0x742d35Cc6634C0532925a3b8D4C05C6D4B5a8B83123456789abcde"
],
"id": 5
}
The receipt includes status, cumulativeGasUsed, and logs. The status field is a 0x0 for failure and 0x1 for success. A silent failure: clients assume status is boolean, but 0x00 is not false in JSON.
To react to on-chain events, clients use eth_getLogs to fetch past events, or eth_subscribe for real-time streaming.
A query for a specific event from a contract:
{
"jsonrpc": "2.0",
"method": "eth_getLogs",
"params": [
{
"fromBlock": "0x1a2b3c",
"toBlock": "0x1a2b40",
"address": "0x742d35Cc6634C0532925a3b8D4C05C6D4B5a8B83",
"topics": [
"0x123456789abcde123456789abcde123456789abcde123456789abcde12345678"
]
}
],
"id": 6
}
The topics array is an ordered list of event topic filters. A common failure: assuming topics[0] is the event signature, but topics[0] is the event signature *only* if address is not provided. If address is set, topics[0] is the first indexed parameter.
For real-time event streaming, eth_subscribe returns a subscription ID:
{
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": [
"logs",
{
"address": "0x742d35Cc6634C0532925a3b8D4C05C6D4B5a8B83",
"topics": [
"0x123456789abcde123456789abcde123456789abcde123456789abcde12345678"
]
}
],
"id": 7
}
The server returns a subscription ID:
{
"jsonrpc": "2.0",
"result": "0x1a2b3c4d5e",
"id": 7
}
The client then receives events as push notifications over a WebSocket:
{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription": "0x1a2b3c4d5e",
"result": {
"removed": false,
"logIndex": "0x1",
"transactionIndex": "0x2",
"transactionHash": "0x742d35Cc6634C0532925a3b8D4C05C6D4B5a8B83123456789abcde",
"blockHash": "0x1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3c",
"blockNumber": "0x1a2b3c",
"address": "0x742d35Cc6634C0532925a3b8D4C05C6D4B5a8B83",
"data": "0x123456789abcde",
"topics": [
"0x123456789abcde123456789abcde123456789abcde123456789abcde12345678"
]
}
}
}
A subtle but common failure: clients expect blockNumber to be 0x1a2b3c as a string, but eth_getBlockByNumber returns 0x1a2b3c as 0x1a2b3c, and eth_getLogs returns blockNumber as 0x1a2b3c — both are correct, but the client must parse them consistently.
When submitting transactions, clients must handle a pipeline of states: pending, confirmed, and final.
The eth_getTransactionByHash method returns a transaction object with status and blockHash:
{
"jsonrpc": "2.0",
"result": {
"hash": "0x742d35Cc6634C0532925a3b8D4C05C6D4B5a8B83123456789abcde",
"nonce": "0x1",
"blockHash": "0x1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3c",
"blockNumber": "0x1a2b3c",
"from": "0x742d35Cc6634C0532925a3b8D4C05C6D4B5a8B83",
"to": "0x0000000000000000000000000000000000000000",
"value": "0x123456789abcde",
"gasPrice": "0x12a05f200",
"gas": "0x5208",
"input": "0x123456789abcde",
"receipt": {
"status": "0x1",
"cumulativeGasUsed": "0x123456",
"logs": []
}
},
"id": 8
}
A missing blockHash implies the transaction is still pending. A null receipt indicates the transaction was submitted but not yet mined.
Clients must also handle eth_getTransactionReceipt failing to return a receipt, often due to a slow or failing node. A common error response:
{
"jsonrpc": "2.0",
"error": {
"code": -32602,
"message": "Invalid params",
"data": {
"method": "eth_getTransactionReceipt",
"params": [
"0x742d35Cc6634C0532925a3b8D4C05C6D4B5a8B83123456789abcde"
]
}
},
"id": 9
}
The error.code is a standard Ethereum error code. A code of -32602 means Invalid params. A code of -32000 means Internal error.
API clients must configure their node connections, including connection pooling, retry strategies, and fallbacks.
A typical client configuration in JSON:
{
"chains": {
"mainnet": {
"url": "https://mainnet.infura.io/v3/abc123",
"timeout": 10000,
"retries": 3,
"backoff": "exponential",
"maxRetries": 5,
"fallbacks": [
"https://cloudflare-eth.com",
"https://eth-mainnet.nodereal.io"
],
"auth": {
"type": "bearer",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
},
"batching": {
"enabled": true,
"maxRequests": 10,
"timeout": 500
}
}
}
}
The batching configuration enables a common optimization: multiple RPC calls are bundled into a single HTTP request:
[
{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"id": 1
},
{
"jsonrpc": "2.0",
"method": "eth_getBalance",
"params": [
"0x742d35Cc6634C0532925a3b8D4C05C6D4B5a8B83",
"latest"
],
"id": 2
}
]
The server responds with an array of results:
[
{
"jsonrpc": "2.0",
"result": "0x1a2b3c",
"id": 1
},
{
"jsonrpc": "2.0",
"result": "0x123456789abcde",
"id": 2
}
]
A failure: clients assume batching is always enabled, but the server returns null for eth_getBalance when the toBlock parameter is missing — a silent error.
latest, pending, and earliest are not equivalent. pending includes transactions not yet mined, while latest includes the most recently confirmed block.0x1234 but expect 1234 in uint256. A 0x1234 is 4660 in decimal, not 1234.eth_getLogs returns logs as an array, but eth_subscribe returns result.logs as a list of log objects, each with topics as an array. Confusing topics with log.topics leads to missing event data.status: "0x1" is true, but status: "0x0" is not false — it is 0x0, which is 0 in uint256, but 0 in boolean is false.timeout on the batch — leading to delayed responses when the first request arrives but the batch is not complete.fallbacks are not used when the primary URL is unreachable, but clients often do not implement retry logic on timeout or networkError.A well-designed blockchain API is predictable, consistent, and resilient. It uses standard JSON-RPC over HTTP with well-documented method parameters, error codes, and response shapes. It supports batching, subscription, and fallback strategies. Its failures are visible and actionable, and its subtle edges — like hex-encoded integers, event topic indexing, and transaction status codes — are not assumed but explicitly handled. The API is not just a contract; it is the foundation of the blockchain application.
This page was rewritten on 18 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy