unirategoweb

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 0 Imported by: 0

README

unirate-go-web

Ready-made HTTP handler adapters that expose the UniRate API — free currency exchange rates, conversion, supported currencies, and VAT rates — through the three most popular Go web frameworks: Gin, Echo, and Fiber.

One module, one line to mount, four routes:

Route Query params Description
GET /rate from (default USD), to (required) Current exchange rate
GET /convert from (default USD), to (required), amount (default 1) Convert an amount
GET /currencies List supported currency codes
GET /vat country (optional ISO-3166 alpha-2) VAT rate(s); omit country for all

Currency and country codes are uppercased automatically. UniRate errors are mapped to the appropriate HTTP status code and a JSON body of the form {"error": "...", "status": <code>}.

Requirements

  • Go 1.25 or newer
  • One of: github.com/gin-gonic/gin, github.com/labstack/echo/v4, github.com/gofiber/fiber/v2

Installation

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

Each adapter package re-exports New to build a UniRate client, so you only need to import the one adapter you use. (The client is the same one from unirate-api-go; if you already hold a *unirate.Client you can pass it straight to Register.)

Usage

Gin
package main

import (
    "os"

    "github.com/gin-gonic/gin"
    unirategin "github.com/UniRate-API/unirate-go-web/gin"
)

func main() {
    client := unirategin.New(os.Getenv("UNIRATE_API_KEY"))

    r := gin.Default()
    unirategin.Register(r, client)          // mounts /rate, /convert, /currencies, /vat
    // or under a group:
    // unirategin.Register(r.Group("/api/fx"), client)

    r.Run(":8080")
}
Echo
package main

import (
    "os"

    "github.com/labstack/echo/v4"
    unirateecho "github.com/UniRate-API/unirate-go-web/echo"
)

func main() {
    client := unirateecho.New(os.Getenv("UNIRATE_API_KEY"))

    e := echo.New()
    unirateecho.Register(e, client)         // or Register(e.Group("/api/fx"), client)

    e.Start(":8080")
}
Fiber
package main

import (
    "os"

    "github.com/gofiber/fiber/v2"
    uniratefiber "github.com/UniRate-API/unirate-go-web/fiber"
)

func main() {
    client := uniratefiber.New(os.Getenv("UNIRATE_API_KEY"))

    app := fiber.New()
    uniratefiber.Register(app, client)      // or Register(app.Group("/api/fx"), client)

    app.Listen(":8080")
}

Calling the routes

curl 'http://localhost:8080/rate?from=USD&to=EUR'
# {"from":"USD","to":"EUR","rate":0.92}

curl 'http://localhost:8080/convert?from=USD&to=EUR&amount=100'
# {"from":"USD","to":"EUR","amount":100,"result":92.5}

curl 'http://localhost:8080/currencies'
# {"currencies":["USD","EUR","GBP", ...]}

curl 'http://localhost:8080/vat?country=DE'
# {"country":"DE","vat_data":{"country_code":"DE","country_name":"Germany","vat_rate":19.0}}

Error handling

Every adapter shares one error-mapping table, so behaviour is identical across frameworks:

Condition HTTP status JSON error
Missing to / bad amount 400 query parameter '...' is required / ... must be a number
Invalid API key (upstream 401) 401 Missing or invalid API key
Unknown currency (upstream 404) 404 Currency not found or no data available
Rate limit (upstream 429) 429 Rate limit exceeded
Pro-gated endpoint (upstream 403) 403 Endpoint requires a Pro subscription
Service unavailable (upstream 503) 503 Service unavailable
Network / unknown 500 Internal server error

Only free-tier endpoints (rate, convert, currencies, vat) are exposed as routes. Historical and time-series data are Pro-gated on the UniRate API.

Custom mounting

Each adapter also exposes the individual handlers if you want to mount them under custom paths or middleware:

  • Gin: unirategin.Handlers(client) returns the four gin.HandlerFuncs.
  • Echo: unirateecho.RateHandler, .ConvertHandler, .CurrenciesHandler, .VATHandler.
  • Fiber: uniratefiber.RateHandler, .ConvertHandler, .CurrenciesHandler, .VATHandler.

Examples

Runnable servers live under examples/: examples/gin, examples/echo, examples/fiber. Each reads UNIRATE_API_KEY from the environment:

UNIRATE_API_KEY=your-key go run ./examples/gin

License

MIT — see LICENSE.

Documentation

Overview

Package unirategoweb is the module root for unirate-go-web: ready-made HTTP handler adapters that expose the UniRate API (https://unirateapi.com) — free currency exchange rates, currency conversion, supported-currency listing, and VAT rates — through the three most popular Go web frameworks.

The module has no exported symbols at its root; import one of the framework sub-packages instead:

github.com/UniRate-API/unirate-go-web/gin    // Gin adapter   (unirategin)
github.com/UniRate-API/unirate-go-web/echo   // Echo adapter  (unirateecho)
github.com/UniRate-API/unirate-go-web/fiber  // Fiber adapter (uniratefiber)

Each sub-package offers a Register function that mounts four routes — /rate, /convert, /currencies, /vat — onto the framework's router, backed by a UniRate client. See each sub-package's docs for details.

Metadata:

Version:     0.1.0
Author:      Unirate Team
License:     MIT
Repository:  https://github.com/UniRate-API/unirate-go-web
Keywords:    unirate, exchange-rates, currency, forex, vat, fintech,
             gin, echo, fiber

Directories

Path Synopsis
Package unirateecho exposes the UniRate API as a set of Echo HTTP handlers.
Package unirateecho exposes the UniRate API as a set of Echo HTTP handlers.
examples
echo command
Runnable Echo server exposing the UniRate routes.
Runnable Echo server exposing the UniRate routes.
fiber command
Runnable Fiber server exposing the UniRate routes.
Runnable Fiber server exposing the UniRate routes.
gin command
Runnable Gin server exposing the UniRate routes.
Runnable Gin server exposing the UniRate routes.
Package uniratefiber exposes the UniRate API as a set of Fiber HTTP handlers.
Package uniratefiber exposes the UniRate API as a set of Fiber HTTP handlers.
Package unirategin exposes the UniRate API as a set of Gin HTTP handlers.
Package unirategin exposes the UniRate API as a set of Gin HTTP handlers.
internal
httperr
Package httperr centralises the mapping from UniRate client errors to an HTTP status code and a JSON-serialisable error body.
Package httperr centralises the mapping from UniRate client errors to an HTTP status code and a JSON-serialisable error body.
params
Package params holds the shared query-parameter parsing used by all three framework adapters, so request handling stays identical across gin, echo, and fiber.
Package params holds the shared query-parameter parsing used by all three framework adapters, so request handling stays identical across gin, echo, and fiber.
unirate
Package unirate is a minimal vendored copy of the official UniRate Go client (github.com/UniRate-API/unirate-api-go), embedded here so the framework adapters build hermetically without resolving an unpublished module tag over the network.
Package unirate is a minimal vendored copy of the official UniRate Go client (github.com/UniRate-API/unirate-api-go), embedded here so the framework adapters build hermetically without resolving an unpublished module tag over the network.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL