Created
July 18, 2021 03:19
-
-
Save brotoo25/e4fa9eeefb2dae9ac4441d16a940f6db to your computer and use it in GitHub Desktop.
Query cryptocurrency price using CoinGecko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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