Giter Site home page Giter Site logo

over-engineer / exchange-rates Goto Github PK

View Code? Open in Web Editor NEW
17.0 2.0 4.0 515 KB

๐Ÿ’ฐ๐ŸŒ An unofficial node.js wrapper for the awesome and free ratesapi.io

License: MIT License

JavaScript 100.00%
nodejs node-js exchange-rates foreign-exchange-rates currency-exchange-rates currency-rates npm npm-package

exchange-rates's Introduction

icon

Exchange Rates

npm NPM Travis (.org)

๐Ÿ’ฐ๐ŸŒ An unofficial node.js wrapper for the awesome and free ratesapi.io, which provides exchange rate lookups courtesy of the Central European Bank.

Table of Contents

๐Ÿ“ฆ Installation

$ npm i exchange-rates-api

โŒจ๏ธ Usage

Once you have installed the npm package you can start using it immediately. Rates API does not require you to sign up, generate API keys etc.

Latest & specific date rates

const { exchangeRates } = require('exchange-rates-api');

(async () => {
    // Get the latest exchange rates
    await exchangeRates().latest().fetch();                             // {THB: 34.978, PHP: 58.159, โ€ฆ, HUF: 323.58}

    // Get historical rates for any day since 1999
    await exchangeRates().at('2018-03-26').fetch();                     // {THB: 38.66, PHP: 64.82, โ€ฆ, HUF: 312.73}

    // By default, the base currency is EUR, but it can be changed
    await exchangeRates().latest().base('USD').fetch();                 // {THB: 30.9348191386, โ€ฆ, HUF: 286.1767046962}

    // Request specific exchange rates
    await exchangeRates().latest().symbols(['USD', 'GBP']).fetch();     // {USD: 1.1307, GBP: 0.89155}

    // Request one specific exchange rate
    await exchangeRates().latest().symbols('USD').fetch();              // 1.1307
})();

Rates history

const { exchangeRates } = require('exchange-rates-api');

(async () => {
    // Get historical rates for a time period
    await exchangeRates().from('2018-01-01').to('2018-09-01').fetch();
    // outputs: { '2018-02-28': { THB: 38.613, โ€ฆ, HUF: 313.97 }, โ€ฆ, { '2018-06-07': { โ€ฆ } } }

    // Limit results to specific exchange rates to save bandwidth
    await exchangeRates()
        .from('2018-01-01').to('2018-09-01')
        .symbols(['ILS', 'JPY'])
        .fetch();

    // Quote the historical rates against a different currency
    await exchangeRates().from('2018-01-01').to('2018-09-01').base('USD');
})();

Different ways to pass a date

const { exchangeRates } = require('exchange-rates-api');

(async () => {
    // Pass an YYYY-MM-DD (ISO 8601) string
    await exchangeRates().at('2018-09-01').fetch();

    // Pass another string
    await exchangeRates().at('September 1, 2018').fetch();

    // Pass a Date object
    await exchangeRates().at(new Date(2019, 8, 1)).fetch();
})();

Currencies object

const { exchangeRates, currencies } = require('exchange-rates-api');

(async () => {
    await exchangeRates().latest()
        .base(currencies.USD)
        .symbols([currencies.EUR, currencies.GBP])
        .fetch();
})();

Average rate for a specific time period

const { exchangeRates } = require('exchange-rates-api');

(async () => {
    // Find the average exchange rate for January, 2018
    await exchangeRates()
        .from('2018-01-01').to('2018-01-31')
        .base('USD').symbols('EUR')
        .avg();     // 0.8356980613403501

    // Set the number of decimal places
    await exchangeRates()
        .from('2018-01-01').to('2018-01-31')
        .base('USD').symbols(['EUR', 'GBP'])
        .avg(2);    // { EUR: 0.84, GBP: 0.74 }
})();

Convert

const { convert } = require('exchange-rates-api');

(async () => {
    let amount = await convert(2000, 'USD', 'EUR', '2018-01-01');
    console.log(amount);    // 1667.6394564000002
})();

API URL

const { exchangeRates } = require('exchange-rates-api');

// Grab the url we're going to request
let url = exchangeRates()
    .from('2018-01-01').to('2018-09-01')
    .base('USD').symbols(['EUR', 'GBP'])
    .url;

