Skip to content

About

Official Go client for the UniRate API โ€” free currency exchange rates, historical data, and VAT rates.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

ย 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

UniRate Go Client

Official Go client for the UniRate API โ€” free, real-time and historical currency exchange rates plus VAT rates.

  • Real-time exchange rates between 170+ currencies (fiat + crypto)
  • Historical rates back to 1999
  • Time-series ranges up to 5 years
  • Currency conversion (current and historical)
  • VAT rates for countries worldwide
  • Free tier, no credit card required
  • Idiomatic Go: context.Context on every method, sentinel errors, no external dependencies

Requirements

  • Go 1.21 or newer

Installation

go get github.com/UniRate-API/unirate-api-go

Quick start

package main

import (
    "context"
    "fmt"
    "log"
    "os"

    unirate "github.com/UniRate-API/unirate-api-go"
)

func main() {
    client := unirate.New(os.Getenv("UNIRATE_API_KEY"))
    ctx := context.Background()

    rate, err := client.GetRate(ctx, "USD", "EUR")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("USD -> EUR: %.4f\n", rate)

    euros, err := client.Convert(ctx, 100, "USD", "EUR")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("100 USD = %.2f EUR\n", euros)
}

Get a free API key at https://unirateapi.com.

API

Construction

client := unirate.New("your-api-key",
    unirate.WithTimeout(10 * time.Second),        // override default 30s
    unirate.WithHTTPClient(customHTTPClient),     // inject your own *http.Client (or any HTTPDoer)
    unirate.WithBaseURL("https://api.example"),   // override base URL (for tests)
)

Current rates

rate, err := client.GetRate(ctx, "USD", "EUR")
// โ†’ float64

rates, err := client.GetAllRates(ctx, "USD")
// โ†’ map[string]float64

result, err := client.Convert(ctx, 100, "USD", "EUR")
// โ†’ float64

codes, err := client.GetSupportedCurrencies(ctx)
// โ†’ []string

Historical data (Pro-gated โ€” 403 on free tier)

rate, err := client.GetHistoricalRate(ctx, "2024-01-01", "USD", "EUR")

rates, err := client.GetHistoricalRates(ctx, "2024-01-01", "USD")

amount, err := client.ConvertHistorical(ctx, 100, "USD", "EUR", "2024-01-01")

series, err := client.GetTimeSeries(ctx,
    "2024-01-01", "2024-01-07",
    1,                        // amount
    "USD",                    // base
    []string{"EUR", "GBP"},   // pass nil for all
)

limits, err := client.GetHistoricalLimits(ctx)

VAT rates

all, err := client.GetVATRates(ctx)

de, err := client.GetVATRate(ctx, "DE")
fmt.Println(de.VATData.VATRate) // 19.0

format and callback

Every method accepts an optional CallOptions struct as a trailing variadic argument:

client.GetRate(ctx, "USD", "EUR",
    unirate.CallOptions{Format: "xml"},
)

Non-JSON formats will cause the typed decode to fail โ€” use a custom *http.Client and build the URL yourself if you need raw XML/CSV bodies.

Error handling

The client returns sentinel errors wrapped with [fmt.Errorf], so use [errors.Is] to classify them and [errors.As] to extract HTTP details:

_, err := client.GetRate(ctx, "USD", "ZZZ")
switch {
case errors.Is(err, unirate.ErrAuthentication):
    // invalid API key
case errors.Is(err, unirate.ErrInvalidCurrency):
    // unknown currency code
case errors.Is(err, unirate.ErrRateLimit):
    // back off and retry
case errors.Is(err, unirate.ErrInvalidDate):
    // bad date format
default:
    var apiErr *unirate.APIError
    if errors.As(err, &apiErr) {
        // generic HTTP error with apiErr.StatusCode / apiErr.Body,
        // including 403 on Pro-gated endpoints
    }
}

Dependency injection for tests

The WithHTTPClient option accepts anything that implements Do(*http.Request) (*http.Response, error), which *http.Client already satisfies. Combined with WithBaseURL, that makes it trivial to point the client at an httptest.Server:

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    _, _ = w.Write([]byte(`{"rate": "0.9"}`))
}))
defer srv.Close()

client := unirate.New("test-key",
    unirate.WithBaseURL(srv.URL),
    unirate.WithHTTPClient(srv.Client()),
)

Rate limits

  • Currency endpoints: standard rate limits apply
  • Historical endpoints: 50 requests/hour on the free tier
  • VAT endpoints: 1800 requests/hour on the free tier

Testing

go test ./...                       # mock tests (no network)
UNIRATE_API_KEY=... go test -tags live ./...   # live tests (free-tier endpoints)

Related clients

UniRate ecosystem

UniRate ships official integrations for 40+ ecosystems, all maintained under the UniRate-API org.

Core clients (9 languages) Python ยท Node.js / TypeScript ยท Go ยท Rust ยท Java ยท Ruby ยท PHP ยท .NET ยท Swift

JavaScript / TypeScript React ยท Next.js ยท Remix ยท SvelteKit ยท Vue ยท Angular ยท Nuxt ยท NestJS ยท tRPC

Static-site generators Astro ยท Eleventy ยท Hugo ยท Jekyll

CMS & e-commerce Wagtail ยท WordPress ยท WooCommerce ยท Drupal ยท Strapi ยท Medusa ยท Symfony ยท Laravel ยท Directus

Data, AI & backend LangChain (Python) ยท LangChain.js ยท FastAPI ยท Flask ยท Django REST Framework ยท Apache Airflow ยท dbt

Platform & tools MCP server ยท CLI ยท Cloudflare Workers ยท Home Assistant ยท n8n ยท Google Sheets ยท VS Code ยท Obsidian

Money library bridges money gem (Ruby) ยท NodaMoney (.NET)

Get a free API key at unirateapi.com.

License

MIT โ€” see LICENSE.

About

Official Go client for the UniRate API โ€” free currency exchange rates, historical data, and VAT rates.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages