In this short tutorial, we will explain how to connect to the Anyblock JSON-RPC Node API Access and use it in Python with the help of web3.py to interact with the Ethereum network. Furthermore, first simple use-cases will be shown.
The Anyblock Node API grants access to both Bitcoin- and Ethereum-based networks like Litecoin, BSC, and xDai using a JSON-RPC API. It relieves you of running a custom node for each chain, as this is handled by Anyblock instead. And archive data, as well as Ethereum block and transaction traces, are also included. Get started for free now!
What does RPC stand for?
RPC stands for Remote Procedure Call. This means that one client executes a procedure or function on another system. In other words, it is a client-server interaction where the client tells the server the procedure to be executed, the server then executes the procedure and sends a response to the client. There are different protocols for RPCs. The Anyblock Node API is a JSON-RPC interface. This means that both the requests and responses are serialized and returned in (the) JSON format.
What is web3.py used for?
web3.py is a Python library that allows users to interact with the Ethereum blockchain. The library makes it possible to develop decentralized applications or to analyze the activities on the blockchain. Furthermore, web3.py enables us to interact with smart contracts and to send transactions. All this is possible because web3.py can connect to nodes (e.g. the ones operated by Anyblock Analytics as the basis for the JSON-RPC Node API). These nodes are active on Ethereum and are responsible for storing the blockchain data. This allows us to query the network and for example get the transaction, block, or event data.
How to use web3.py with Anyblock JSON-RPC Node API
Image: Anyblock account login interface. Click to enlarge.
After account creation or login, navigate to the RPC tab in your profile. There you will find all the RPC endpoints for the different networks expanded by your personal API key. In this example, we are working with the Ethereum Mainnet. Therefore, the following URL should be copied.
Image: URL to copy under the Anyblock Account RPC tab. Click to enlarge.
If Python is not installed on your system yet, it is necessary to install it either directly via the command line or to download the installation file from the web. Subsequently, it is necessary to install the Python package manager pip. Best, open a command line and execute the following command:
python3 -m pip install --user --upgrade pip
It is always recommended to create new projects in virtual environments. This way the packages can be managed separately, you can create an isolated Python installation and you do not influence the system Python interpreter. Using virtual environments requires the installation of the “virtualenv” package (for further documentation, visit this tutorial).
python3 -m pip install --user virtualenv
Image: Create virtual environment command line. Click to enlarge.
Next, we have to create a virtual environment. Therefore we first have to create a new project directory in the user folder:
After creating the directory, we need to change to this folder and create a virtual environment.
python3 -m venv env
Now the environment can be started:
Please note that in Windows the path is different. Here is how it looks in CMD:
In the following step, we install the web3.py package with pip. This allows it to be called and used in Python applications and scripts.
pip install web3
Now we are ready to use the Anyblock JSON-RPC Node API with web3.py. For this purpose, it is necessary to create a Python script in your text or code editor of choice. We use Vim.
from web3 import Web3 web3 = Web3(Web3.HTTPProvider('https://api.anyblock.tools/ethereum/ethereum/mainnet/rpc/YOUR-API-KEY-HERE/')) print("Client Connected?: ", web3.isConnected())
The last line shows whether we have successfully connected to the RPC provider, or not. If so, True is output here, otherwise False. We now have successfully connected to the Anyblock JSON-RPC Node API.
To run the script in the command line, change to its directory and type:
Image: Connecting to the Anyblock JSON-RPC Node API. Click to enlarge.
Information about the latest block, or any other, can be obtained with method web3.eth.get_block(). The output is presented in a Python dictionary (JSON).
# Latest Block print("Information about the latest Block: " , web3.eth.get_block('latest'))
# Transaction Information print("Information about Transaction : " , web3.eth.get_transaction(“https://etherscan.io/tx/0x111eab00a47cfdba2271e65b04522543fd1cb97fcc660a85cc96781a425b443f”))
Image: Transaction information. Click to enlarge.
Let’s look at a more detailed example. This is about the POAP token. In this example, we will interact with a smart contract.
First, the ABI (Application Binary Interface) of the POAP token is retrieved using the etherscan API and stored in a variable.
Then a contract instance is created using method web3.eth.contract(address= POAP_CONTRACT, abi = ABI).
Finally, one of the ERC-721 functions is called for the POAP contract instance. With the help of function “tokenURI” and the passing of a token ID, the URI can be found out as well as where the metadata of this particular NFT is stored.
Again here, replace YOUR-API-KEY-HERE with the API key from your account page.
from web3 import Web3 import urllib.request import json web3 = Web3(Web3.HTTPProvider('https://api.anyblock.tools/ethereum/ethereum/mainnet/rpc/YOUR_API_KEY')) print("Client Connected?: ", web3.isConnected()) # load the ABI response = urllib.request.urlopen("http://api.etherscan.io/api?module=contract&action=getabi&address=0x96d0ec5abb022ebf3a269344b2593ed909a77948&format=raw") ABI = json.loads(response.read()) # Loading the Application Binary Interface # POAP Contract which has tokenURI Method POAP_CONTRACT = "0x22C1f6050E56d2876009903609a2cC3fEf83B415" tokenInst = web3.eth.contract(address=POAP_CONTRACT, abi=ABI) print("Token Metadata for Token 437283: " , tokenInst.functions.tokenURI(437283).call()) # Calling the defined method
Image: Retrieving POAP token metadata. Click to enlarge.
Looking to create an Anyblock account?
Takes seconds and it's free!