A gold price API that converts XAU ounces to USD does one job: it takes a live gold quote and multiplies it by an ounce amount, so your app shows the dollar value of a holding instead of a raw rate. In API terms, XAU is the ISO 4217 code for one troy ounce of gold, and XAUUSD is the price of that ounce in US dollars. One troy ounce is exactly 31.1034768 grams, not the 28.35-gram ounce used for groceries.
Some providers expose a dedicated convert endpoint. Others return a live quote and leave the multiplication to you. The second approach takes a few lines of code and gives you more control, as long as you read every field correctly.
Key takeaways
- XAUUSD is US dollars per troy ounce (31.1034768 grams).
- A live quote has a bid and an ask. Pick the side that matches the question you are answering.
- Prices may arrive as strings, so cast them before any math.
- Timestamps in milliseconds have 13 digits. Do not read them as seconds.
- Label the result as a reference value, not a price anyone will trade at.
Request: one ounce of gold in USD
On Sifting IO, a multi-asset market data API covering stocks, forex, crypto and commodities, the latest gold quote is a single REST call. The API key goes in an X-API-Key header and the path names the market and the symbol:
curl -H “X-API-Key: YOUR_API_KEY” \
“https://api.sifting.io/v1/last/quote/commodities/XAUUSD”
The same pattern works across markets, so switching a tile from gold to EUR/USD or bitcoin changes the path, not the parser. For live updates, the provider offers WebSocket streaming on the same key, passed as a ?key= query parameter or sent in an auth message after connecting. It describes its data as aggregated, de-duplicated and normalized from multiple sources, and states that its outputs are reference values rather than raw exchange feeds or official exchange-of-record prices. That tells you how to label every number it returns.
Response: the quote envelope
The provider’s commodities page shows this sample response for XAUUSD:
{
“s”: “XAUUSD”,
“b”: “2387.42”,
“B”: “120.5”,
“a”: “2387.61”,
“A”: “98.0”,
“t”: 1746189123456
}
The fields read as follows: s is the symbol, b and a are the best bid and best ask, B and A are their sizes, and t is a Unix timestamp in milliseconds. This sample is a documentation example from 2 May 2025 (12:32:03 UTC), not a current price, so always use the live values from your own key.
Two details matter before you convert anything:
- Prices are strings. The quotes around “2387.42” preserve exact decimals. In JavaScript, “2387.42” + 1 gives “2387.421”, not a sum. Cast first.
- The timestamp is in milliseconds. Divide by 1,000 before passing it to anything that expects seconds.
From this sample, the mid price is 2,387.515 and the spread is 0.19.
Convert: XAU ounces to USD in code
With a quote in hand, conversion is multiplication. The only real decision is which side of the quote to use:
- Bid: what the market would pay. Use it to value a holding someone might sell.
- Ask: what a buyer would pay. Use it for a purchase estimate.
- Mid: the neutral reference for dashboards and portfolio tiles.
Using the sample quote, 25 troy ounces works out to:
- At the bid: 25 × 2,387.42 = 59,685.50 US dollars
- At the mid: 25 × 2,387.515 = 59,687.875 US dollars
- At the ask: 25 × 2,387.61 = 59,690.25 US dollars
A compact version:
const TROY_OUNCE_GRAMS = 31.1034768;
function xauToUsd(quote, ounces, side = “mid”) {
const bid = Number(quote.b);
const ask = Number(quote.a);
const price = side === “bid” ? bid : side === “ask” ? ask : (bid + ask) / 2;
return ounces * price;
}
const usdPerGram = (quote) => xauToUsd(quote, 1) / TROY_OUNCE_GRAMS;
If your users think in grams, divide the per-ounce price by 31.1034768. At the sample mid, that is about 76.76 US dollars per gram. Dividing by 28.35 instead would overstate the per-gram price by roughly 9.7 percent, and nothing in the API response would warn you. Round only the final figure you display.
Consumer gold platforms, like the ones in this roundup of gold investment apps, usually show buy and sell prices that include a dealer spread or premium. Your reference value will not match them exactly, and it should say so on the screen.
Other data on the same key

The quote endpoint is only one piece. On the same credential, the provider’s homepage lists:
- Historical prices and OHLCV bars over REST, included on every plan, for charting a holding’s value over time.
- WebSocket streams for live quotes and trades, so a gold tile can update without polling.
- Forex quotes for majors, minors and exotics, which you need to turn a USD value into rupees, euros or any other local currency.
When you pair gold with an FX rate, compare the two timestamps first. Gold and currencies move independently, and a fresh gold quote multiplied by a stale FX rate produces a number that never existed.
Go live
- Create an account and generate an API key. The free tier needs no credit card.
- Call the XAUUSD quote endpoint with the key in the X-API-Key header.
- Cast b and a to numbers and convert t from milliseconds.
- Multiply your ounce amount by the bid, ask or mid, depending on the use case.
- Before showing prices to customers, confirm that your plan covers commercial use and customer-facing display.
Conclusion
Converting XAU ounces to USD is the simplest calculation in a gold app, which is exactly why mistakes slip through. Read the quote as strings, respect the millisecond timestamp, pick the right side of the spread, keep the troy ounce constant exact and label the output as a reference value. Get those five details right on day one, and the same code will hold up as you add grams, local currencies and historical charts.
FAQ
What does XAU mean in a gold price API? XAU is the ISO 4217 code for one troy ounce of gold. XAUUSD is the price of that ounce in US dollars.
How do I convert gold ounces to USD with an API? Fetch a live XAUUSD quote, cast the price to a number and multiply it by the number of troy ounces.
Should I use the bid, ask or mid price? Use the bid to value gold someone might sell, the ask for a purchase estimate and the mid for neutral dashboard displays.
How many grams are in a troy ounce? Exactly 31.1034768 grams, about 9.7 percent heavier than the everyday avoirdupois ounce.
Why doesn’t my app’s gold price match a dealer’s price? A market data feed gives a reference value. Dealers add spreads, premiums and sometimes taxes on top.
