Utilizing the Covalent API with Python and Jupyter Notebook.

adam.xyz
Covalent
Published in
2 min readMar 14, 2021

Hello everyone! This past month, I was lucky enough to be selected into the Covalent Alchemists Program. As such, I will be creating a series of developer-focused articles on how to best utilize the Covalent API.

The first step is to head over to https://www.covalenthq.com/ and get a free API key. Once that is done, we can jump into some code.

The first thing we want to do is import the necessary libraries. Since we are using Python, we need to import requests, JSON, and Pandas for data manipulation.

import requests
import json
import pandas as pd

Depending on the type of data you are looking for, the endpoint you are utilizing will differ. For this tutorial, I will analyze the popular Ethereum address 0xb1’s activity on compound.finance.

Now, make a get request utilizing the Covalent API endpoint for Compound addresses, and the Python requests library. Replace “YOUR_KEY_HERE” with the API key you received:

result = requests.get("https://api.covalenthq.com/v1/1/address/0xB1AdceddB2941033a090dD166a462fe1c2029484/stacks/compound/acts/?key=YOUR_KEY_HERE")result

If the requests go through correctly, you’ll receive a 200 response after running the code above. If you receive a 404 error, double-check that you are calling the correct endpoint.

The next step is to format the result into a JSON so that we can view it as a first pass:

result = result.json()
result
{'data': {'address': '0xb1adceddb2941033a090dd166a462fe1c2029484',
'updated_at': '2021-03-14T19:50:53.407207391Z',
'next_update_at': '2021-03-14T19:55:53.407208758Z',
'quote_currency': 'USD',
'chain_id': 1,
'items': [{'act_at': '2021-03-14T13:54:10Z',
'tx_hash': '0xd9828955b60513be7c91bc541d5495204a09f3bb01363f397a4683f4c2033db6',
'act': 'BORROW',
'description': 'Borrowed 2500000.00 USDC',
'amount': '2500000000000',
'ctoken_amount': None,
'contract_address': '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
'contract_logo_url': 'https://logos.covalenthq.com/tokens/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.png',
'contract_decimals': 6,
'ctoken_contract_decimals': 0,
'contract_ticker_symbol': 'USDC',
'ctoken_contract_ticker_symbol': '-',
'quote_rate': 0.9998834,
'quote': 2499708.5,
'successful': True,
'gas_offered': 542020,
'gas_spent': 439291,
'gas_price': 145000000000,
'gas_quote': 119.38957}

The response is quite long, so I have included a snippet of it above. We can see that our call is working correctly, and we have a ton of different data points that we can utilize based on a single API call. Amazing. Now, we need to clean up the response a bit more, and put it into a Pandas DataFrame.

result = result['data']['items']
df = pd.DataFrame(result)
df = df.set_index('act_at')

Running the above code will help filter the JSON down into a useable format, place the JSON into a Panda’s data frame, and set the index of the data frame to ‘act_at’ which is the time of the transaction.

From here, the possibilities are endless. If you want to export the data frame into a CSV file, simply run:

df.to_csv('covalent_api_example.csv')

Stay tuned for the next article, where I will show how to query for NFTs.

--

--