-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetalprices.js
38 lines (37 loc) · 1.12 KB
/
metalprices.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//Sample Call:
// async function updateGoldPrice() {
// const bobInput = document.getElementById("bob");
// bobInput.value = await fetchBidPrice("gold");
// }
// window.onload = updateGoldPrice;
async function fetchBidPrice(mType) {
let result = 0;
//Public api key. Free account.
let aKey = "A9LHCEI3NN7SJJE3UI2I732E3UI2I";
let bUrl = "https://api.metals.dev/v1/metal/spot";
let url = `${bUrl}?api_key=${aKey}&metal=${mType}¤cy=USD`;
let response = await fetch(url);
//Sample Return json:
// {
// "status": "success",
// "timestamp": "2024-02-23T05:45:07.691Z",
// "currency": "USD",
// "unit": "toz",
// "metal": "silver",
// "rate": {
// "price": 22.6235,
// "ask": 22.632,
// "bid": 22.614,
// "high": 22.8448,
// "low": 22.617,
// "change": -0.129,
// "change_percent": -0.57
// }
// }
let jsonResult = await response.json();
if (jsonResult.rate && jsonResult.rate.bid > 0)
{
result = jsonResult.rate.bid;
}
return result;
}