For anyone trying to fix the issue with Coingecko API

0 29
Avatar for bala41288
1 year ago

Today was one of the toughest days for me. Ever since I woke up in the morning I had to deal with issues on multiple services that I host for others. Something is wrong with Coingecko. I did not bother to check my apps when Aggy made an announcement yesterday. Today I was forced to check my apps and do some fixes. Someone said that Coingecko API had a DDoS attack on their API and the regular requests were also affected.

I guess Coingecko is now switching from an open API to an API key-based solution. I guess that's better because only authenticated people will be able to get their requests served. I did not get the time to explore this and was also not sure if their DDoS problem was resolved or not. I started looking for an alternative solution and thought of using Coinmarketcap instead. They have an API key-based solution and there are only limited requests someone can make to the API per day. It is only 333 requests per day.

It was a long day for me so, this post is going to be a short one. For someone switching from Coingecko's API to Coinmarketcap, below is the code snippet to query the price of Hive and HBD with the help of the coinmarketcap API endpoint. It took me a while to understand the documentation and come up with this function.

const axios = require('axios');

const price = async () => {
  try {
    const p = await axios.get(
      'https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?slug=hive-dollar,hive-blockchain',
      {
        headers: {
          'X-CMC_PRO_API_KEY': '03be2b5b-f285-4624-8082-0ab0c077fcfa',
        },
      },
    );
    return {
      hive: p.data.data['5370'].quote.USD.price,
      hbd: p.data.data['5375'].quote.USD.price,
    };
  } catch (e) {
    // eslint-disable-next-line no-console
    console.log('Error in getting price', e);
    return e;
  }
};

I have this function being called every 5 minutes to get the updated price. I initially had a once in 30 seconds call but with the current API restrictions, I guess I will not be able to do calls that frequently. So I'm switching to a once in 5 minutes call and also saving the price locally. This way I don't have to call the API every time I need data. Maybe if I can use two API keys from two email addresses, I guess I should be able to do more calls which would be 666 calls per day. But currently, I don't have a need for it. Maybe in the future, if there is a need, I can think of having multiple email addresses as the solution.

There is also one more option instead of using Coingecko API or Coinmarketcap API. This would be to get the price directly from exchanges and get an average out of them. This function in the price feed application can also be used for this purpose. Anyone who would like to explore these can give them a try.


If you like what I'm doing on Hive, you can vote me as a witness with the links below.

Vote@balazas aHive Witness
Vote@kanibotas aHive Engine Witness


2
$ 0.08
$ 0.08 from @TheRandomRewarder
Avatar for bala41288
1 year ago

Comments