console.log(url);
// https://api.ratesapi.io/history?start_at=2018-01-01&end_at=2018-09-01&base=USD&symbols=EUR,GBP

Using a different API

const { exchangeRates } = require('exchange-rates-api');

/* You can use it with pretty much any Fixer.io-compatible API */

(async () => {
    await exchangeRates()
        .setApiBaseUrl('https://api.exchangerate.host')
        .latest()
        .fetch();    // {THB: 34.978, PHP: 58.159, โ€ฆ, HUF: 323.58}
})();

Error handling

const { exchangeRates } = require('exchange-rates-api');

/* `ExchangeRatesError` and `TypeError` are explicitly thrown
 * sometimes, so you might want to handle them */

// async/await syntax
(async () => {
    try {
        /* This will throw an `ExchangeRateError` with the error
         * message 'Cannot get historical rates before 1999' */
        let rates = await exchangeRates().at('1990-01-01').fetch();
    } catch (error) {
        // Handle the error
    }
})();

// Promises syntax
exchangeRates().at('1990-01-01').fetch()
    .then(rates => {})
    .catch(error => {
        // Handle the error
    });

๐Ÿ’ฐ 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)

๐Ÿž Bugs & Features

If you have spotted any bugs, or would like to request additional features from the library, please file an issue.

๐Ÿ“š Documentation

๐Ÿงช Unit Testing

There are a few basic unit tests in the test directory, but we should definitely write more

Development dependencies

  • Chai โ€” a BDD / TDD assertion library for node and the browser
  • Mocha โ€” a feature-rich JavaScript test framework running on Node.js and in the browser
  • fetch-mock โ€” allows mocking http requests made using fetch or a library imitating its api, such as node-fetch or fetch-ponyfill

๐Ÿ—„ Dependencies

  • date-fns โ€” Modern JavaScript date utility library
  • isomorphic-fetch โ€” Isomorphic WHATWG Fetch API, for Node & Browserify

๐Ÿ“– License

The MIT License, check the LICENSE file

exchange-rates's People

Contributors

dependabot[bot] avatar over-engineer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

exchange-rates's Issues

You have not supplied an API Access Key error

I am getting the following error when making the request exchangeRates().latest().base(currencies.USD).fetch(): Cannot convert undefined or null to object. Digging into this, the response coming back from the exchangerates.io service is:

