Introducing go-goldapi: a Go Package for Querying Precious Metal Prices

Joel Guerra
2 min readNov 5, 2020
Photo by Stanislaw Zarychta on Unsplash

Would you like to have automation workflows or alerting related to current spot pricing of gold, silver, platinum or palladium? Of course you would! But first you need to grab the price…

Introducing go-goldapi, a Go package for querying current and historical precious metal prices from goldapi.io. Note: I am the author of the Go package (github.com/joel-g/go-goldapi) but I have no affiliation with goldapi.io.

This API and package can be used to query current and historical prices of gold, silver, platinum and palladium in the following currencies:

  • USD — United States dollar
  • AUD — Australian dollar
  • GBP — British pound
  • EUR — European Euro
  • CHF — Swiss franc
  • CAD — Canadian dollar
  • JPY — Japanese yen
  • INR — Indian rupee
  • SGD — Singapore Dollar
  • BTC — Bitcoin
  • CZK — Czech Krona
  • RUB — Russian Ruble
  • PLN — Polish Złoty
  • MYR — Malaysian Ringgit
  • XAG — Gold/Silver Ratio

To start fetching spot prices with Go get your free API key here and then import github.com/joel-g/go-goldapi into your project.

Try this code sample after you replace “YOUR API KEY” with your API key from goldapi.io.

Then try replacing “USD” with any of the above currency codes.

Then try different metals using the following consts goldapi.Gold, goldapi.Silver, goldapi.Platinum, goldapi.Palladium.

Then try different dates in the YYMMDDD format for the third argument.

package main

import (
"fmt"
"log"
"os"

"github.com/joel-g/go-goldapi/goldapi"
)

func main() {
// Get your API key from https://www.goldapi.io
api := goldapi.NewAPIClient("YOUR API KEY")

// Use one of the 4 const Metals from goldapi package:
// Must include currency in ISO 4217.
// Date is optional but must be in YYYYMMDD format.
// If date is left blank today's date will be used.
silverReport, err := api.GetPrice(goldapi.Silver, "USD", "")
if err != nil {
log.Fatal(err)
}

fmt.Printf("The price of silver is %f\n",silverReport.Price)
fmt.Printf("%+v", silverReport)

// A report of your API usage
stats, err := api.GetStats()
if err != nil {
log.Fatal(err)
}

fmt.Printf("\nYou have used %d requests this month", stats.RequestsMonth)
}

Have fun and as always, bother me on Twitter.

--

--