Node.js theorytheory 0/50 · 0%
Web3 integration · medium

30. Interacting with blockchain nodes over JSON-RPC

How a Node backend talks to an Ethereum-style node.

Ethereum-compatible nodes expose a JSON-RPC API over HTTP or WebSocket. Every call sends `{ jsonrpc: "2.0", id, method, params }` and gets back `{ jsonrpc: "2.0", id, result }` or an `error` object.

js
async function rpcCall(url, method, params = []) {
  const res = await fetch(url, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
  });
  const json = await res.json();
  if (json.error) throw new Error(json.error.message);
  return json.result;
}

const blockHex = await rpcCall(process.env.RPC_URL, "eth_blockNumber");
console.log(parseInt(blockHex, 16));

Libraries like ethers.js and viem wrap this pattern with typed helpers, retries, batching and ABI encoding/decoding — but understanding the raw JSON-RPC exchange helps you debug when those libraries surface a confusing error.

Check your understanding

  1. 1. What shape does a JSON-RPC request take?

  2. 2. How is an RPC error typically reported?

  3. 3. What do libraries like ethers.js/viem add over raw fetch calls?