bam4564 / coingecko_py

An Advanced Python API Client For The Coingecko API
MIT License
20 stars 0 forks source link

coingecko_py

Api Version Api Updated Tests Coverage

PyPi Version GitHub

An Advanced API Client For The Coingecko API

The base api client class and its documentation are automatically generated with swagger-codegen from the OpenAPI specification available here on the coingecko website.

The documentation for the api client can be found here.

This ensures that all endpoints and their corresponding parameters are 100% correct. Furthermore, the "Client Updated" badge you see at the top of this README is a live check that the spec used to generate the client code matches the latest version of the spec available on the coingecko website. This badge is updated once a day as a part of the CICD pipeline.

Additionally, the base api client has been extended to provide additional functionality like

Outline

Installation

Usage

API Reference

Advanced Features - Mitigate Rate Limiting

Advanced Features - Page Range Queries

Client Configuration

Summary

Development and Testing

License

Installation

This package is currently only available through PyPI. You can install it by running

pip install coingecko_py

Usage

This package exposes a single class called CoingeckoApi. To import and initialize this class, do the following

from coingecko_py import CoingeckoApi
cg = CoingeckoApi()

Check out the API Reference for more details on how to use this object.

Advanced Features

This section includes usage examples for advanced features that have been added to the base api client.

Advanced Features - Mitigate Rate Limiting

Note: This functionality is available for all endpoints available on the base client.

Imagine you wanted to get price data for the last year on the top 1000 market cap coins.

First, we get the data for the top 1000 market cap coins. Each page returns 100 results and pages are already sorted by market cap.

# np.ravel flattens a list of lists
import numpy as np 
coins = np.ravel([cg.coins_markets_get('usd', page=i) for i in range(1, 11)])

Next, we iterate over coins and use each coin id to query for it's price data.

ndays = 365
prices = dict()
for c in coins: 
    cid = c['id']
    prices[cid] = cg.coins_id_market_chart_get(cid, 'usd', ndays)['prices']

The issue here is that the coingecko api performs server side rate limiting. If you are using the free tier, it's about 50 api calls per second. Paid tiers have higher limits, but there is still a limit.

Since the above code block would be sending 1000 api requests synchronously, it is likely to fail at some point if you have a decent internet connection. In order to get around this, you would have to add error detection and call management logic. If you are writing a complex app with many api calls, this can be really annoying.

The coingecko_py client introduces a mechanism to queue api calls and execute a series of queued calls while performing client side exponential backoff retries. See here for an explanation of this strategy.

This allows you to write code without worrying about rate limiting! Here is a block of code that is equivalent to the above code block that won't error out due to rate limiting.

ndays = 365
for c in coins: 
    cid = c['id']
    cg.coins_id_market_chart_get(cid, 'usd', ndays, qid=cid)
prices = cg.execute_queued()
prices = {k: v['prices'] for k, v in prices.items()}

The key differences here are

These two blocks of code both produce a dictionary prices with the same exact structure (assuming the first code block doesn't error out because of rate limiting).

prices = {
    'bitcoin': {
        'prices': [
            [1610236800000, 40296.5290038294],
            [1610323200000, 38397.895985418174],
            [1610409600000, 35669.90668663349],
            ...
        ]
    }, 
    'ethereum': {
        'prices': [
            [1610236800000, 1282.979575527323],
            [1610323200000, 1267.7310031512136],
            [1610409600000, 1092.9143378806064],
            ...
        ]
    },
    ...
} 

Advanced Features - Page Range Queries

Note: This functionality is available for all endpoints the base client that support paging.

The coingecko api has a number of endpoints that support pagination. Pagination is a common api feature where you can request a specific page of data from an api. This is often necessary as some data objects are too large to return in a single api response. If you want all the data for a particular api call you are executing, you must request data from all pages.

Here is an example that uses the client to query for a single page of data

cg.coins_id_tickers_get('bitcoin', page=2, per_page=50)

Page range queries allow you to request a range of pages in a single client call. The api client supports bounded and unbounded page range queries.

For the code blocks below, let's assume we magically know there are 100 data pages for the coins_id_tickers_get endpoint for the given set of parameters. In reality, if you wanted to determine the number of data pages for a client call, you would need to make the call, inspect the HTTP headers, perform a calculation to determine the total number of pages, then loop over the page range and make a request per each page.

Here is an example of doing pagination manually using the base api client functionality

data = []
for i in range(1, 101):
    res = cg.coins_id_tickers_get('bitcoin', page=i)
    data.append(res)

Here is an example of doing a bounded page range query with the extended client.

cg.coins_id_tickers_get('bitcoin', qid="data", page_start=1, page_end=100)
data = cg.execute_queued()['data']

Here is an example of doing an unbounded page range query with the extended client.

cg.coins_id_tickers_get('bitcoin', qid="data", page_start=1)
data = cg.execute_queued()['data']

All code blocks will produce equivalent output. The return value of a page range query is a list of response data from each individual api call. So data[0] contains the result for page 1, data[49] contains the result for page 50.

It's important to note that qid must be included as a keyword argument for page range queries. Thus, page range queries will also automatically deal with rate limiting as detailed in the rate limiting section.

Client Configuration

The extended client supports multiple configuration options which impact its behavior.

Kwarg Default Description
exp_limit 8 Max exponent (2exp_limit) for exponential backoff retries
progress_interval 10 Min percentage interval at which to log progress of queued api calls
log_level logging.INFO python logging log level for client log messages

The API client doesn't print any messages, but has logs at the following levels.

Here's an example of how to configure the client with non-default values.

cg = CoingeckoApi(log_level=10, exp_limit=6, progress_interval=5)

Summary

A quick summary of the functionality offered by this package

Development and Testing

This package is packaged with poetry

If you have poetry installed, you can perform the following steps to set up the development environment.

git clone https://github.com/brycemorrow4564/coingecko_py.git
cd coingecko_py
poetry shell 
poetry update 
poetry install 

If you want to run the tests (within the dev environment), do the following

poetry run test 

License

MIT