Skip to content

Instantly share code, notes, and snippets.

@brotoo25
Created July 18, 2021 03:19
Show Gist options
  • Save brotoo25/e4fa9eeefb2dae9ac4441d16a940f6db to your computer and use it in GitHub Desktop.
Save brotoo25/e4fa9eeefb2dae9ac4441d16a940f6db to your computer and use it in GitHub Desktop.
Query cryptocurrency price using CoinGecko
/**
* Get most recent price of any crypto listed at CoinGecko.
* Params: (coin) - Crypto ID from CoinGecko, all crypto IDs can be found at:
* https://api.coingecko.com/api/v3/coins/list?include_platform=false
* (defaultCurrency) - Base Currency Code to calculate price. Ex: USD, BRL, NZD
**/
async function fetchPriceOnCoinGecko(coin, defaultCurrency) {
try {
const response = await axios.get(`https://api.coingecko.com/api/v3/simple/price?ids=${coin}&vs_currencies=${defaultCurrency}`);
return response.data[`${coin}`][defaultCurrency.toLowerCase()]
} catch (error) {
console.error(error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment