Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mebjas
Created May 7, 2022 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mebjas/26176811f41bfbd1655dc0d73a52ae95 to your computer and use it in GitHub Desktop.
Save mebjas/26176811f41bfbd1655dc0d73a52ae95 to your computer and use it in GitHub Desktop.
MetalsApi.gs with API call
/**
* 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