Created
May 7, 2022 05:12
-
-
Save mebjas/26176811f41bfbd1655dc0d73a52ae95 to your computer and use it in GitHub Desktop.
MetalsApi.gs with API call
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
/** | |
* Returns the value of given {@param symbol} from metals-api. | |
*/ | |
function metalsApi(symbol) { | |
if (!symbol || symbol === "") { | |
return "symbol is mandatory" | |
} | |
// TODO(Minhaz): Add caching support, limited calls/hour. | |
const apiKey = "<Your api key here - get it from metals-api.com>"; | |
const url = "https://metals-api.com/api/latest?access_key=" + apiKey; | |
var response = UrlFetchApp.fetch(url); | |
var data = JSON.parse(response.getContentText()); | |
Logger.log(response); | |
if (data.success !== true) { | |
return "No Success"; | |
} | |
/** | |
* Data format {..., "rates": {"XAU": 0.22}, ...} | |
*/ | |
let value = parseFloat(data.rates[symbol]); | |
if (Number.isNaN(value)) { | |
return "NaN"; | |
} | |
// For this API the value is returned in this format. | |
return 1 / value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment