Using etherscan-python library with Etherscan.io API to access Ethereum data

Steven Kyle
4 min readOct 31, 2021
Photo by Executium on Unsplash

Recently I have come across the need to gather information from the Ethereum blockchain. The simplest way I have found to access the information is by using etherscan’s API with the etherscan-python library. In this blog post I’ll introduce the library and how we can use it with the etherscan API to access Ethereum data.

Installing the library

The installation of etherscan-python can be quite simple. We can use pip to install the library. The following command can be entered into the command prompt. The documentation for the library can be found here.

pip install etherscan-python

API Key

To interact with etherscan an API key has to be created. A free account can be made at https://etherscan.io/. Once the account is created an API key can be requested. REMEMBER: API keys should always be hidden, make sure that you do not accidentally give out your API keys/secrets.

Creating a client

Once you have the library installed and an API key we have all the necessary tools to access ethereum data. First we will load the library into python by importing Etherscan from etherscan.

Once the library is important we can use our API key to create a client. Your personal API key will go where “ether_api_key” is.

The client is now “eth” and we can now call upon methods with the client to access information.

Accessing Information

There is plethora of methods that can be used to access information from the client. Some of the methods are limited to “Pro” accounts that pay for an API key, however there is still a lot of methods that can be used with the free account. A list of methods can be found here.

Now I will show case some useful methods that can be used.

get_eth_balance
One reason you’re probably looking to access ethereum information is probably to get specific wallet balances. This method can be used to receive the eth balance of a single account. For this example I will use a random wallet to get info on.

The reason we have to divide the eth_balance is because the original output unit is in wei and we have to convert it to eth.

get_eth_balance_multiple
This method is the same as above but instead of getting info for one address, it will give you the eth balance of multiple addresses. This method is very useful when trying to access multiple wallet balances at one time. A list of wallet addresses has to be passed into the method, an example can be seen below.

Remember, the reported balance is still in wei and has to be converted to eth.

get_eth_last_price
This method can be used to access the latest price of eth. This will return the price of eth compared to bitcoin as well as the price of eth compared to USD. An example can be seen below.

get_acc_balance_by_token_and_contract_address
This last method I will talk about will let you access ERC-20 token balances. To access the balance of an ERC-20 token, the wallet address as well as the ERC-20 contract address has to be known. By putting both addresses into the method, the balance for that ERC-20 token will be returned. The first address we used also has Tether USD, so we will need to input the Tether USD contract address in. The example can be seen down below.

The reported USDT still needs to be converted (moving the decimal place over 6 places to the left) since the reported amount is shown in the lowest unit.

Conclusion

Most of the methods I introduced were just to get account data for a specific wallet. There are many other methods in the etherscan-python library that is useful for getting ethereum data. I encourage you to explore the documentation that can be found here and here.

--

--

Steven Kyle

25 year old Texan in the midst of a career change into DataScience.