UNPKG

5.65 kBMarkdownView Raw
1
2Node JavaScript wrapper for [the currencylayer API](https://currencylayer.com/).
3
4Supports both traditional callbacks and Promises/A+.
5
6---
7
8## Installation
9 npm install currency-conversion [--save]
10
11
12## Configuration
13
14Before using the currencylayer API client you have to setup your account and obtain your API Access Key.
15You can get it by signing up at [https://currencylayer.com/product](https://currencylayer.com/product).
16
17---
18
19## Usage
20
21The general API is documented here: [https://currencylayer.com/documentation](https://currencylayer.com/documentation).
22You can find parameters, result set definitions and status codes documented here as well.
23
24
25### Setup
26
27 var API = require('currency-conversion');
28 var api = new API({
29 access_key: [ACCESS_KEY],
30 secure: [true|false] (Optional, defaults to false)
31 });
32
33#### Optional Parameters
34
35##### Secure (only available for Basic, Pro and Enterprise accounts)
36Boolean value to indicate if the calls to the API should use a secure protocol or insecure (HTTP/HTTPS). Defaults to false (HTTP, insecure).
37
38---
39
40## Callbacks vs. Promises
41
42The Promises/A+ implementation used for this is this excellent bare bones library:
43[https://www.npmjs.com/package/promise](https://www.npmjs.com/package/promise)
44
45The language-detection library supports either mode and use of either one is not mutually exclusive to the alternative, so it's possible to use one exclusively or a combination, even in the same call, both the callback will be called and the promise handlers invoked.
46
47---
48
49## API Overview
50All endpoints in the public API is available through this library.
51
52- live
53- list
54- change
55- convert
56- historical
57- timeframe
58
59---
60
61## Live
62Takes a simple string and detects the language with a list of detections.
63
64###### Define Query
65
66 var liveQuery = {
67 source: 'SGD',
68 currencies: ['USD', 'THB']
69 };
70
71###### Simple Request (using Callback)
72
73 api.live(liveQuery, function (err, result) {
74 if (err) {
75 return console.log('Live Callback (Error): ' + JSON.stringify(err));
76 }
77 console.log('Live Callback (Result): ' + JSON.stringify(result));
78 });
79
80###### Response
81 {
82 "success": true,
83 "terms": "https://currencylayer.com/terms",
84 "privacy": "https://currencylayer.com/privacy",
85 "currencies": {
86 "AED": "United Arab Emirates Dirham",
87 "AFN": "Afghan Afghani",
88 "ALL": "Albanian Lek",
89 ...
90 }
91 }
92
93#### Automatic Refresh
94
95The Live API accepts a third argument which contains the options for the operation of the API.
96
97```
98var liveOptions = {
99 refresh_rate: [interval in milliseconds]
100};
101```
102
103When the refresh_rate option is added, the API will update automatically and invoke the callbacks and promises accordingly. The example application illustrates this.
104
105Internally the API uses the ETag and Date headers in the responses and If-None-Match and If-Modified-Since correspondingly on the requests. This reduces the payload sent from the server by utilising the 304 HTTP codes to use data from the internal cache instead of retrieving identical values from the server.
106
107[https://currencylayer.com/documentation](https://currencylayer.com/documentation)
108
109---
110
111## Example Application
112
113In the [rootdir]/example directory there is a fully functional application which runs all requests against all the endpoints in the API, the examples above can be seen there as source code.
114
115The example application uses a process.env variable to hold the access key.
116
117For running in development environments, it's easy to use the [https://www.npmjs.com/package/dotenv](https://www.npmjs.com/package/dotenv) to load variables from a local file into the environment.
118
119---
120
121## Tests [![Travis](https://travis-ci.org/pmoelgaard/currency-conversion.svg?branch=master)](Travis)
122
123The tests are written for any NodeJS testing library, but has been run and targeted at the [https://mochajs.org/](https://mochajs.org/) testing library.
124
125---
126
127## Customer Support
128
129Need any assistance? [Get in touch with Customer Support](mailto:support@apilayer.net?subject=%5Blanguagelayer%5D).
130
131---
132
133## Updates
134Stay up to date by following [@apilayernet](https://twitter.com/apilayernet) on Twitter.
135
136---
137
138## Legal
139
140All usage of the languagelayer website, API, and services is subject to the [languagelayer Terms & Conditions](https://languagelayer.com/terms) and all annexed legal documents and agreements.
141
142---
143
144## Author
145Peter Andreas Moelgaard ([GitHub](https://github.com/pmoelgaard), [Twitter](https://twitter.com/petermoelgaard))
146
147---
148
149## License
150Licensed under the The MIT License (MIT)
151
152Copyright (©) 2016 Peter Andreas Moelgaard & apilayer
153
154Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
155
156The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
157
158THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\No newline at end of file