Newer
Older
# easy-currencies
[](https://npmjs.org/package/easy-currencies "View this project on npm")
[](https://travis-ci.org/scharkee/easy-currencies)
[](https://coveralls.io/github/Scharkee/easy-currencies?branch=master)
[](https://david-dm.org/scharkee/easy-currencies)
Convert currencies with ease! Five exchange rate providers to choose from, others easily implementable.
## Features
- Easily convert currencies using one of the five built-in API providers
- Two modes of operation:
- Easy mode - no configuration or API keys required at all
- Custom mode - choose one or more providers, use key-gated providers.
- Provider fallbacks - automatic switching of active providers in the case of failure
Matas Rastenis
committed
```bash
$ npm install easy-currencies
```
## Usage (Easy/chain mode)
Easy/chain mode does not require initialization, and thus uses the default, no API key-required provider (exchangeratesapi.io)
```js
// CommonJS
const { Convert } = require("easy-currencies");
// ES6
import { Convert } from "easy-currencies";
let value = await Convert(15)
.from("USD")
.to("EUR");
console.log(value); // converted value
```
## Usage (custom mode)
Default provider initialization, no key needed
```js
import { Converter } from "easy-currencies";
let converter = new Converter();
let value = await converter.convert(15, "USD", "EUR");
## Usage (raw mode / cached mode)
Use this to get a JSON of conversion rates from your current provider.
```js
import { Convert } from "easy-currencies";
let convert = await Convert()
.from("USD")
.fetch();
console.log(convert.rates); // fetched conversion rates for main currencies with regard to set base currency
```
This also allows for cached conversion:
```js
import { Convert } from "easy-currencies";
let convert = await Convert()
.from("USD")
.fetch();
// use the fetched rates: (does not use the current provider's API anymore)
let value1 = await convert.amount(10).to("GBP");
await convert.from("USD").fetch(); // refresh rates
// or await convert.from("GBP").fetch() to switch base currency
let value2 = await convert.amount(10).to("GBP");
```
Custom single provider initialization
```js
import { Converter } from "easy-currencies";
let converter = new Converter("OpenExchangeRates", "API_KEY");
let value = await converter.convert(15, "USD", "EUR");
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
console.log(value); // converted value
```
Custom multiple provider initialization
```js
import { Converter } from "easy-currencies";
let converter = new Converter(
{ name: "OpenExchangeRates", key: "API_KEY" },
{ name: "AlphaVantage", key: "API_KEY" }
{ name: "Fixer", key: "API_KEY" }
);
let value = await converter.convert(15, "USD", "EUR");
console.log(value); // converted value
```
## Supported providers and API keys
The list of supoprted exchange rate providers is as follows:
1. [ExchangeRatesAPI](https://exchangeratesapi.io/) (default, no API key required)
2. [CurrencyLayer](https://currencylayer.com/) (requires an api key with base currency supoprt)
3. [OpenExchangeRates](https://openexchangerates.org/)
4. [AlphaVantage](https://www.alphavantage.co/)
5. [Fixer](https://fixer.io/) (requires an api key with base currency supoprt)
## API
Check out the [api reference docs.](https://scharkee.github.io/easy-currencies/)
The list of configured (active) providers can be accessed like so:
```js
import { Converter } from "easy-currencies";
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
console.log(converter.providers);
/**
* [{
* endpoint: {
* base: "https://openexchangerates.org/api/latest.json?app_id=%KEY%",
* single: "&base=%FROM%",
* multiple: "&base=%FROM%"
* },
* key: "API_KEY",
* handler: function(data) {
* return data.rates;
* },
* errors: {
* 401: "Invalid API key!"
* },
* errorHandler: function(data) {
* return data.status;
* }
* }]
*/
```
The current active provider can be retrieved like this:
```js
import { Converter } from "easy-currencies";
console.log(converter.activeProvider()); // ...provider data
```
### Automatic provider fallbacks
Upon creation of a converter, a default provider that does not require any API keys is automatically inserted into the list of active providers as a primary fallback. It always has lower priority than the providers the converter was initialized with.
If a provider is well defined(all possible errors are registered properly), a conversion error will log the mapped error, and remove the provider from the active providers list. The conversion flow will attempt to resume by repeating the conversion using a different active provider.
If there are no more providers to fall back on, the converter throws the error. Moreover, if the error is not registered (unhandled error), it will be thrown as well.
### Adding custom providers
Custom provider definitions can be added as such:
```js
import { Converter } from "easy-currencies";
let converter = new Converter();
converter.add("MyProvider", {
// the name of the custom provider
endpoint: {
base: "http://myprovider.net/api/live?access_key=%KEY%", // the base endpoint of the conversion API, with %KEY% being the api key's slot
single: "&source=%FROM%", // the string that will be appended to the base endpoint, with %FROM% being the base currency abbreviation
multiple: "&source=%FROM%¤cies=%TO%" // the string that will be appended to the base endpoint when fetching specific currencies, with %TO% being the target currencies, separated by ','
},
key: "API_KEY", // your api key
handler: function(data) {
// the function that takes the JSON data returned by the API and returns the rate key-value object
return data.rates;
},
errors: {
// key-value object of common errors and their text representations
101: "Invalid API key!"
201: "Invalid base currency!"
},
errorHandler: function(data) {
// the function that takes the JSON error data and returns the error status (could be a HTTP status or a custom API-layer status)
}
});
```
Multiple providers can be added with addMultiple:
```js
import { Converter } from "easy-currencies";
let converter = new Converter();
converter.add([
{ name: "Name1", provider: provider1 },
{ name: "Name2", provider: provider2 }
]);
```
Submit bugs and feature requests through the project's issue tracker:
[](https://github.com/Scharkee/easy-currencies/issues)