Skip to content

Instantly share code, notes, and snippets.

@Tyrael
Created December 26, 2017 15:41
Show Gist options
  • Save Tyrael/b7ee9085c31283d7cbc710a5f544bfe7 to your computer and use it in GitHub Desktop.
Save Tyrael/b7ee9085c31283d7cbc710a5f544bfe7 to your computer and use it in GitHub Desktop.
coinbase prices google apps script
/**
* Get the coinbase price.
*
* @param {string} currency_pair The currency pair (BTC-EUR, ETH-EUR or LTC-EUR)
* @param {string} price_type The price type (buy, sell or spot
* @return The current price on coinbase
* @customfunction
*/
function COINBASE_PRICE(currency_pair, price_type) {
//if (currency_pair == null) {
// currency_pair = "BTC-EUR"
//}
//if (price_type == null) {
// price_type = "spot"
//}
var valid_currency_pairs = [ "BTC-EUR", "ETH-EUR", "LTC-EUR" ]
var valid_price_types = [ "buy", "sell", "spot" ]
if (valid_currency_pairs.indexOf(currency_pair) == -1) {
throw new Error('Invalid currency_pair: ' + currency_pair + " Valid pairs are: " + valid_currency_pairs.join(","))
}
if (valid_price_types.indexOf(price_type) == -1) {
throw new Error('Invalid price_type: ' + price_type + " Valid types are: " + valid_price_types.join(","))
}
var url = "https://api.coinbase.com/v2/prices/" + currency_pair + "/" + price_type
var response = UrlFetchApp.fetch(url)
var data = JSON.parse(response)
var price = data.data.amount
return price
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment