Building a Cryptocurrency Portfolio Tracker with Python

Part 1: Fetch Data From the CoinMarketCap API

Eric Flynn
5 min readMay 14, 2024
Photo from Kanchanara on Unsplash

Introduction

Welcome to the first part of my tutorial series on building a cryptocurrency portfolio tracker with Python. In this series, I will guide you through the process of creating your own custom portfolio application, enabling you to fetch real time price quotes and track your portfolio performance all within Python.

In this article, I will kick off the series by explaining how to fetch live cryptocurrency data using the CoinMarketCap API. By the end of this tutorial, you’ll have a foundational understanding of how to retrieve real-time prices in a structured and repeatable manner which will later be used to form the backend for the application. Let’s dive in!

CoinMarketCap API

The CoinMarketCap API is a powerful tool that allows developers to retrieve real-time and historical data on thousands of cryptocurrencies, including price, market cap, trading volume, and much more. With its user-friendly documentation [1] and robust features, the CoinMarketCap API is widely used by developers throughout the cryptocurrency community.

For this project, I will be using the free edition of the API which allows for 10,000 calls per month. This version does not have full historical information, but it does maintain data on the last month, week, and 24 hours.

The first step is to setup an account on CoinMarketCap.com. Once created, navigate to the API section on the top right above the search bar.

Then, you should see a screen similar to below. Under API key, there will be an option to generate a key. Click this and then store your key somewhere safe. (This look slightly different from mine since I’ve already created my key)

Making Requests

Now that we are setup with the API and have obtained a key, we are ready to start pulling data. The API has eight endpoints and four endpoint paths available.

For this project we will be using the following endpoint.

https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest

Python Packages

Before diving in and starting to make requests, it’s important to spend some time setting up an appropriate development environment. For this, I will use conda, but any Python virtual environment manager will do. You will need to install the the pandas and requests packages. Here’s the versions I am using.

python==3.9.7
pandas==1.4.2
requests==2.26.0

Request Headers

For this project, I am using the Session object from requests. This object allows you to store a persistent header configuration for repeated API requests. A dictionary of headers containing the API key previously generated is passed into the Session object and stored, so any subsequent request made with this session variable will have the API key embedded.

from requests import Session
import json

url = 'https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest'

headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': '<ENTER API KEY HERE>',
}

session = Session()
session.headers.update(headers)

A Simple Request

Now that the Session is configured, we are ready to pull some actual data. For this, I am creating a dictionary called parameters that then gets passed into session.get() specifying which crypto symbol to fetch data for. The url is the path previously defined which points to the /quotes/latest path. This data is then finally loaded from json into a dictionary.

def get_response(symbol):
parameters = {
'symbol': symbol
}
response = session.get(url, params=parameters)
return json.loads(response.text)

get_response('BTC')

You should have gotten a big blob of text like below.

Data Processing

It’s great that we’ve gotten a request back, but if you look through this blob you’ll see there’s only some data that’s useful. Lets create a function to extract the info we need.

This function will decompose the dictionary and strip away the useless information until it reaches what we want. Once there, it will convert the dictionary into a DataFrame.

def clean_response(symbol):
data = get_response(symbol)
new_dict = data['data'][symbol][0]['quote']['USD']
new_dict['symbol'] = symbol
df = pd.DataFrame(new_dict, index=[0])
df.set_index('symbol', inplace=True)
return df

df = clean_response('BTC')

We can view all the columns with:

df.columns

Request Multiple Symbols

Now that we know how to request quotes for a single crypto, its time to scale up this process to request quotes for multiple symbols at a time. The first thing we need to change is the structure of the request. The api requires a string of comma separated strings *(not a list), which we can create from a list with:

symbols = ['BTC','ETH','SOL']
print(','.join(symbols))

This can now be integrated into the previous request function pretty easily.

def get_response_multiple(symbols):
parameters = {
'symbol': ','.join(symbols)
}
response = session.get(url, params=parameters)
return json.loads(response.text)

get_response_multiple(['BTC','ETH','SOL'])

If we now try to request data for the list of the three symbols above, then we will get a long blob like before, this time repeating for each symbol. Lets modify the cleaning function to process data returned from multiple symbols as well.

Below, I use list comprehension to loop through each symbol being requested and restructure the response dictionary to include an entry for the symbol. This entire list of dictionaries is then converted into a DataFrame.

def clean_response_multiple_2(symbols):
data = get_response_multiple(symbols)
df = pd.DataFrame(
[{'symbol': symbol, **data['data'][symbol][0]['quote']['USD']} for symbol in symbols]
).set_index('symbol')
return df

symbols = ['BTC','ETH','SOL']
df = clean_response_multiple(symbols)

Conclusion

In this article we reviewed the basics of the CoinMarketCap API, and how to fetch and process data for multiple cryptocurrencies in a single request. This will serve as the foundation for the backend of our portfolio tracker application moving forward.

Stay tuned and don’t forget to follow so you don’t miss the next article in the series, where I will be using Streamlit and SQLite to create a CRUD application allowing end-users to store their portfolio information. Thanks for reading, and as always please comment if you have any questions or ideas for how to do things more efficiently.

--

--

Eric Flynn

Analytics Engineer | Spreading knowledge and positivity.