Skip to content

Instantly share code, notes, and snippets.

@stiucsib86
Created January 22, 2021 11:38
Show Gist options
  • Save stiucsib86/9934f139ed1ccf93da47d41e9bbd8942 to your computer and use it in GitHub Desktop.
Save stiucsib86/9934f139ed1ccf93da47d41e9bbd8942 to your computer and use it in GitHub Desktop.
[Google Sheet] Useful scripts to pull data from Coingecko API
function getPriceCoingecko(crypto) {
if (!crypto) return null;
var cache = CacheService.getDocumentCache();
var cache_key = "Price_" + crypto;
var results;
if (results = cache.get(cache_key)) {
return parseFloat(results);
}
// Reference: https://api.coingecko.com/api/v3/coins/list
var response = UrlFetchApp.fetch("https://api.coingecko.com/api/v3/simple/price?ids=" + crypto + "&vs_currencies=usd");
var w = JSON.parse(response.getContentText());
price = w[crypto]['usd'];
cache.put(cache_key, price, 15*60);
return price;
}
function getMarketsCoingecko(crypto) {
if (!crypto) return null;
var cache = CacheService.getDocumentCache();
var cache_key = "Markets_" + crypto;
var results;
if (results = cache.get(cache_key)) {
return results;
}
// https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=kusama&order=market_cap_desc&per_page=100&page=1&sparkline=false
var response = UrlFetchApp.fetch("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=" + crypto + "&order=market_cap_desc&per_page=100&page=1&sparkline=false");
var w = JSON.parse(response.getContentText());
let data = JSON.stringify(w[0]);
cache.put(cache_key, data, 15*60);
return data;
}
function getObjectValue(item, key) {
if (!item) return;
if ("string" === typeof item) {
item = JSON.parse(item);
}
return item[key];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment