noobcodersan
noobcodersan

Reputation: 33

KeyError prices for Coingecko API

I have tested this and has implemented it into my High charts javascript file. The caveat is that when I reload the page twice it will crash due to an error.

@app.route('/') def cryptodashboard():
    # Get historical price data for Bitcoin, Ethereum, and Ripple

        btc_data = requests.get(
            'https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=365').json()['prices']
        eth_data = requests.get(
            'https://api.coingecko.com/api/v3/coins/ethereum/market_chart?vs_currency=usd&days=365').json()['prices']
        xrp_data = requests.get(
            'https://api.coingecko.com/api/v3/coins/ripple/market_chart?vs_currency=usd&days=365').json()['prices']

        # Get live data for Bitcoin, Ethereum, and Ripple
        btc_live = requests.get(
            'https://api.coingecko.com/api/v3/coins/bitcoin').json()
        eth_live = requests.get(
            'https://api.coingecko.com/api/v3/coins/ethereum').json()
        xrp_live = requests.get(
            'https://api.coingecko.com/api/v3/coins/ripple').json()

        # Get market cap data for Bitcoin, Ethereum, and Ripple
        btc_market_cap = btc_live['market_data']['market_cap']['usd']
        eth_market_cap = eth_live['market_data']['market_cap']['usd']
        xrp_market_cap = xrp_live['market_data']['market_cap']['usd']
        return render_template('index.html', btc_data=(btc_data), eth_data=(eth_data), xrp_data=(xrp_data), btc_live=(btc_live), eth_live=(eth_live), xrp_live=(xrp_live), btc_market_cap=(btc_market_cap), eth_market_cap=(eth_market_cap), xrp_market_cap=(xrp_market_cap))

This is the error in the Flask Debugger, KeyError: 'prices'.

When I look at the website https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=365 it tells me that I have reached the API limit hence it is not able to show the price array. What I have done is try to change the days=365 in the API to days=2 but the problem still persists.

Please advise me how to fix this problem.

Upvotes: 0

Views: 379

Answers (0)

Related Questions