node-currency-swap
Currency Exchange Rates library for nodejs
Motivation
node-currency-swap is designed to be a simple and universal exchange rate library with support for multiple providers. This library is heavily inspired from PHP Swap
Installation
npm install node-currency-swap
Usage
First, you need to add a provider to swap by using addProvider() method
var swap = require('node-currency-swap');
swap.addProvider(new swap.providers.GoogleFinance());
You can also add multiple providers
var swap = require('node-currency-swap');
swap.addProvider(new swap.providers.GoogleFinance());
swap.addProvider(new swap.providers.YahooFinance({timeout: 2000}));
swap.providers
To get the list of all providers
swap.providers;
quote(options, callback)
To retrieve the latest exchange rate for a currency pair asynchronously.
Arguments
options
- An object to specify options for quote. For complete list refer Options Section.callback(err, rate)
- A callback which returns error on any failure or rate array on success.
quoteSync(options)
To retrieve the latest exchange rate for a currency pair synchronously.
Arguments
options
- An object to specify options for quote. For complete list refer Options Section.
Examples
swap.quote({currency: 'USD/SAR'}, function (err, rate) {
console.log(rate[0].value);
console.log(rate[0].date);
console.log(rate[0].provider);
});
Synchronously in case of any error it throws an error which you should handle through try/catch
var rate = swap.quoteSync({currency: 'USD/SAR'});
console.log(rate[0].value);
console.log(rate[0].date);
console.log(rate[0].provider);
To fetch rate from all the added providers
var rates = swap.quoteSync({currency: 'USD/SAR', fetchMultipleRate: true});
rates.forEach(function(rate){
console.log(rate.value);
console.log(rate.date);
console.log(rate.provider);
});
To fetch rate from cache if available if not it fetch the rate from provider and store in cache
var rates = swap.quoteSync({currency: 'USD/SAR', cache: true});
rates.forEach(function(rate){
console.log(rate.value);
console.log(rate.date);
console.log(rate.provider);
});
Currencies are expressed as their ISO 4217 code.
swap.currencyCodes
Swap provides an object of currency codes so you can use it to avoid typos.
var rates = swap.quoteSync({
currency: {
baseCurrency: swap.currencyCodes.ISO_USD,
quoteCurrency: swap.currencyCodes.ISO_AED
}
});
Options
currency
- currency info to get exchange rate either as string USD/AED
or as object {baseCurrency:'USD', quoteCurrency:'AED'}
.fetchMultipleRate
- if true, fetch rate from all the added providers. (default: false
)cache
- if true, it tries to fetch rate from cache if available otherwise fetch rate from added provider and store it in cache. (default: false
)ttl
- time in ms
to retain rate in cache. (default: 360000
) 1 hour
Providers
swap.addProvider(new swap.providers.EuropeanCentralBank(options));
- Google Finance
Supports multiple currencies as base and quote currencies.
swap.addProvider(new swap.providers.GoogleFinance(options));
- Open Exchange Rates
Supports only USD as base currency for the free version and multiple ones for the enterprise version.
swap.addProvider(new swap.providers.OpenExchangeRates(options));
- Xignite
You must have access to the
XigniteGlobalCurrencies
API.
Supports multiple currencies as base and quote currencies.
swap.addProvider(new swap.providers.Xignite(options));
- Yahoo Finance
Supports multiple currencies as base and quote currencies.
swap.addProvider(new swap.providers.YahooFinance(options));
swap.addProvider(new swap.providers.NationalBankOfRomania(options));
- Currency Layer
Supports multiple currencies as base and quote currencies.
swap.addProvider(new swap.providers.CurrencyLayer(options));
Copyright and license
Code released under the Apache Software License v2.0