Created
December 26, 2017 15:41
-
-
Save Tyrael/b7ee9085c31283d7cbc710a5f544bfe7 to your computer and use it in GitHub Desktop.
coinbase prices google apps script
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
/** | |
* 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