Skip to content

goldapi-io/gold-api-examples-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoldAPI.io Go Example: Live Gold Price API

This repository shows how to call the GoldAPI.io live gold price API using Go. It includes a typed GoldAPIResponse, Go standard-library HTTP calls, and a small command-line script for fetching the current XAU/USD gold price.

Use this example if you are looking for:

  • GoldAPI.io Go example
  • GoldAPI.io Golang integration
  • Live gold price API with Go
  • XAU/USD API request sample
  • Precious metals API example using Go
  • Gold price API quickstart for GitHub projects

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

  • Go 1.22 or newer
  • A GoldAPI.io API token

This example uses net/http from the Go standard library, so it does not need a third-party HTTP client.

Installation

Download module dependencies:

go mod download

This example has no external module dependencies, so the command should complete immediately.

Quick Start

Run the Go example:

GOLD_API_TOKEN=your_goldapi_token_here go run ./cmd/gold-api-example

Or build and run the compiled binary:

go build -o bin/gold-api-example ./cmd/gold-api-example
GOLD_API_TOKEN=your_goldapi_token_here ./bin/gold-api-example

You can also change the metal and currency pair:

GOLD_API_TOKEN=your_goldapi_token_here GOLD_API_METAL=XAU GOLD_API_CURRENCY=USD go run ./cmd/gold-api-example

Go Example

The core GoldAPI.io request is:

request, err := http.NewRequest(http.MethodGet, "https://www.goldapi.io/api/XAU/USD", nil)
if err != nil {
    log.Fatal(err)
}

request.Header.Set("x-access-token", os.Getenv("GOLD_API_TOKEN"))
request.Header.Set("Accept", "application/json")

response, err := http.DefaultClient.Do(request)
if err != nil {
    log.Fatal(err)
}
defer response.Body.Close()

var data map[string]any
if err := json.NewDecoder(response.Body).Decode(&data); err != nil {
    log.Fatal(err)
}

fmt.Println(data)

This repository expands that into a typed Go example with:

  • GoldAPIResponse struct
  • JSON mapping with encoding/json
  • Basic HTTP error handling
  • Configurable GOLD_API_METAL and GOLD_API_CURRENCY
  • Safe token usage through environment variables

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.

Formatting and Checks

Run:

go fmt ./...
go test ./...

Security Note

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

This example is intended for server-side Go. Do not expose private GoldAPI.io tokens in client-side applications.

License

MIT

About

Golang GoldAPI example integration

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages