Building a Currency Converter in Go

Currencyapi.com provides currency data with update rates ranging from daily to minutely update frequencies. Hence, it is the perfect data provider for your Go currency converter. Developer experience is important to us. Therefore, you may access our API using Go SDK, or alternatively via simple GET requests using a HTTP client. Furthermore, we provide a postman collection to make the implementation even easier for you. In this tutorial, we will focus on accessing the API directly using GET requests.

Simple Go Currency Converter Example:

Using the Go SDK is really the most efficient method to build a Go currency converter, it requires the least amount of code lines. Exchange Rate Data can easily be obtained by running the following script:

Request


package main

import (
"fmt"
"net/http"
"io/ioutil"
)

func main() {
    url := "https://api.currencyapi.com/v3/latest"
    method := "GET"

    client := &http.Client {
}
req, err := http.NewRequest(method, url, nil)

if err != nil {
    fmt.Println(err)
    return
}
req.Header.Add("apikey", "YOUR-API-KEY")

res, err := client.Do(req)
if err != nil {
    fmt.Println(err)
    return
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(string(body))
}

Since USD is set as a default base currency, all exchange rates are relative to USD. If you would like to change this, simply add the desired base_currency parameter to your request:

Changing the base currency

...
url := "https://api.currencyapi.com/v3/latest?&base_currency=CAD"
...

Also, it is possible to limit the amount of currencies that are returned by introducing the currencies parameter to your request.

Limiting the amount of currencies returned

...
url := "https://api.currencyapi.com/v3/latest?base_currency=EUR&currencies=USD,AED,CHF"
...

This will of course not have an impact on the data retrieved, but it will the response easier to handle and easier to read.

JSON response

    {
        "meta":{
            "last_updated_at":"2023-08-03T23:59:59Z"
        },
        "data":{
            "BYN": {
                "code": "BYN",
                "value": 2.4997623239
            },
            "BYR": {
                "code": "BYR",
                "value": 24997.623333524
            },
            "BZD": {
                "code": "BZD",
                "value": 2
            },
            "CAD": {
                "code": "CAD",
                "value": 1.3349901826
            },
            "CDF": {
                "code": "CDF",
                "value": 2480.7373295025
            },
            "...": "..."
    }
}

The values in the JSON response are exchange rates. Since we intend to build a currency converter, we still have to multiply the exchange rate with the amount that we would like to convert, to end up with the desired result.

Converter Endpoint

If you do not wish to take this small extra step, currencyapi.com provides an extra /convert endpoint that was specifically designed for this task. However, it is only available for plans >= medium.