Skip to main content

Module for fethcing data from CoinMarketCap's v1 API

Project description

Coverage Python Version License

CoinMarketCap

A Python implementation of CoinMarketCap's V1 API.

You need an API key from CoinMarketCap in order to utilize this module, you can get a key from here. Due the low request rate, I recommend you to test your application in a sandbox environment. You need a different API key when using their sandbox environment, which you can get from here.

Installing

python3 -m pip install CoinMarketCapAPI

Authentication

If no keys are provided during init, the module will look for a JSON file in $HOME/.coinmarketcap.json, with the following syntax.

{
  "sandbox": "API_KEY",
  "production": "API_KEY"
}

Alternatively, you can set your keys as OS environment variables. With .bashrc, add the following two lines.

export COINMARKETCAP_SANDBOX="API_KEY"
export COINMARKETCAP_PRODUCTION="API_KEY"

Examples

Init client with an API Key.

from coinmarketcap import Client

key = "API_KEY"
client = Client(apikey=key)
```request limit

The following example shows how you can access and send a request to each of CoinMarketCap's endpoint (cryptocurrency, exchange, global_metrics, tools). Notice plural and singular method endings. Plurals allow a list of `id`, `symbol` or `slug`, singular allows only a single value.

```python
from coinmarketcap import Client
from datetime import datetime

# Init sandbox client.
client = Client(sandbox=True)

# fetch cryptocurrency info
client.cryptocurrency.info.symbols(["BTC", "ETH"])
client.cryptocurrency.info.ids(1)

# fetch exchange info
client.exchange.info.slugs("binance")
client.exchange.info.ids([1, 4])

# fetch global market data
time = datetime.strptime("2018-12-21", "%Y-%m-%d")
client.global_metrics.quotes.latest()
client.global_metrics.quotes.historical(time_start=time)

amount_1 = 100.4
amount_2 = "901.23"
id = 4
# convert price historic price
client.tools.price.convert_id(amount_1, id, time=time)
client.tools.price.convert_symbol(amount_2, "BTC", convert=["USD", "ETH"])

Due to CoinMarketCap's credit and rate limit system, implementing a proper request throttler is complex. Anyway, I tried to apply three different levels of throttling. "minute", "daily", "monthly". Each level make sure you don't exceed the request limit of the given plan. The different levels have no perception of CoinMarketCap's credit system...

from coinmarketcap import Client
# By default throttling of requests are off.
# Ignore the Client's keyword arguments "throttle", "plan", and "block" if
# you don't want the client to throttle requests.  

# client_1 will not exceed the number of request each minute with the basic plan.
client_1 = Client(throttle="minute", plan="basic")

# client_2 will not exceed the daily request limit.
client_2 = Client(throttle="daily", block=True, plan="hobbyist")

# client_3 will never exceeded CoinMarketCap monthly request rate.
client_3 = Client(throttle="monthly", block=False, plan="professional")

Each request is cached, the expiration time of data can be adjusted with the keyword argument expire. Set expire=0 if don't want any cached data.

from coinmarketcap import Client
# cached request will be removed after 1 second
client = Client(expire=1)

# cacherd request will be removed after 1 h
client = Client(expire=3600)

# To remove all data from the cache, use the method clear_cache
client.clear_cache()

TODO

  • Enable Proper throttling of requests.
  • Testing of different python versions.

This is my first python module. So, I appreciate any feedback :)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

CoinMarketCapAPI-0.5.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

CoinMarketCapAPI-0.5-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file CoinMarketCapAPI-0.5.tar.gz.

File metadata

  • Download URL: CoinMarketCapAPI-0.5.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for CoinMarketCapAPI-0.5.tar.gz
Algorithm Hash digest
SHA256 dfb6db77683abda56e89fe8cc0126c5ca6c067f6f9cf5d4d99208b47463a0775
MD5 778332ec67f1505f3a65987f6e38186b
BLAKE2b-256 bba53d3fa508ebee7d3062705aa97405a929bc58b9a91067ffc099d34ca1974b

See more details on using hashes here.

File details

Details for the file CoinMarketCapAPI-0.5-py3-none-any.whl.

File metadata

  • Download URL: CoinMarketCapAPI-0.5-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for CoinMarketCapAPI-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 754af0534dcccb665f45af1e0733d32ffd2c33640b82b2593b5eb0db7539c5f3
MD5 694ee4e0d626e77a3fe11fc1381bea4f
BLAKE2b-256 067a2e48939954ddbb23bcaa88965a99067c2cbb3c851d12a6ca14468cdaf418

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page