CoinMarketCap API

fetching crypto prices

Table of Contents

    24 Jan 2023

    Replacing Nomics API with CoinMarketCap API for Cryptee Cryptee

    Documentation:

    Works as:

    from requests import Request, Session
    from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
    import json
    
    url = 'https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest'
    parameters = {
        'symbol':'BTC,ETH',
    }
    headers = {
        'Accepts': 'application/json',
        'X-CMC_PRO_API_KEY': key,
    }
    
    session = Session()
    session.headers.update(headers)
    
    try:
        response = session.get(url, params=parameters)
        data = json.loads(response.text)
        pp.pprint(data)
        btc = data['data']['BTC'][0]['quote']['USD']['price']
        eth = data['data']['ETH'][0]['quote']['USD']['price']
        print(f"\n{btc=}")
        print(f"\n{eth=}")
    except (ConnectionError, Timeout, TooManyRedirects) as e:
        print(e)
    

    links

    social