Skip to content

Instantly share code, notes, and snippets.

@Roms1383
Created November 10, 2020 06:30
Show Gist options
  • Save Roms1383/23a195fd77c183a6eb4d15e262f180ac to your computer and use it in GitHub Desktop.
Save Roms1383/23a195fd77c183a6eb4d15e262f180ac to your computer and use it in GitHub Desktop.
Google sheet : quickly replace CRYPTOFINANCE
// use as before: e.g. CRYPTOFINANCE("XLM/USD")
// there's no way to use CRYPTOFINANCE("BINANCE:XLM/USD") without a paid account, so I didn't implement it
const API_KEY = 'YOUR_COINMARKETCAP_API_KEY' // do not forget to replace it with your own API key ;)
const domain = 'https://pro-api.coinmarketcap.com';
const ticker = 'v1/cryptocurrency/quotes/latest';
const options = {
method: 'GET',
headers: {
'X-CMC_PRO_API_KEY': API_KEY,
}
};
function CRYPTOFINANCE (PAIR) {
const [BASE, QUOTE] = PAIR.split('/')
const uri = domain + '/' + ticker
const symbol = 'symbol=' + BASE
const convert = 'convert=' + QUOTE
const url = uri + '?' + symbol + '&' + convert
const response = UrlFetchApp.fetch(url, options)
const json = JSON.parse(response)
const price = json.data[BASE].quote[QUOTE].price
return price
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment