|
| 1 | +/** |
| 2 | + * Copyright Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// See https://support.coinbase.com/customer/en/portal/articles/1914910-how-can-i-generate-api-keys-within-coinbase-commerce- |
| 18 | +var COINBASE_API_TOKEN = ''; // TODO |
| 19 | +/** |
| 20 | + * Get's the bitcoin price at a date. |
| 21 | + * |
| 22 | + * @param {string} date The date in yyyy-MM-dd format. Defaults to today. |
| 23 | + * @return The value of BTC in USD at the date. From Coinbase's API |
| 24 | + * @customfunction |
| 25 | + */ |
| 26 | +function BTC(date) { |
| 27 | + var dateObject = new Date(); |
| 28 | + if (date) { |
| 29 | + dateObject = new Date(date); |
| 30 | + } |
| 31 | + var dateString = Utilities.formatDate(dateObject, "GMT", "yyyy-MM-dd"); |
| 32 | + var res = UrlFetchApp.fetch("https://api.coinbase.com/v2/prices/BTC-USD/spot?date=" + dateString, { |
| 33 | + headers: { |
| 34 | + "CB-VERSION": "2016-10-10", |
| 35 | + Authorization: "Bearer " + COINBASE_API_TOKEN |
| 36 | + } |
| 37 | + }); |
| 38 | + var json = JSON.parse(res.getContentText()); |
| 39 | + return json.data.amount; |
| 40 | +} |
0 commit comments