Python Coin Gecko – Get cryptocurrency volume

In this post we will be creating a Python script that will retrieve the 24 hour volume for a given cryptocurrency via 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 24 hour volume for a given cryptocurrency. In this example we retrieve the volume for Bitcoin in the US Dollar currency.

import requests

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

crypto_id = 'bitcoin'
fiat_currency = 'usd'
A24h_volume = get_24h_volume(crypto_id, fiat_currency)
print(f'The current 24-hour volume of {crypto_id} in {fiat_currency} is ${A24h_volume:,.2f}')

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

The current 24-hour volume of bitcoin in usd is $42,492,109,328.00

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

Leave a Reply