A gold price ticker for a jeweller's site in about 30 lines of JavaScript

Most people who buy gold check the price in their own money and their own unit: dirhams a gram in Dubai, rupees for 10 grams in Mumbai, naira a gram in Lagos. A jeweller's site that shows that figure answers the first question every visitor has. This tutorial builds a small ticker that does it, with no server code, no API key and no build step.
The figures come from the free rates API of Crystal Ball Markets (I work there). Its gold.json file gives the gold price in local currency for 69 countries, in the unit and purity each country usually buys in, and is refreshed every few minutes. It is free on one condition: the page that shows the figures also shows the link "Rates by Crystal Ball Markets", visible and without nofollow. The ticker below includes it.
What the file looks like
https://crystalballmarkets.com/markets/api/v1/gold.json returns:
{
"attribution": { "required": true, "text": "Rates by Crystal Ball Markets", "url": "https://crystalballmarkets.com/markets" },
"updated": "2026-12-01T21:10:00Z",
"data": {
"usdPerOunce": 3610.99,
"countries": [
{ "country": "AE", "currency": "AED", "unit": "gram", "purity": "k24", "price": 426.85 },
{ "country": "IN", "currency": "INR", "unit": "tenGrams", "purity": "k24", "price": 107389 }
]
}
}
The figures above are an example, and each country entry has a few more fields than shown (the full list is in the OpenAPI description). The ones we need:
country: the ISO code,AE,IN,PK,NGand so on.currency: the ISO currency code, whichIntl.NumberFormatunderstands directly.unitandpurity: how gold is sold there (tenGramsandk24in India,tolain Pakistan).price: one unit at that purity, in local money.
The ticker
Paste this where the price should appear and set data-country to your country's code:
<div id="gold-ticker" data-country="AE">
<strong class="gold-price">Loading the gold price…</strong>
<span class="gold-unit"></span>
<small class="gold-time"></small>
<a href="https://crystalballmarkets.com/markets">Rates by Crystal Ball Markets</a>
</div>
<script>
(function () {
const box = document.getElementById('gold-ticker');
const units = { gram: 'gram', tenGrams: '10 grams', tola: 'tola', ounce: 'troy ounce', bhori: 'bhori', pavan: 'pavan', mithqal: 'mithqal' };
const karats = { k24: '24K', k22: '22K', k21: '21K', k18: '18K', k14: '14K', th965: '96.5%' };
const show = (cls, text) => { box.querySelector(cls).textContent = text; };
async function update() {
try {
const res = await fetch('https://crystalballmarkets.com/markets/api/v1/gold.json');
const body = await res.json();
if (!res.ok) return show('.gold-price', body.message || 'The price is not available right now.');
const row = body.data.countries.find((c) => c.country === box.dataset.country);
if (!row) return show('.gold-price', 'No gold price for this country.');
const money = new Intl.NumberFormat(undefined, { style: 'currency', currency: row.currency });
show('.gold-price', money.format(row.price));
show('.gold-unit', `per ${units[row.unit] || row.unit}, ${karats[row.purity] || row.purity}`);
show('.gold-time', `Updated ${new Date(body.updated).toLocaleTimeString()}`);
} catch (err) {
// Network trouble: keep the last price on screen and try again at the next refresh.
}
}
update();
setInterval(update, 5 * 60 * 1000);
})();
</script>
For data-country="AE" it shows something like AED 426.85 per gram, 24K; for IN, ₹107,389.00 per 10 grams, 24K.
How it works
Formatting. Intl.NumberFormat with style: 'currency' takes the ISO code from the file and writes the right symbol, separators and decimals for the visitor's locale. Passing undefined as the locale uses the visitor's browser setting; pass 'en-IN' or 'ar-AE' to fix it.
Units and purity. The file names them with codes so they can be translated; the two small maps turn them into words. Add the units your country uses (the full list is in the OpenAPI description) or write them in your own language.
Refresh. Every five minutes is plenty: gold rarely moves enough in that time to change what a customer decides, and it keeps the page light.
Errors. Two kinds are handled differently. If the network fails, the last price stays on screen and the next refresh tries again. If the API answers with an error, for example because the credit link is missing from the page, the ticker shows the API's own message, which says what to fix. Showing that message is more useful to the site owner than a silent blank.
The credit link
The link is part of the HTML, not added by the script, so it is there even before the price loads and for visitors without JavaScript. The API checks pages that use its files once a day; a page whose link is missing or marked nofollow gets a warning in the API's answers, and is refused a day later if the link is still not there. Keep it visible and unchanged and nothing else is needed: no key, no sign-up.
Calling it from a server instead
The snippet above runs in the visitor's browser, which is the simplest case. If you fetch the file from your own server (to cache it, or for a WordPress plugin), the API asks you to name the page that shows the figures with site= and to send a secret of your own in an X-Site-Key header. The API page has the details.
No code at all
If you would rather not write any code, the same figures come as a ready-made widget: pick the country on the widgets page and paste the embed code it gives you.
The gold prices in the file are indicative, converted from the international price at the exchange rates the file names; a jeweller's selling price adds making charges and local premiums. Information only, not investment advice.
About the author
- Crystal Ball Markets publishes free market data: about 210 live quotes, exchange rates for 60 currencies, gold prices in local money for 69 countries, and a free JSON rates API with no key. https://crystalballmarkets.com/markets
Comments
Join the discussionNo comments yet. Be the first to comment.