Skip to content

Instantly share code, notes, and snippets.

@dansalias
Last active October 8, 2021 13:15
Show Gist options
  • Save dansalias/03315365a467d06fb9b5abb87de30334 to your computer and use it in GitHub Desktop.
Save dansalias/03315365a467d06fb9b5abb87de30334 to your computer and use it in GitHub Desktop.
Import coinmarketcap.com crypto prices into Google Sheets
/**
* - Create a coinmarketcap.com account and paste your API key below.
* - In Google Sheets go to Tools -> Script editor and paste this code in a new CMCIMPORT.gs script file
* - Use the function in your sheet, e.g. =CMCIMPORT("ADA")
*/
function CMCIMPORT(symbol) {
const cache = CacheService.getDocumentCache()
if (cache.get('prices') == null) {
const response = UrlFetchApp.fetch('https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=500&convert=USD',{
headers: {
'X-CMC_PRO_API_KEY': '<CMC-API-KEY-GOES-HERE>',
'Accept': 'application/json',
}
})
const prices = JSON.parse(response).data.map(currency => [currency.symbol, currency.quote.USD.price])
cache.put('prices',JSON.stringify(prices),3600)
}
const prices = new Map(JSON.parse(cache.get('prices')))
return prices.get(symbol)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment