Skip to content

Instantly share code, notes, and snippets.

@nrese
Created June 4, 2020 23:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nrese/97706f303239dd824e62f1021c6c4d6d to your computer and use it in GitHub Desktop.
Save nrese/97706f303239dd824e62f1021c6c4d6d to your computer and use it in GitHub Desktop.
#This example uses Python 2.7 and the python-request library.
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
parameters = {
'start':'1',
'limit':'2',
'convert':'USD'
}
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': 'YOUR_API_KEY_HERE',
}
session = Session()
session.headers.update(headers)
try:
response = session.get(url, params=parameters)
data = json.loads(response.text)
print(data)
except (ConnectionError, Timeout, TooManyRedirects) as e:
print(e)
{'status': {'timestamp': '2020-06-04T23:29:58.259Z', 'error_code': 0, 'error_message': None, 'elapsed': 8, 'credit_count': 1, 'notice': None}, 'data': [{'id': 1, 'name': 'Bitcoin', 'symbol': 'BTC', 'slug': 'bitcoin', 'num_market_pairs': 8338, 'date_added': '2013-04-28T00:00:00.000Z', 'tags': ['mineable'], 'max_supply': 21000000, 'circulating_supply': 18394318, 'total_supply': 18394318, 'platform': None, 'cmc_rank': 1, 'last_updated': '2020-06-04T23:28:31.000Z', 'quote': {'USD': {'price': 9822.47850516, 'volume_24h': 26031798003.1612, 'percent_change_1h': 0.522824, 'percent_change_24h': 2.02458, 'percent_change_7d': 3.47049, 'market_cap': 180677793172.07767, 'last_updated': '2020-06-04T23:28:31.000Z'}}}, {'id': 1027, 'name': 'Ethereum', 'symbol': 'ETH', 'slug': 'ethereum', 'num_market_pairs': 5188, 'date_added': '2015-08-07T00:00:00.000Z', 'tags': ['mineable'], 'max_supply': None, 'circulating_supply': 111215199.6865, 'total_supply': 111215199.6865, 'platform': None, 'cmc_rank': 2, 'last_updated': '2020-06-04T23:28:23.000Z', 'quote': {'USD': {'price': 244.389235229, 'volume_24h': 10225795792.572, 'percent_change_1h': 0.60123, 'percent_change_24h': 0.819696, 'percent_change_7d': 11.6172, 'market_cap': 27179797597.224255, 'last_updated': '2020-06-04T23:28:23.000Z'}}}]}
@gunjarfuley
Copy link

Thanks a lot. Really helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment