diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc92c58 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.npmrc +node_modules/ +package-lock.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f1deecb --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2023 EnergypriceAPI + +Permission 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: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE 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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..6ca4633 --- /dev/null +++ b/README.md @@ -0,0 +1,133 @@ +# EnergypriceAPI + +energypriceapi is the official Node.js wrapper for EnergypriceAPI.com. This allows you to quickly integrate our foreign exchange rate API and currency conversion API into your application. Check https://energypriceapi.com documentation for more information. + + + +## Installation + +#### NPM + +``` +$ npm i energypriceapi +``` +--- +## Usage + +```js +const api = require('energypriceapi'); + +api.setAPIKey('SET_YOUR_API_KEY_HERE'); +await api.fetchLive('USD', ['BRENT','GASOLINE','NATURALGAS','WTI']); +``` +--- +## Documentation + +#### setAPIKey(apiKey) + +- `apiKey` <[string]> API Key + +In order to use this library, you must first call this function with an API key. + +```js +api.setAPIKey('SET_YOUR_API_KEY_HERE'); +``` +--- +#### fetchSymbols() +```js +await api.fetchSymbols(); +``` + +[Link](https://energypriceapi.com/documentation#api_symbol) + +--- +#### fetchLive(base, currencies) + +- `base` <[string]> Optional. Pass in a base currency, defaults to USD. +- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for. + +```js +await api.fetchLive('USD', ['BRENT','GASOLINE','NATURALGAS','WTI']); +``` + +[Link](https://energypriceapi.com/documentation#api_realtime) + +--- +#### fetchHistorical(date, base, currencies) + +- `date` <[string]> Required. Pass in a string with format `YYYY-MM-DD` +- `base` <[string]> Optional. Pass in a base currency, defaults to USD. +- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for. + +```js +await api.fetchHistorical('2021-04-05', 'USD', ['BRENT','GASOLINE','NATURALGAS','WTI']); +``` + +[Link](https://energypriceapi.com/documentation#api_historical) + +--- +#### convert(from, to, amount, date) + +- `from` <[string]> Optional. Pass in a base currency, defaults to USD. +- `to` <[string]> Required. Specify currency you would like to convert to. +- `amount` <[number]> Required. The amount to convert. +- `date` <[string]> Optional. Specify date to use historical midpoint value for conversion with format `YYYY-MM-DD`. Otherwise, it will use live exchange rate date if value not passed in. + +```js +await api.convert('USD', 'EUR', 100, '2021-04-05'); +``` + +[Link](https://energypriceapi.com/documentation#api_convert) + +--- +#### timeframe(start_date, end_date, base, currencies) + +- `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`. +- `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`. +- `base` <[string]> Optional. Pass in a base currency, defaults to USD. +- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for. + +```js +await api.timeframe('2021-04-05', '2021-04-06', 'USD', ['BRENT','GASOLINE','NATURALGAS','WTI']); +``` + +[Link](https://energypriceapi.com/documentation#api_timeframe) + +--- +#### change(start_date, end_date, base, currencies) + +- `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`. +- `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`. +- `base` <[string]> Optional. Pass in a base currency, defaults to USD. +- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for. + +```js +await api.change('2021-04-05', '2021-04-06', 'USD', ['BRENT','GASOLINE','NATURALGAS','WTI']); +``` + +[Link](https://energypriceapi.com/documentation#api_change) + +--- +**[Official documentation](https://energypriceapi.com/documentation)** + + +--- +## FAQ + +- How do I get an API Key? + + Free API Keys are available [here](https://energypriceapi.com). + +- I want more information + + Checkout our FAQs [here](https://energypriceapi.com/faq). + + +## Support + +For support, get in touch using [this form](https://energypriceapi.com/contact). + + +[array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 'Array' +[number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type 'Number' +[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type 'String' \ No newline at end of file diff --git a/example/index.js b/example/index.js new file mode 100644 index 0000000..022e0d5 --- /dev/null +++ b/example/index.js @@ -0,0 +1,27 @@ +const api = require('../index'); + +const apiKey = 'REPLACE_ME'; + +(async function() { + api.setAPIKey(apiKey); + + var result; + + result = await api.fetchSymbols(); + console.log(result.data); + + result = await api.fetchLive('USD', ['BRENT','GASOLINE','NATURALGAS','WTI']); + console.log(result.data); + + result = await api.fetchHistorical('2021-04-05', 'USD', ['BRENT','GASOLINE','NATURALGAS','WTI']); + console.log(result.data); + + result = await api.convert('USD', 'EUR', 100, '2021-04-05'); + console.log(result.data); + + result = await api.timeframe('2021-04-05', '2021-04-06', 'USD', ['BRENT','GASOLINE','NATURALGAS','WTI']); + console.log(result.data); + + result = await api.change('2021-04-05', '2021-04-06', 'USD', ['BRENT','GASOLINE','NATURALGAS','WTI']); + console.log(result.data); +})(); diff --git a/index.js b/index.js new file mode 100644 index 0000000..41a0583 --- /dev/null +++ b/index.js @@ -0,0 +1,88 @@ +(function() { + const axios = require('axios'); + + var apiKey; + + function removeEmpty(obj) { + for (var propName in obj) { + if (obj[propName] === null || obj[propName] === undefined || obj[propName] == '') { + delete obj[propName]; + } + } + return obj; + } + + exports.setAPIKey = function(apiKey) { + this.apiKey = apiKey; + }; + + exports.fetchSymbols = function() { + return axios({ + url: 'https://api.energypriceapi.com/v1/symbols', + params: { + api_key: this.apiKey, + }, + }); + }; + + exports.fetchLive = function(base, currencies) { + return axios({ + url: 'https://api.energypriceapi.com/v1/latest', + params: removeEmpty({ + api_key: this.apiKey, + base: base, + currencies: (currencies || []).join(','), + }), + }); + }; + + exports.fetchHistorical = function(date, base, currencies) { + return axios({ + url: `https://api.energypriceapi.com/v1/${date}`, + params: removeEmpty({ + api_key: this.apiKey, + base: base, + currencies: (currencies || []).join(','), + }), + }); + }; + + exports.convert = function(from, to, amount, date) { + return axios({ + url: 'https://api.energypriceapi.com/v1/convert', + params: removeEmpty({ + api_key: this.apiKey, + from: from, + to: to, + amount: amount, + date: date, + }), + }); + }; + + exports.timeframe = function(startDate, endDate, base, currencies) { + return axios({ + url: 'https://api.energypriceapi.com/v1/timeframe', + params: removeEmpty({ + api_key: this.apiKey, + start_date: startDate, + end_date: endDate, + base: base, + currencies: (currencies || []).join(','), + }), + }); + }; + + exports.change = function(startDate, endDate, base, currencies) { + return axios({ + url: 'https://api.energypriceapi.com/v1/change', + params: removeEmpty({ + api_key: this.apiKey, + start_date: startDate, + end_date: endDate, + base: base, + currencies: (currencies || []).join(','), + }), + }); + } +})(); diff --git a/package.json b/package.json new file mode 100644 index 0000000..ba57b25 --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "energypriceapi", + "version": "1.0.0", + "description": "Official Node.js Library for EnergypriceAPI", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "EnergypriceAPI", + "license": "GNU GPLv3", + "dependencies": { + "axios": "^0.24.0" + }, + "homepage": "https://energypriceapi.com", + "repository": { + "type": "git", + "url": "https://github.com/energypriceapi/energypriceapi-nodejs.git" + }, + "keywords": [ + "oil api", + "crude oil api", + "oilpriceapi", + "foreign exchange rates", + "forex", + "currency coversion", + "currency price", + "currency rates", + "api" + ] +}