Cryptocurrency Portfolio Tracker with Python: Fetch Data From CoinMarketCap API

·

Introduction

Welcome to this tutorial on building a cryptocurrency portfolio tracker using Python. This guide will walk you through fetching real-time cryptocurrency data via the CoinMarketCap API, a foundational step for creating a custom portfolio management tool. By the end, you'll learn how to retrieve and process live price quotes efficiently.


Understanding the CoinMarketCap API

The CoinMarketCap API provides access to real-time and historical data for thousands of cryptocurrencies, including:

👉 Explore CoinMarketCap API documentation

API Setup Steps:

  1. Create an account on CoinMarketCap.com.
  2. Generate an API key under the "API" section.
  3. Store the key securely for future requests.

Fetching Data with Python

Required Packages:

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

Step 1: Configure API Headers

Use the requests.Session object to persist headers (including your API key):

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': 'YOUR_API_KEY',  
}  

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

Step 2: Make API Requests

Single Symbol Request:

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

btc_data = get_response('BTC')  

Multiple Symbols Request:

Convert a list of symbols to a comma-separated string:

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

Step 3: Process API Responses

Clean and Structure Data:

Convert raw JSON into a pandas DataFrame for analysis:

import pandas as pd  

def clean_response_multiple(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  

portfolio_data = clean_response_multiple(['BTC', 'ETH', 'SOL'])  

Key Data Columns:

print(portfolio_data.columns)  
# Output: ['price', 'volume_24h', 'market_cap', ...]  

FAQ Section

1. What’s the rate limit for CoinMarketCap’s free API?

The free tier allows 10,000 monthly calls, sufficient for small-scale projects.

2. Can I fetch historical data with this API?

The free version includes limited historical data (e.g., past 24 hours or 7 days).

3. How do I secure my API key?

Never expose it in client-side code; use environment variables or a secrets manager.

👉 Learn more about API best practices


Next Steps

In upcoming tutorials, we’ll:


Conclusion

You’ve now learned how to:

  1. Fetch live cryptocurrency prices via the CoinMarketCap API.
  2. Process and structure data using Python and pandas.
  3. Scale requests for multiple cryptocurrencies.

This forms the backbone of your cryptocurrency portfolio tracker. Stay tuned for the next part!