Python Coin Gecko – Get crypto market capitalization

In this post we will be creating a Python script that will retrieve the market capitalization for a given cryptocurrency using the free Coin Gecko API. To do this we will be using the Python ‘Requests’ library which makes it super easy to send HTTP requests.

See the sample of Python code below where we utilize the Coin Gecko API to retrieve the market capitalization for a given cryptocurrency. In this example we retrieve the market cap for Bitcoin in the US Dollar currency.

import requests

def get_crypto_info(crypto_id, fiat_currency):
    url = f'{crypto_id}'
    response = requests.get(url)
    data = response.json()
    market_cap = data['market_data']['market_cap'][fiat_currency]
    return market_cap

crypto_id = 'bitcoin'
fiat_currency = 'usd'
market_cap = get_crypto_info(crypto_id, fiat_currency)
print(f'The current market capitalization of {crypto_id} in {fiat_currency} is ${market_cap:,.2f}')

The above would return the following as output. Results are based on the time and date of writing this post.

The current market capitalization of bitcoin in usd is $349,302,876,778.00

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply