|
| 1 | +# AbstractAPI javascript-exchange-rates library |
| 2 | + |
| 3 | +Integrate the powerful [Exchange Rates API from Abstract](https://www.abstractapi.com/exchange-rate-api) in your Javascript or NodeJS project in a few lines of code. |
| 4 | + |
| 5 | +The Exchange Rate API is an REST API that allows you to: |
| 6 | + |
| 7 | +- look up the latest exchange rates for 80+ currencies via the *live* endpoint |
| 8 | +- get historical exchange rates using the *historical* endpoint |
| 9 | +- convert an arbitrary amount from one currency to another using the *convert* endpoint |
| 10 | + |
| 11 | +It's very simple to use: you only need to submit your API key and a currency symbole (such as "USD"), and the API will respond with current exchange rate, historical data, or convertion rates. |
| 12 | + |
| 13 | +# Documentation |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +You can install **javascript-exchange-rates** via npm, from our CDN, or download the source into your project. |
| 18 | + |
| 19 | +### ES6 |
| 20 | + |
| 21 | +Download and install the library from npm: |
| 22 | + |
| 23 | +``` |
| 24 | +npm install @abstractapi/javascript-exchange-rates --save |
| 25 | +``` |
| 26 | + |
| 27 | +In your project, import it and configure your `API_KEY`: |
| 28 | + |
| 29 | +```js |
| 30 | +import {AbstractExchangeRates} from 'javascript-exchange-rates' |
| 31 | + |
| 32 | +AbstractExchangeRates.configure('API_KEY') |
| 33 | +``` |
| 34 | + |
| 35 | +### Browser, from the CDN |
| 36 | + |
| 37 | +You can have the browser download the library from its closest location through jsDeliver CDN: |
| 38 | + |
| 39 | +```js |
| 40 | +<script src="https://cdn.jsdelivr.net/npm/@abstractapi/javascript-core@latest/dist/javascript-core.js"></script> |
| 41 | +<script src="https://cdn.jsdelivr.net/npm/@abstractapi/javascript-exchange-rates@latest/dist/javascript-exchange-rates.js"></script> |
| 42 | +<script> |
| 43 | + AbstractExchangeRates.configure('API_KEY'); |
| 44 | + |
| 45 | + // use the library |
| 46 | +</script> |
| 47 | +``` |
| 48 | + |
| 49 | +### Browser, from the built file |
| 50 | + |
| 51 | +You can build the library yourself, or get the already built file from the `dist` directory and load it: |
| 52 | + |
| 53 | +```js |
| 54 | +<script src="dist/javascript-exchange-rates.js"></script> |
| 55 | +<script> |
| 56 | + AbstractExchangeRates.configure('API_KEY'); |
| 57 | + |
| 58 | + // use the library |
| 59 | +</script> |
| 60 | +``` |
| 61 | + |
| 62 | +## API key |
| 63 | + |
| 64 | +Get your API key for free and without hassle from the [Abstact website](https://app.abstractapi.com/users/signup?target=/api/exchange-rates/pricing/select). |
| 65 | + |
| 66 | +## Quickstart |
| 67 | + |
| 68 | +AbstractAPI **javascript-exchange-rates** library returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) so you can use one of the following approaches: |
| 69 | + |
| 70 | +### Async/Await |
| 71 | + |
| 72 | +```js |
| 73 | +async function getLiveRates(currency) { |
| 74 | + let response = await AbstractExchangeRates.live(currency); |
| 75 | + console.log(response); |
| 76 | +} |
| 77 | + |
| 78 | +async function getHistoricalRates(currency, date) { |
| 79 | + let response = await AbstractExchangeRates.historical(currency, date); |
| 80 | + console.log(response); |
| 81 | +} |
| 82 | + |
| 83 | +async function getConvertion(currency, target) { |
| 84 | + let response = await AbstractExchangeRates.convert(currency, target); |
| 85 | + console.log(response); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Using .then() |
| 90 | + |
| 91 | +```js |
| 92 | +function getLiveRates(currency) { |
| 93 | + AbstractExchangeRates.live(currency) |
| 94 | + .then(response => { |
| 95 | + console.log(response); |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +function getHistoricalRates(currency, date) { |
| 100 | + AbstractExchangeRates.historical(currency, date) |
| 101 | + .then(response => { |
| 102 | + console.log(response); |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +function getConvertion(currency, target) { |
| 107 | + AbstractExchangeRates.convert(currency, target) |
| 108 | + .then(response => { |
| 109 | + console.log(response); |
| 110 | + }) |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## API response |
| 115 | + |
| 116 | +The API response contains the following fields: |
| 117 | + |
| 118 | +### `live` response parameters |
| 119 | +| Parameter| Type| Details | |
| 120 | +| - | - | - | |
| 121 | +| base | String | The base currency used to get the exchange rates. | |
| 122 | +| last_updated | String | The Unix timestamp of when the returned data was last updated. | |
| 123 | +| exchange_rates | Object | A JSON Object containing each of the target currency as the key and its exchange rate versus the base currency as that key's value. | |
| 124 | + |
| 125 | +### `historical` response parameters |
| 126 | + |
| 127 | +| Parameter | Type | Details | |
| 128 | +| - | - | - | |
| 129 | +| base | String | The base currency used to get the exchange rates. | |
| 130 | +| date | String | The date the currencies were pulled from, per the successful request. | |
| 131 | +| exchange_rates | Object | A JSON Object containing each of the target currency as the key and its exchange rate versus the base currency as that key's value. | |
| 132 | + |
| 133 | +### `convert` response parameters |
| 134 | + |
| 135 | +| Parameter | Type | Details | |
| 136 | +| - | - | - | |
| 137 | +| base | String | The base currency used to get the exchange rates. | |
| 138 | +| target | String | The target currency that the base_amount was converted into. | |
| 139 | +| date | String | The date the currencies were pulled from, per the successful request. | |
| 140 | +| base_amount | Float | The amount of the base currency from the request. | |
| 141 | +| converted_amount | Float | The amount of the target currency that the base_amount has been converted into | |
| 142 | +| exchange_rate | Float | The exchange rate used to convert the base_amount from the base currency to the target currency | |
| 143 | + |
| 144 | +## Detailed documentation |
| 145 | + |
| 146 | +You will find additional information and request examples in the [Abstract help page](https://app.abstractapi.com/api/exchange-rates/documentation). |
| 147 | + |
| 148 | +## Getting help |
| 149 | + |
| 150 | +If you need help installing or using the library, please contact [Abstract's Support](https://app.abstractapi.com/api/exchange-rates/support). |
| 151 | + |
| 152 | +For bug report and feature suggestion, please use [this repository issues page](https://github.com/abstractapi/javascript-exchange-rates/issues). |
| 153 | + |
| 154 | +# Contribution |
| 155 | + |
| 156 | +Contributions are always welcome, as they improve the quality of the libraries we provide to the community. |
| 157 | + |
| 158 | +Please provide your changes covered by the appropriate unit tests, and post them in the [pull requests page](https://github.com/abstractapi/javascript-exchange-rates/pulls). |
| 159 | + |
| 160 | +## NPM |
| 161 | + |
| 162 | +### Installation |
| 163 | + |
| 164 | +Run `npm install` in the command line to install the dependencies. To update those dependencies you need to run `npm update`. |
| 165 | + |
| 166 | +### Building |
| 167 | + |
| 168 | +To build the library and generate the minified file in the *dist* directory, you need to run `npm run build`. |
| 169 | + |
| 170 | +To build the lib, you need to run `npm run build:lib`. |
| 171 | + |
| 172 | +### Test |
| 173 | + |
| 174 | +To run the test suite, you need the API key from the abstract website and you can run: |
| 175 | + |
| 176 | + EXCHANGE_RATES_API_KEY=(your key here) npm run test |
| 177 | + |
0 commit comments