This repository shows how to call the GoldAPI.io live gold price API from Google Sheets using Google Apps Script custom functions.
Use this example if you are looking for:
- GoldAPI.io Google Sheets example
- GoldAPI.io Apps Script integration
- Google Sheets custom function for gold prices
- Live gold price API in Google Sheets
- XAU/AUD API request sample for spreadsheets
- Precious metals API example for Google Sheets
After installing the script, use this formula in Google Sheets:
=GOLDPRICE("XAU","AUD")You can request another pair:
=GOLDPRICE("XAG","USD")The function calls:
https://www.goldapi.io/api/XAU/AUDand returns the current GoldAPI.io price value.
The example calls:
https://www.goldapi.io/api/XAU/USDEquivalent curl request:
curl -X GET "https://www.goldapi.io/api/XAU/USD" \
-H "x-access-token: GOLD_API_TOKEN"- Google Sheets
- Google Apps Script
- A GoldAPI.io API token
- Open your Google Sheet.
- Select Extensions > Apps Script.
- Create or open the Apps Script project attached to the spreadsheet.
- Paste the contents of src/Code.gs into the script editor.
- Replace
your_goldapi_token_hereinsetGoldApiTokenwith your GoldAPI.io token. - Run
setGoldApiTokenonce from the Apps Script editor and approve the requested permissions. - Return to the sheet and use
=GOLDPRICE("XAU","AUD").
The token is stored in Apps Script user properties under GOLD_API_TOKEN, so it does not need to appear in worksheet formulas.
The core GoldAPI.io request is:
const response = UrlFetchApp.fetch("https://www.goldapi.io/api/XAU/USD", {
method: "get",
headers: {
"x-access-token": token,
Accept: "application/json",
},
});
const data = JSON.parse(response.getContentText());
return data.price;This repository expands that into a Google Sheets example with:
GOLDPRICE(metal, currency)custom functionsetGoldApiToken()helper for token setup- Basic HTTP error handling
- Configurable metal and currency pair
- Safe token usage through Apps Script user properties
{
"timestamp": 1776907250,
"metal": "XAU",
"currency": "USD",
"exchange": "FOREXCOM",
"symbol": "FOREXCOM:XAUUSD",
"prev_close_price": 4739.215,
"open_price": 4739.215,
"low_price": 4694.355,
"high_price": 4753.79,
"open_time": 1776902400,
"price": 4733.125,
"ch": -6.09,
"chp": -0.13,
"ask": 4733.72,
"bid": 4732.69,
"price_gram_24k": 152.1735,
"price_gram_22k": 139.4924,
"price_gram_21k": 133.1518,
"price_gram_20k": 126.8113,
"price_gram_18k": 114.1301,
"price_gram_16k": 101.449,
"price_gram_14k": 88.7679,
"price_gram_10k": 63.4056
}| Setting | Required | Default | Description |
|---|---|---|---|
GOLD_API_TOKEN |
Yes | None | Your GoldAPI.io access token stored in Apps Script user properties. |
metal |
No | XAU |
Metal symbol passed to GOLDPRICE. |
currency |
No | USD |
Currency symbol passed to GOLDPRICE. |
Do not put private GoldAPI.io tokens directly in worksheet formulas. Formulas can be exposed when a spreadsheet is shared.
Use setGoldApiToken() to store your token in Apps Script user properties, or adapt the script to fetch the token from your own backend.
MIT