Skip to content

goldapi-io/gold-api-examples-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoldAPI.io JavaScript Example: Live Gold Price API with Vanilla JS

This repository shows how to call the GoldAPI.io live gold price API using vanilla JavaScript and the native Node.js fetch API. It is a small, dependency-free example for fetching the current XAU/USD gold price with a GoldAPI.io API token.

Use this example if you are looking for:

  • GoldAPI.io JavaScript example code
  • Live gold price API integration in Node.js
  • XAU/USD API request sample
  • Vanilla JavaScript fetch example for precious metals prices
  • Gold price API quickstart for GitHub projects

API Endpoint

The example calls the GoldAPI.io endpoint:

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

  • Node.js 18 or newer
  • A GoldAPI.io API token

Node.js 18+ is used because it includes native fetch, so no third-party HTTP client is required.

Quick Start

Clone the repository and run:

GOLD_API_TOKEN=your_goldapi_token_here npm start

Optional values:

GOLD_API_TOKEN=your_goldapi_token_here GOLD_API_METAL=XAU GOLD_API_CURRENCY=USD npm start

JavaScript Example

The core request is implemented in index.js:

const response = await fetch("https://www.goldapi.io/api/XAU/USD", {
  method: "GET",
  headers: {
    "x-access-token": process.env.GOLD_API_TOKEN,
  },
});

const data = await response.json();
console.log(data);

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
}

Environment Variables

Variable Required Default Description
GOLD_API_TOKEN Yes None Your GoldAPI.io access token.
GOLD_API_METAL No XAU Metal symbol to request.
GOLD_API_CURRENCY No USD Currency symbol to request.

Security Note

Do not commit your GoldAPI.io token to GitHub. Keep it in environment variables or a local .env file that is ignored by Git.

This example is intended for server-side JavaScript with Node.js. Avoid putting private API tokens directly into browser JavaScript because users can view client-side source code.

License

MIT

About

This repository shows how to call the GoldAPI.io live gold price API using vanilla JavaScript

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors