Skip to content

Instantly share code, notes, and snippets.

@carobaldino
Last active August 27, 2022 16:12
Show Gist options
  • Save carobaldino/75c03a0e3a4ff8ccb127b02191cfd793 to your computer and use it in GitHub Desktop.
Save carobaldino/75c03a0e3a4ff8ccb127b02191cfd793 to your computer and use it in GitHub Desktop.
Get price and 24h change of cryptos - BTC example - Apps Script Google Spreadsheets - CoinMarketCap API
//Usando API de CoinMarketCap! Logueate y obtené un token gratis
function request_info(ticker) {
const url = `https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=${ticker}`;
const headers = {
"X-CMC_PRO_API_KEY": <tu_key>
}
const body = {
method: 'GET',
uri: url,
headers: headers,
json: true,
gzip: true,
};
var request = UrlFetchApp.fetch(url, body);
var response = JSON.parse(request.getContentText());
return response;
}
function get_price(a_ticker) {
const ticker = a_ticker.toUpperCase();
var info = request_info(ticker);
Logger.log(info.data[ticker].quote.USD.price);
return info.data[ticker].quote.USD.price;
}
function get_percent_change_24h(a_ticker) {
const ticker = a_ticker.toUpperCase();
var info = request_info(ticker.toUpperCase());
Logger.log(info.data[ticker].quote.USD.percent_change_24h);
return info.data[ticker].quote.USD.percent_change_24h;
}
// Ejemplos:
// =get_price("btc")
// =get_percent_change_24h("btc")
// =get_price("eth")
// =get_percent_change_24h("eth")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment