This repository shows how to call the GoldAPI.io live gold price API using Ruby. It includes Ruby 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 Ruby example
- GoldAPI.io Ruby integration
- Live gold price API with Ruby
- XAU/USD API request sample
- Precious metals API example using Ruby
- Gold price API quickstart for GitHub projects
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"- Ruby 2.6 or newer
- A GoldAPI.io API token
This example uses net/http, json, and uri from the Ruby standard library, so it does not need Faraday, HTTParty, or another HTTP client.
This example has no runtime dependencies outside the Ruby standard library, so you do not need Bundler to run it.
Install the example package locally:
gem build gold-api-examples-ruby.gemspec
gem install ./gold-api-examples-ruby-1.0.0.gemYou can also run it directly from the repository without installing the gem.
Run the Ruby example:
GOLD_API_TOKEN=your_goldapi_token_here ruby -Ilib -rgold_api_examples -e 'exit GoldApiExamples.main'Or run the executable:
GOLD_API_TOKEN=your_goldapi_token_here ruby -Ilib exe/gold-api-exampleYou can also change the metal and currency pair:
GOLD_API_TOKEN=your_goldapi_token_here GOLD_API_METAL=XAU GOLD_API_CURRENCY=USD ruby -Ilib exe/gold-api-exampleThe core GoldAPI.io request is:
require "json"
require "net/http"
require "uri"
endpoint = URI("https://www.goldapi.io/api/XAU/USD")
request = Net::HTTP::Get.new(endpoint)
request["x-access-token"] = ENV.fetch("GOLD_API_TOKEN")
request["Accept"] = "application/json"
response = Net::HTTP.start(endpoint.host, endpoint.port, use_ssl: true, read_timeout: 30) do |http|
http.request(request)
end
data = JSON.parse(response.body)
puts dataThis repository expands that into a Ruby example with:
- Basic HTTP error handling
- Configurable
GOLD_API_METALandGOLD_API_CURRENCY - Safe token usage through environment variables
- A
gold-api-exampleexecutable - No runtime dependencies outside the Ruby standard library
{
"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
}| 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. |
Check the Ruby syntax:
ruby -c lib/gold_api_examples/main.rb
ruby -c exe/gold-api-exampleBuild the gem:
gem build gold-api-examples-ruby.gemspecDo 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 Ruby. Do not expose private GoldAPI.io tokens in browser or client-side applications.
MIT