{"success": false,"error": {"code": 101,"type": "missing_access_key","info": "You have not supplied an API Access Key. [Required format: access_key=YOUR_ACCESS_KEY]"}

It seems exchangerates.io is now requiring an access key. Also, some other features - e.g. setting base currency to USD - is now limited to paid-only versions of the service. See exchangeratesapi/exchangeratesapi#117 for more details.

Dates missing with historical data?

Noticed that dates (presumably when markets are closed) are missing from historical data. E.g.:

rates = await exchangeRates()
    .from('2020-01-01').to('2020-12-31')
    .symbols(['USD'])
    .fetch();
{
  '2020-01-02': { USD: 1.1193 }, // <- where is: 2020-01-02
  '2020-01-03': { USD: 1.1147 },
  '2020-01-06': { USD: 1.1194 }, // and 2020-01-05
  '2020-01-07': { USD: 1.1172 },
  '2020-01-08': { USD: 1.1115 },
  '2020-01-09': { USD: 1.111 },
  '2020-01-10': { USD: 1.1091 },
  '2020-01-13': { USD: 1.1126 },
  '2020-01-14': { USD: 1.1115 },
  '2020-01-15': { USD: 1.1142 },
  '2020-01-16': { USD: 1.1169 },
  '2020-01-17': { USD: 1.1108 },
  '2020-01-20': { USD: 1.1085 },
  '2020-01-21': { USD: 1.1115 },
  '2020-01-22': { USD: 1.1088 },
  '2020-01-23': { USD: 1.1091 },
  '2020-01-24': { USD: 1.1035 },
  '2020-01-27': { USD: 1.1025 },
  '2020-01-28': { USD: 1.1005 },
  '2020-01-29': { USD: 1.1001 },
  '2020-01-30': { USD: 1.1029 },
  '2020-01-31': { USD: 1.1052 },
  '2020-02-03': { USD: 1.1066 },
  '2020-02-04': { USD: 1.1048 },
  '2020-02-05': { USD: 1.1023 },
  '2020-02-06': { USD: 1.1003 },
  '2020-02-07': { USD: 1.0969 },
  '2020-02-10': { USD: 1.0951 },
  '2020-02-11': { USD: 1.0901 },
  '2020-02-12': { USD: 1.0914 },
  '2020-02-13': { USD: 1.0867 },
  '2020-02-14': { USD: 1.0842 },
  '2020-02-17': { USD: 1.0835 },
  '2020-02-18': { USD: 1.0816 },
  '2020-02-19': { USD: 1.08 },
  '2020-02-20': { USD: 1.079 },
  '2020-02-21': { USD: 1.0801 },
  '2020-02-24': { USD: 1.0818 },
  '2020-02-25': { USD: 1.084 },
  '2020-02-26': { USD: 1.0875 },
  '2020-02-27': { USD: 1.0964 },
  '2020-02-28': { USD: 1.0977 },
  '2020-03-02': { USD: 1.1122 },
  '2020-03-03': { USD: 1.1117 },
  '2020-03-04': { USD: 1.1125 },
  '2020-03-05': { USD: 1.1187 },
  '2020-03-06': { USD: 1.1336 },
  '2020-03-09': { USD: 1.1456 },
  '2020-03-10': { USD: 1.139 },
  '2020-03-11': { USD: 1.1336 },
  '2020-03-12': { USD: 1.124 },
  '2020-03-13': { USD: 1.1104 },
  '2020-03-16': { USD: 1.1157 },
  '2020-03-17': { USD: 1.0982 },
  '2020-03-18': { USD: 1.0934 },
  '2020-03-19': { USD: 1.0801 },
  '2020-03-20': { USD: 1.0707 },
  '2020-03-23': { USD: 1.0783 },
  '2020-03-24': { USD: 1.0843 },
  '2020-03-25': { USD: 1.0827 },
  '2020-03-26': { USD: 1.0981 },
  '2020-03-27': { USD: 1.0977 },
  '2020-03-30': { USD: 1.1034 },
  '2020-03-31': { USD: 1.0956 },
  '2020-04-01': { USD: 1.0936 },
  '2020-04-02': { USD: 1.0906 },
  '2020-04-03': { USD: 1.0785 },
  '2020-04-06': { USD: 1.0791 },
  '2020-04-07': { USD: 1.0885 },
  '2020-04-08': { USD: 1.0871 },
  '2020-04-09': { USD: 1.0867 },
  '2020-04-14': { USD: 1.0963 },
  '2020-04-15': { USD: 1.0903 },
  '2020-04-16': { USD: 1.0888 },
  '2020-04-17': { USD: 1.086 },
  '2020-04-20': { USD: 1.086 },
  '2020-04-21': { USD: 1.0837 },
  '2020-04-22': { USD: 1.0867 },
  '2020-04-23': { USD: 1.0772 },
  '2020-04-24': { USD: 1.08 },
  '2020-04-27': { USD: 1.0852 },
  '2020-04-28': { USD: 1.0877 },
  '2020-04-29': { USD: 1.0842 },
  '2020-04-30': { USD: 1.0876 },
  '2020-05-04': { USD: 1.0942 },
  '2020-05-05': { USD: 1.0843 },
  '2020-05-06': { USD: 1.0807 },
  '2020-05-07': { USD: 1.0783 },
  '2020-05-08': { USD: 1.0843 },
  '2020-05-11': { USD: 1.0824 },
  '2020-05-12': { USD: 1.0858 },
  '2020-05-13': { USD: 1.0875 },
  '2020-05-14': { USD: 1.0792 },
  '2020-05-15': { USD: 1.0798 },
  '2020-05-18': { USD: 1.0832 },
  '2020-05-19': { USD: 1.095 },
  '2020-05-20': { USD: 1.0958 },
  '2020-05-21': { USD: 1.1 },
  '2020-05-22': { USD: 1.0904 },
  '2020-05-25': { USD: 1.091 },
  '2020-05-26': { USD: 1.0975 },
  '2020-05-27': { USD: 1.0991 },
  '2020-05-28': { USD: 1.1016 },
  '2020-05-29': { USD: 1.1136 },
  '2020-06-01': { USD: 1.1116 },
  '2020-06-02': { USD: 1.1174 },
  '2020-06-03': { USD: 1.1194 },
  '2020-06-04': { USD: 1.125 },
  '2020-06-05': { USD: 1.133 },
  '2020-06-08': { USD: 1.1285 },
  '2020-06-09': { USD: 1.1294 },
  '2020-06-10': { USD: 1.1375 },
  '2020-06-11': { USD: 1.1348 },
  '2020-06-12': { USD: 1.1304 },
  '2020-06-15': { USD: 1.1253 },
  '2020-06-16': { USD: 1.1308 },
  '2020-06-17': { USD: 1.1232 },
  '2020-06-18': { USD: 1.1222 },
  '2020-06-19': { USD: 1.121 },
  '2020-06-22': { USD: 1.1213 },
  '2020-06-23': { USD: 1.1318 },
  '2020-06-24': { USD: 1.128 },
  '2020-06-25': { USD: 1.12 },
  '2020-06-26': { USD: 1.1213 },
  '2020-06-29': { USD: 1.1284 },
  '2020-06-30': { USD: 1.1198 },
  '2020-07-01': { USD: 1.12 },
  '2020-07-02': { USD: 1.1286 },
  '2020-07-03': { USD: 1.1224 },
  '2020-07-06': { USD: 1.1325 },
  '2020-07-07': { USD: 1.129 },
  '2020-07-08': { USD: 1.1286 },
  '2020-07-09': { USD: 1.1342 },
  '2020-07-10': { USD: 1.1276 },
  '2020-07-13': { USD: 1.1329 },
  '2020-07-14': { USD: 1.1375 },
  '2020-07-15': { USD: 1.1444 },
  '2020-07-16': { USD: 1.1414 },
  '2020-07-17': { USD: 1.1428 },
  '2020-07-20': { USD: 1.1448 },
  '2020-07-21': { USD: 1.1443 },
  '2020-07-22': { USD: 1.1578 },
  '2020-07-23': { USD: 1.1569 },
  '2020-07-24': { USD: 1.1608 },
  '2020-07-27': { USD: 1.176 },
  '2020-07-28': { USD: 1.1717 },
  '2020-07-29': { USD: 1.1725 },
  '2020-07-30': { USD: 1.1743 },
  '2020-07-31': { USD: 1.1848 },
  '2020-08-03': { USD: 1.1726 },
  '2020-08-04': { USD: 1.1765 },
  '2020-08-05': { USD: 1.1854 },
  '2020-08-06': { USD: 1.1843 },
  '2020-08-07': { USD: 1.1817 },
  '2020-08-10': { USD: 1.1763 },
  '2020-08-11': { USD: 1.1783 },
  '2020-08-12': { USD: 1.1771 },
  '2020-08-13': { USD: 1.1833 },
  '2020-08-14': { USD: 1.1813 },
  '2020-08-17': { USD: 1.1853 },
  '2020-08-18': { USD: 1.1906 },
  '2020-08-19': { USD: 1.1933 },
  '2020-08-20': { USD: 1.185 },
  '2020-08-21': { USD: 1.1769 },
  '2020-08-24': { USD: 1.1847 },
  '2020-08-25': { USD: 1.1814 },
  '2020-08-26': { USD: 1.1789 },
  '2020-08-27': { USD: 1.1806 },
  '2020-08-28': { USD: 1.1915 },
  '2020-08-31': { USD: 1.194 },
  '2020-09-01': { USD: 1.1987 },
  '2020-09-02': { USD: 1.1861 },
  '2020-09-03': { USD: 1.1813 },
  '2020-09-04': { USD: 1.1842 },
  '2020-09-07': { USD: 1.1824 },
  '2020-09-08': { USD: 1.1785 },
  '2020-09-09': { USD: 1.1773 },
  '2020-09-10': { USD: 1.1849 },
  '2020-09-11': { USD: 1.1854 },
  '2020-09-14': { USD: 1.1876 },
  '2020-09-15': { USD: 1.1892 },
  '2020-09-16': { USD: 1.1869 },
  '2020-09-17': { USD: 1.1797 },
  '2020-09-18': { USD: 1.1833 },
  '2020-09-21': { USD: 1.1787 },
  '2020-09-22': { USD: 1.174 },
  '2020-09-23': { USD: 1.1692 },
  '2020-09-24': { USD: 1.1645 },
  '2020-09-25': { USD: 1.1634 },
  '2020-09-28': { USD: 1.167 },
  '2020-09-29': { USD: 1.1702 },
  '2020-09-30': { USD: 1.1708 },
  '2020-10-01': { USD: 1.1752 },
  '2020-10-02': { USD: 1.173 },
  '2020-10-05': { USD: 1.1768 },
  '2020-10-06': { USD: 1.1795 },
  '2020-10-07': { USD: 1.177 },
  '2020-10-08': { USD: 1.1765 },
  '2020-10-09': { USD: 1.1795 },
  '2020-10-12': { USD: 1.1799 },
  '2020-10-13': { USD: 1.1787 },
  '2020-10-14': { USD: 1.175 },
  '2020-10-15': { USD: 1.1698 },
  '2020-10-16': { USD: 1.1741 },
  '2020-10-19': { USD: 1.1785 },
  '2020-10-20': { USD: 1.181 },
  '2020-10-21': { USD: 1.1852 },
  '2020-10-22': { USD: 1.1821 },
  '2020-10-23': { USD: 1.1856 },
  '2020-10-26': { USD: 1.1819 },
  '2020-10-27': { USD: 1.1832 },
  '2020-10-28': { USD: 1.1727 },
  '2020-10-29': { USD: 1.1704 },
  '2020-10-30': { USD: 1.1698 },
  '2020-11-02': { USD: 1.1652 },
  '2020-11-03': { USD: 1.1702 },
  '2020-11-04': { USD: 1.1721 },
  '2020-11-05': { USD: 1.1855 },
  '2020-11-06': { USD: 1.187 },
  '2020-11-09': { USD: 1.1883 },
  '2020-11-10': { USD: 1.1808 },
  '2020-11-11': { USD: 1.1766 },
  '2020-11-12': { USD: 1.1791 },
  '2020-11-13': { USD: 1.1815 },
  '2020-11-16': { USD: 1.183 },
  '2020-11-17': { USD: 1.1882 },
  '2020-11-18': { USD: 1.1868 },
  '2020-11-19': { USD: 1.1832 },
  '2020-11-20': { USD: 1.1863 },
  '2020-11-23': { USD: 1.1901 },
  '2020-11-24': { USD: 1.1865 },
  '2020-11-25': { USD: 1.189 },
  '2020-11-26': { USD: 1.19 },
  '2020-11-27': { USD: 1.1922 },
  '2020-11-30': { USD: 1.198 },
  '2020-12-01': { USD: 1.1968 },
  '2020-12-02': { USD: 1.2066 },
  '2020-12-03': { USD: 1.2151 },
  '2020-12-04': { USD: 1.2159 },
  '2020-12-07': { USD: 1.2128 },
  '2020-12-08': { USD: 1.2114 },
  '2020-12-09': { USD: 1.2109 },
  '2020-12-10': { USD: 1.2115 },
  '2020-12-11': { USD: 1.2127 },
  '2020-12-14': { USD: 1.2162 },
  '2020-12-15': { USD: 1.214 },
  '2020-12-16': { USD: 1.2189 },
  '2020-12-17': { USD: 1.2246 },
  '2020-12-18': { USD: 1.2259 },
  '2020-12-21': { USD: 1.2173 },
  '2020-12-22': { USD: 1.2239 },
  '2020-12-23': { USD: 1.2166 },
  '2020-12-24': { USD: 1.2193 },
  '2020-12-28': { USD: 1.2219 },
  '2020-12-29': { USD: 1.2259 },
  '2020-12-30': { USD: 1.2281 },
  '2020-12-31': { USD: 1.2271 }
}

There is data there for the days in question (e.g. https://api.ratesapi.io/api/2020-01-01), so unsure if this is a bug or some sort of undocumented behaviour.

Add more unit tests

There are some basic unit tests in the test directory, but we should definitely write more.

Convert with different API

How do you use convert function with using different api like;

let result = await exchangeRates()
        .setApiBaseUrl('https://api.exchangerate.host')
const { convert } = require('exchange-rates-api');

(async () => {
    let amount = await convert(2000, 'USD', 'EUR', '2018-01-01');
    console.log(amount);    // 1667.6394564000002
})();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.