For anyone trying to fix issue with Coingecko API

By Bala | Bala | 29 Mar 2023


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.

image.png

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 @balaz as a Hive Witness

 

Vote @kanibot as a Hive Engine Witness

 


qjrE4yyfw5pJD9LKQ1BG3NbSyKTowh8wnMyt99dK1UPEkAAsgygincveTbT6c9mQn2FjHQKTrSCy4VodV2BeGrrUYhYALXcPP7zMVVgpzvwiX1ZGczXYyQXp.webp?format=webp&mode=fit 8SzwQc8j2KJa5zNbRVVrXnjUH7HeFHWfeS6aA81JxfFe6qdWqg5LTFpmft9g9sJnTThvVGirtRvu3qSwkxJvgPc7wEE69pJp5rZ2ZXnZnPkqJDYcHEE.webp?format=webp&mode=fit FUkUE5bzkAZT3HzV5tJDiU2ik81PCd4JCyhWnRcDN8XJsVFY3UNB8DCRUuhCDN66wiMnTycF4W5TM6SbQHGDwYmP1FStMmkUvfPEVxYfXyDTCmU2bEwm3fiku8ti6aos9RroLdtDQHPhjEDFeNyzYWy7k1g6WcZGHHGJ.webp?format=webp&mode=fit 3W72119s5BjW4PvRk9nXC1RsHWxNoWjhqsdMpcQrTsuSz5KpfZW7qpxhpoSQZvFhxkRRmq1UF41U8uxdrJDzJnoJ8ZxCS5GvqdJLbrGYbMeySF4iXYaEXp.webp?format=webp&mode=fit 2gsjgna1uruvGBHDnRaj2zVCcpeJW4jyLMe4SBCGvYRn4kXTQGFWvCLdiA9W4zv1tZV6Yvh46bKBawf8GCSfXvVWPUrg2zxTdRztPy74sCAg9ZZK3k.webp?format=webp&mode=fit 3W72119s5BjW4PvRk9nXC1RsHWxNoWjhqsdMpcQqzxxkgPuJFq7EcFz1sUUCcJwEk8zQ2zYKBtbXBrdJEjCpw1YuaREa8cBFZpYhib711zbJgapbJQZrwx.webp?format=webp&mode=fit

How do you rate this article?

10


Bala
Bala

Developer | Writer | Blogger | Gamer | Blockchain Enthusiast


Bala
Bala

This is my general blog. I share different articles I write every single day.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.