Last active
October 8, 2021 13:15
-
-
Save dansalias/03315365a467d06fb9b5abb87de30334 to your computer and use it in GitHub Desktop.
Import coinmarketcap.com crypto prices into Google Sheets
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
/** | |
* - 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