Skip to content

goldapi-io/gold-api-examples-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoldAPI.io Python Example: Live Gold Price API

This repository shows how to call the GoldAPI.io live gold price API using Python. It includes a typed GoldApiResponse, Python 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 Python example
  • GoldAPI.io Python integration
  • Live gold price API with Python
  • XAU/USD API request sample
  • Precious metals API example using Python
  • 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

  • Python 3.9 or newer
  • A GoldAPI.io API token

This example uses urllib.request from the Python standard library, so it does not need Requests, HTTPX, or another HTTP client.

Installation

Create and activate a virtual environment:

python3 -m venv .venv
source .venv/bin/activate

Install the example package:

python -m pip install -e .

Quick Start

Run the Python example:

GOLD_API_TOKEN=your_goldapi_token_here python -m gold_api_examples

Or run the installed console command:

GOLD_API_TOKEN=your_goldapi_token_here 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 python -m gold_api_examples

Python Example

The core GoldAPI.io request is:

import json
import os
from urllib.request import Request, urlopen

request = Request(
    "https://www.goldapi.io/api/XAU/USD",
    headers={
        "x-access-token": os.environ["GOLD_API_TOKEN"],
        "Accept": "application/json",
    },
    method="GET",
)

with urlopen(request, timeout=30) as response:
    data = json.loads(response.read().decode("utf-8"))

print(data)

This repository expands that into a typed Python example with:

  • GoldApiResponse TypedDict
  • Basic HTTP error handling
  • Configurable GOLD_API_METAL and GOLD_API_CURRENCY
  • Safe token usage through environment variables
  • A gold-api-example console command

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.

Type Checking

Install the optional development dependency:

python -m pip install -e ".[dev]"

Run:

mypy src

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 Python. Do not expose private GoldAPI.io tokens in browser or client-side applications.

License

MIT

Releases

No releases published

Packages

 
 
 

Contributors

Languages