https://github.com/mattevans/dinero
📈 💰 A golang interface for the Open Exchange Rates API
https://github.com/mattevans/dinero
currency exchange-rates forex go golang golang-library open-exchange
Last synced: 3 months ago
JSON representation
📈 💰 A golang interface for the Open Exchange Rates API
- Host: GitHub
- URL: https://github.com/mattevans/dinero
- Owner: mattevans
- License: mit
- Created: 2016-12-15T19:00:35.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-03-31T12:22:17.000Z (over 3 years ago)
- Last Synced: 2024-08-10T05:09:23.103Z (about 1 year ago)
- Topics: currency, exchange-rates, forex, go, golang, golang-library, open-exchange
- Language: Go
- Homepage: https://docs.openexchangerates.org/docs/
- Size: 54.7 KB
- Stars: 79
- Watchers: 7
- Forks: 9
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# dinero
[](https://godoc.org/github.com/mattevans/dinero)
[](https://travis-ci.org/mattevans/dinero)
[](https://goreportcard.com/report/github.com/mattevans/dinero)
[](https://github.com/mattevans/dinero/blob/master/LICENSE)dinero is a [Go](http://golang.org) client library for accessing the Open Exchange Rates API (https://docs.openexchangerates.org/docs/).
Any forex rates requested will be cached (in-memory), keyed by base currency. With a customisable expiry window, subsequent requests will use cached data or fetch fresh data accordingly.
Installation
-----------------`go get -u github.com/mattevans/dinero`
Usage
-----------------**Intialize**
```go
// Init dinero client passing....
// - your OXR app ID
// - base currency code for conversions to work from
// - your preferrerd cache expiry
client := NewClient(
os.Getenv("OPEN_EXCHANGE_APP_ID"),
"AUD",
20*time.Minute,
)
```---
## Currencies
**List**
```go
// List all currencies available.
rsp, err := client.Currencies.List()
if err != nil {
return err
}
``````json
[
{
"code": "INR",
"name": "Indian Rupee"
},
{
"code": "PYG",
"name": "Paraguayan Guarani"
},
{
"code": "AED",
"name": "United Arab Emirates Dirham"
},
...
}
```---
## Rates
**List**
```go
// List latest forex rates. This will use AUD (defined when intializing the client) as the base.
rsp, err := client.Rates.List()
if err != nil {
return err
}
``````json
{
"rates":{
"AED": 2.702388,
"AFN": 48.893275,
"ALL": 95.142814,
"AMD": 356.88691,
...
},
"base": "AUD"
}
```---
**Get**
```go
// Get latest forex rate for NZD. This will use AUD (defined when intializing the client) as the base.
rsp, err := client.Rates.Get("NZD")
if err != nil {
return err
}
``````
1.045545
```
---## Historical Rates
**List**
```go
historicalDate := time.Now().AddDate(0, -5, -3)// List historical forex rates. This will use AUD (defined when intializing the client) as the base.
rsp, err := client.HistoricalRates.List(historicalDate)
if err != nil {
return err
}
``````json
{
"rates":{
"AED": 2.702388,
"AFN": 48.893275,
"ALL": 95.142814,
"AMD": 356.88691,
...
},
"base": "AUD"
}
```---
**Get**
```go
historicalDate := time.Now().AddDate(0, -5, -3)// Get historical forex rate for NZD. This will use AUD (defined when intializing the client) as the base.
rsp, err := client.HistoricalRates.Get("NZD", historicalDate)
if err != nil {
return err
}
``````
1.045545
```---
**Change Base Currency**
You set a base currency when you the intialize dinero client. Should you wish to change this at anytime, you can call...
```go
client.Rates.SetBaseCurrency("USD")
```> NOTE: Changing the API `base` currency is available for Developer, Enterprise and Unlimited plan clients only.
---
Contributing
-----------------
If you've found a bug or would like to contribute, please create an issue here on GitHub, or better yet fork the project and submit a pull request!