JSON-RPC
Learn how the JSON-RPC API works in Ethereum.
We'll cover the following
In the previous lesson, we learned about the role of Ethereum clients and how network users can use an API provided by clients to interact with an Ethereum network. In this lesson, we'll learn what we can do with this API and how it works.
JSON-RPC
An API provided by Ethereum clients is structured around a set of methods we can call. To call a particular method, we need to send a JSON object with the method name and parameters to a client and wait for a response. This API style is called JSON-RPC, where RPC stands for Remote Procedure Call.
The Ethereum API provides various methods we can call, and we can find a comprehensive list in the official documentation. Here are some of the methods and a brief overview of what we can do with them:
web3_clientVersion
: This is to get the version of the Ethereum client we're interacting with.net_peerCount
: This is to get the current number of network peers a client is connected to on the node we're using.eth_getBlockByHash
: This is to get information about a past block by its hash.eth_getTransactionByBlockNumberAndIndex
: This is to get information about a transaction from a particular block.eth_call
: This is to execute a method using the call mechanism without sending an Ethereum transaction.eth_getBalance
: This is to get the Ether balance of a particular account.
Notice that all method names start with a prefix like eth_
. This is because all functions are grouped by their functionality, and a prefix specifies the group’s name.
Sample request
To get an idea of what a method call in the Ethereum API looks like, here's an example of a JSON payload we need to send to call a method. In this case, we call the eth_getTransactionByBlockNumberAndIndex
method that returns a selected transaction from a particular block. To get a transaction, we need to provide the position of the block in the chain of blocks and the position of a transaction in the block to return.
Get hands-on with 1200+ tech skills courses.