LIVE GOLD RATE
// Fetch live gold price
async function getGoldPrice() {
const apiKey = ‘YOUR_API_KEY’; // Get free key from goldapi.io
const apiUrl = https://www.goldapi.io/api/price/XAU/USD;
try {
const response = await fetch(apiUrl, {
method: ‘GET’,
headers: {
‘x-access-token’: apiKey
}
});const data = await response.json(); console.log(`Gold Price: $${data.price} per troy ounce`); console.log(`Timestamp: ${data.timestamp}`); // Display on website document.getElementById('gold-price').innerHTML = ` <h2>Gold Price (USD)</h2> <p>$${data.price} per troy ounce</p> <small>Updated: ${new Date(data.timestamp).toLocaleString()}</small> `; return data;
Error loading price’;
}
}
// Call function when page loads
document.addEventListener(‘DOMContentLoaded’, getGoldPrice);
// Refresh price every 60 seconds
setInterval(getGoldPrice, 60000);
} catch (error) {
console.error(‘Error fetching gold price:’, error);
document.getElementById(‘gold-price’).innerHTML = ‘
![[ ONE-GRAM-GOLD ]](https://stargoldtraders.com/wp-content/uploads/2026/09/file_00000000b73c82088e7f99f7ad2e5c60.png)