Skip to content

error2215/exchange-rates-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

exchange-rates-api - Currency Exchange Rates API SDK

This is an unofficial wrapper for the awesome, free ExchangeRatesAPI, which provides exchange rate lookups courtesy of the Central European Bank.

1. Installation:

Use go get to retrieve the SDK to add it to your GOPATH workspace, or project's Go module dependencies.

go get github.com/error2215/exchange-rates-api

2. Getting Started:

Since the CurrencyExchangeAPI does not require API keys or authentication in order to access and interrogate its API, getting started with this library is easy. The following examples show how to achieve various functions using the library.

Basic usage:
Fetch the latest exchange rates from the European Central Bank:

import (
  "github.com/sirupsen/logrus"
	api "github.com/error2215/exchange-rates-api"
)

func main() {
	json, err := api.New().Get()
	if err != nil{
		logrus.Error(err)
	}else{
		logrus.Info(json)
	}
}

Historical data:
Get historical rates for any day since 1999:

json, err := api.New().AddDateFrom("2015-01-20").Get()
	if err != nil {
		logrus.Error(err)
	} else {
		logrus.Info(json)
	}

Set the base currency:
By default, the base currency is set to Euro (EUR), but it can be changed:

json, err := api.New(true).SetBaseCurrency("USD").Get()
	if err != nil {
		logrus.Error(err)
	} else {
		logrus.Info(json)
	}

Fetch specific rates:
If you do not want all current rates, it's possible to specify only the currencies you want using AddRate(). The following code fetches only the exchange rate between USD and EUR:

json, err := api.New(true).AddRate("USD").Get()
	if err != nil {
		logrus.Error(err)
	} else {
		logrus.Info(json)
	}

Please refer to the API website for further information and full API docs.

3. API Reference:

The following API reference lists the publicly-available methods for the

exchange-rates-api Reference:

AddDateFrom( from string ):
Set the date from which to retrieve historic rates. from should be an ISO 8601 date.

GetDateFrom():
Returns the specified date from which to retrieve historic rates. Returns "" if none is specified.

RemoveDateFrom():
Removes the specified start date for the retrieval of historic rates.

AddDateTo( to string):
Set the end date for the retrieval of historic rates. to should be an ISO 8601 date.

GetDateTo():
Returns the specified end date for the retrieval of historic rates. Returns "" if none is specified.

RemoveDateTo():
Removes the specified end date for the retrieval of historic rates.

ValidateCurrency( currency string ):
Checks if a specific currency code is supported. currency should be passed as an ISO 4217 code (e.g. EUR).
Returns nil if supported, or error if not.

ValidateDateFormat( date string ):
Checks if a specific date is supported. date should be passed as an ISO 8601 notation (e.g. YYYY-MM-DD).
Returns nil if supported, or error if not.

SetBaseCurrency( currency string):
Set the base currency to be used for exchange rates. currency should be passed an ISO 4217 code (e.g. EUR).
currency must be one of the supported currency codes.

GetBaseCurrency():
Returns the currently specified base currency. If SetBaseCurrency() hasn't been called, this will return the default base currency EUR.

AddRate( currency string ):
Adds a new currency to be retrieved. currency should be passed an ISO 4217 code (e.g. EUR).
currency must be one of the supported currency codes.
If no rates are added, all rates will be returned.

RemoveRate( currency string ):
Removes a currency that has already been added to the retrieval list. currency should be passed an ISO 4217 code (e.g. EUR).
currency must be one of the supported currency codes.

Get():
Send off the request to the API and return a raw JSON response.

GetSupportedCurrencies():
Returns a slice of supported currency codes.

GetSupportedCurrenciesInString(string delimiter):
Returns a string of supported currency codes separated by delimiter.

GetRates():
Returns a slice of the currently specified rates to retrieve.

GetRatesInString(string delimiter):
Returns a string of currently specified rates to retrieve separated by delimiter.

GetBaseCurrency():
Returns the base currency of the request. If not base currency was specified using SetBaseCurrency on the request, this will return the default (EUR).

4. Supported Currencies:

The library supports any currency currently available on the European Central Bank's web service, which at the time of the latest release are as follows:

Australian Dollar (AUD)
Brazilian Real (BRL)
British Pound Sterline (GBP)
Bulgarian Lev (BGN)
Canadian Dollar (CAD)
Chinese Yuan Renminbi (CNY)
Croatian Kuna (HRK)
Czech Koruna (CZK)
Danish Krone (DKK)
Euro (EUR)
Hong Kong Dollar (HKD)
Hungarian Forint (HUF)
Icelandic Krona (ISK)
Indonesian Rupiah (IDR)
Indian Rupee (INR)
Israeli Shekel (ILS)
Japanese Yen (JPY)
Malaysian Ringgit (MYR)
Mexican Peso (MXN)
New Zealand Dollar (NZD)
Norwegian Krone (NOK)
Philippine Peso (PHP)
Polish Zloty (PLN)
Romanian Leu (RON)
Russian Rouble (RUB)
Singapore Dollar (SGD)
South African Rand (ZAR)
South Korean Won (KRW)
Swedish Krona (SEK)
Swiss Franc (CHF)
Thai Baht (THB)
Turkish Lira (TRY)
US Dollar (USD)

5. Bugs & Features:

If you have spotted any bugs, or would like to request additional features from the library, please file an issue via the Issue Tracker on the project's Github page: https://github.com/error2215/exchange-rates-api/issues.

About

An unofficial SDK for the ExchangeRatesAPI. https://exchangeratesapi.io/

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages