@public-js/exchange-rates
v0.0.2
Published
An extensive exchange rates API client supporting multiple services for best results
Downloads
29
Maintainers
Readme
@public-js/exchange-rates
An extensive exchange rates API client supporting multiple services for best results
Installing
Add the package to your project by running:
npm i @public-js/exchange-rates
Usage
Instantiate a class:
import { ExchangeRates } from '@public-js/exchange-rates';
const service = await new ExchangeRates({
OpenexchangeratesOrg: env.OXR_API_KEY,
ExchangerateHost: true,
});
Fetch the latest rates:
const rates = await service.getRates('USD', ['EUR', 'CNY']);
console.log(rates); // {"CNY":6.8936,"EUR":0.901185}
Fetch the historical rates:
const rates = await service.getRates('USD', ['EUR', 'CNY'], '2023-01-15');
console.log(rates); // {"CNY":6.8936,"EUR":0.901185}
Supported services
OpenexchangeratesOrg
– has a limited free planExchangeratesapiIo
– has a limited free planCurrencyapiCom
– has a limited free planExchangerateHost
– free to use, accepts donationsExchangerateApiCom
– has a limited free plan
Note The majority of services require an API key/token to work. This doesn't apply to ExchangerateHost, which needs any truthy value provided as a key to be active.
How does it work
When the ExchangeRates
class is instantiated it requires a configuration object to be provided.
The said object has to list the desired services and their respective API keys/tokens.
Since some of the services don't handle unsupported currency codes well, they will be queried for a full rates list which then gets filtered out. This process is repeated for each service until all of them are queried or the required currencies are fetched.
The order of the services is listed in the "Supported services" section, it can't be altered at a time and is pre-defined based on data reliability, precision and currency coverage. It is possible however to enable only part of the supported services by providing only some of the API keys.
Resources
License
MIT, full license text. Read more about it on TLDRLegal.