Interacting with an Ethereum node using APIs
By Sumi Maria Abraham, Research and Development Engineer, Kerala Blockchain Academy
Hey Everyone; in the previous article, you saw how to run an Ethereum node. If you have missed the reading piece, do check it out here.
In this blog, I’ll show you how to interact with the up-and-running node. If reading feels like a chore, don’t worry, I’ve got you covered. Check out our video tutorial at https://youtu.be/ZcmVOczxxzQ.
Let us begin.
We assume your node is up and the execution and consensus clients are running.
To interact with the clients, we have a set of functions called API associated with the client.
What is an API?
An application programming interface (API) is a program code that enables two software to communicate. An API defines how to request services from an application and disclose data.
It takes the request from the user and sends it to the service provider, and then again sends the result generated from the service provider to the user. API consists of a specification that describes how data is exchanged between programs in the form of a request and a return of the necessary data. It will also have a software interface written per the specification and published. APIs help developers implement sophisticated features without writing complex codes.
We have APIs associated with the execution client (Besu) and the consensus client (Lighthouse) here. We will access the APIs via a VS Code extension.
APIs can be tested in command line or using tools like postman also.
Besu API: https://besu.hyperledger.org/en/stable/assets/postman/postman_collection.json
Lighthouse API: https://ethereum.github.io/beacon-APIs/releases/v2.4.0/beacon-node-oapi.json
- Install the Thunder Client VS Code extension.
This extension helps you use APIs to create requests for Besu and Lighthouse clients. The response received will also be conveyed.
2. Open the extension, select collections, click on the options bar next to the filter option and select import from URL. Paste the Besu API link and press enter.
3. Similarly, import the beacon node API also.
4. First, let’s invoke a function to check the syncing status of the execution client. Under ETH, click on eth_syncing. Its request format will appear on the right.
Edit the IP address and port number to the IP address and port number of your execution client.
Give 127.0.0.1 or localhost as IP address if you are running API calls and clients on the same machine.
Click on send to send the request to besu. Once the request is complete, the result will appear on the right. It will include the block syncing status. Please note that the block numbers are hex; you may convert them to decimal format using any online converter.
There is another function to check the block details.
The API will connect with your client and retrieve the details. When you provide a block number, ensure the given block number is already synced.
You may cross-check the details with those available from etherscan or any other online block explorer.
5. Let us try some functions in Beacon Chain API.
To interact with the lighthouse, you need the URL and port number. From the client console, note the port number of your client.
Now open the beacon API and click on the function to get syncing details.
Update the IP address and port and send the request. The syncing details will be displayed on the right. If the node has completed syncing, the syncing status will be false.
Now you may try out other functions in a similar way.
APIs are standard definitions. Usually, it will be consistent across the clients. You may use the besu collection & beacon API collection for interacting with any execution or consensus clients, provided the IP address and port numbers are correct.