Skip to content

goldapi-io/gold-api-examples-google-sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

GoldAPI.io Google Sheets Example: Live Gold Price API

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

Custom Function

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/AUD

and returns the current GoldAPI.io price value.

API Endpoint

The example calls:

https://www.goldapi.io/api/XAU/USD

Equivalent curl request:

curl -X GET "https://www.goldapi.io/api/XAU/USD" \
  -H "x-access-token: GOLD_API_TOKEN"

Requirements

  • Google Sheets
  • Google Apps Script
  • A GoldAPI.io API token

Installation

  1. Open your Google Sheet.
  2. Select Extensions > Apps Script.
  3. Create or open the Apps Script project attached to the spreadsheet.
  4. Paste the contents of src/Code.gs into the script editor.
  5. Replace your_goldapi_token_here in setGoldApiToken with your GoldAPI.io token.
  6. Run setGoldApiToken once from the Apps Script editor and approve the requested permissions.
  7. 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.

Apps Script Example

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 function
  • setGoldApiToken() helper for token setup
  • Basic HTTP error handling
  • Configurable metal and currency pair
  • Safe token usage through Apps Script user properties

Sample GoldAPI.io Response

{
  "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
}

Configuration

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.

Security Note

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.

License

MIT

Releases

No releases published

Packages

 
 
 

Contributors