Skip to content

Commit fa6c1d3

Browse files
committed
v1.3.0 — add hourly endpoint, EU server support, math/date_type params
1 parent 8e89f3b commit fa6c1d3

4 files changed

Lines changed: 101 additions & 23 deletions

File tree

README.md

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ const api = require('forexrateapi');
2020
api.setAPIKey('SET_YOUR_API_KEY_HERE');
2121
await api.fetchLive('USD', ['AUD', 'CAD', 'GBP', 'JPY']);
2222
```
23+
---
24+
## Server Regions
25+
26+
ForexRateAPI provides two regional endpoints. Choose the one closest to your servers for optimal performance.
27+
28+
| Region | Base URL |
29+
|--------|----------|
30+
| United States (default) | `https://api.forexrateapi.com/v1` |
31+
| Europe | `https://api-eu.forexrateapi.com/v1` |
32+
33+
```js
34+
api.setAPIKey('SET_YOUR_API_KEY_HERE');
35+
36+
// Default is US server
37+
// Switch to EU server:
38+
api.setServer('eu');
39+
```
40+
2341
---
2442
## Documentation
2543

@@ -32,6 +50,15 @@ In order to use this library, you must first call this function with an API key.
3250
```js
3351
api.setAPIKey('SET_YOUR_API_KEY_HERE');
3452
```
53+
---
54+
#### setServer(server)
55+
56+
- `server` <[string]> Pass `'eu'` to use the EU server (`api-eu.forexrateapi.com`), or `'us'` for the US server. Defaults to US if not specified.
57+
58+
```js
59+
api.setServer('eu');
60+
```
61+
3562
---
3663
#### fetchSymbols()
3764
```js
@@ -41,10 +68,11 @@ await api.fetchSymbols();
4168
[Link](https://forexrateapi.com/documentation#api_symbol)
4269

4370
---
44-
#### fetchLive(base, currencies)
71+
#### fetchLive(base, currencies, math)
4572

4673
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
4774
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
75+
- `math` <[string]> Optional. Pass in a math expression to apply to the rates.
4876

4977
```js
5078
await api.fetchLive('USD', ['AUD', 'CAD', 'GBP', 'JPY']);
@@ -65,6 +93,22 @@ await api.fetchHistorical('2024-02-05', 'USD', ['AUD', 'CAD', 'GBP', 'JPY']);
6593

6694
[Link](https://forexrateapi.com/documentation#api_historical)
6795

96+
---
97+
#### hourly(base, currency, startDate, endDate, math, dateType)
98+
99+
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
100+
- `currency` <[string]> Required. Specify currency you would like to get hourly rates for.
101+
- `startDate` <[string]> Required. Specify the start date using the format `YYYY-MM-DD`.
102+
- `endDate` <[string]> Required. Specify the end date using the format `YYYY-MM-DD`.
103+
- `math` <[string]> Optional. Pass in a math expression to apply to the rates.
104+
- `dateType` <[string]> Optional. Pass in a date type, overrides date parameters if passed in.
105+
106+
```js
107+
await api.hourly('USD', 'EUR', '2024-02-05', '2024-02-05');
108+
```
109+
110+
[Link](https://forexrateapi.com/documentation#api_hourly)
111+
68112
---
69113
#### ohlc(base, currency, date, dateType)
70114

@@ -94,10 +138,10 @@ await api.convert('USD', 'EUR', 100, '2024-02-05');
94138
[Link](https://forexrateapi.com/documentation#api_convert)
95139

96140
---
97-
#### timeframe(start_date, end_date, base, currencies)
141+
#### timeframe(startDate, endDate, base, currencies)
98142

99-
- `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
100-
- `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
143+
- `startDate` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
144+
- `endDate` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
101145
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
102146
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
103147

@@ -108,12 +152,13 @@ await api.timeframe('2024-02-05', '2024-02-06', 'USD', ['AUD', 'CAD', 'GBP', 'JP
108152
[Link](https://forexrateapi.com/documentation#api_timeframe)
109153

110154
---
111-
#### change(start_date, end_date, base, currencies)
155+
#### change(startDate, endDate, base, currencies, dateType)
112156

113-
- `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
114-
- `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
157+
- `startDate` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
158+
- `endDate` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
115159
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
116160
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
161+
- `dateType` <[string]> Optional. Pass in a date type, overrides date parameters if passed in.
117162

118163
```js
119164
await api.change('2024-02-05', '2024-02-06', 'USD', ['AUD', 'CAD', 'GBP', 'JPY']);
@@ -153,4 +198,4 @@ For support, get in touch using [this form](https://forexrateapi.com/contact).
153198

154199
[array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 'Array'
155200
[number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type 'Number'
156-
[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type 'String'
201+
[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type 'String'

example/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const apiKey = 'REPLACE_ME';
55
(async function() {
66
api.setAPIKey(apiKey);
77

8+
// Or use EU server:
9+
// api.setServer('eu');
10+
811
var result;
912

1013
result = await api.fetchSymbols();
@@ -16,6 +19,9 @@ const apiKey = 'REPLACE_ME';
1619
result = await api.fetchHistorical('2024-02-05', 'USD', ['AUD', 'CAD', 'GBP', 'JPY']);
1720
console.log(result.data);
1821

22+
result = await api.hourly('USD', 'EUR', '2024-02-05', '2024-02-05');
23+
console.log(result.data);
24+
1925
result = await api.ohlc('USD', 'EUR', '2024-02-05', null);
2026
console.log(result.data);
2127

index.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
(function() {
22
const axios = require('axios');
33

4+
const SERVERS = {
5+
us: 'https://api.forexrateapi.com/v1',
6+
eu: 'https://api-eu.forexrateapi.com/v1'
7+
};
8+
49
var apiKey;
10+
var baseUrl = SERVERS.us;
511

612
function removeEmpty(obj) {
713
for (var propName in obj) {
8-
if (obj[propName] === null || obj[propName] === undefined || obj[propName] == '') {
14+
if (obj[propName] === null || obj[propName] === undefined || obj[propName] === '') {
915
delete obj[propName];
1016
}
1117
}
@@ -16,29 +22,34 @@
1622
this.apiKey = apiKey;
1723
};
1824

25+
exports.setServer = function(server) {
26+
baseUrl = SERVERS[server] || SERVERS.us;
27+
};
28+
1929
exports.fetchSymbols = function() {
2030
return axios({
21-
url: 'https://api.forexrateapi.com/v1/symbols',
31+
url: `${baseUrl}/symbols`,
2232
params: {
2333
api_key: this.apiKey,
2434
},
2535
});
2636
};
2737

28-
exports.fetchLive = function(base, currencies) {
38+
exports.fetchLive = function(base, currencies, math) {
2939
return axios({
30-
url: 'https://api.forexrateapi.com/v1/latest',
40+
url: `${baseUrl}/latest`,
3141
params: removeEmpty({
3242
api_key: this.apiKey,
3343
base: base,
3444
currencies: (currencies || []).join(','),
45+
math: math,
3546
}),
3647
});
3748
};
3849

3950
exports.fetchHistorical = function(date, base, currencies) {
4051
return axios({
41-
url: `https://api.forexrateapi.com/v1/${date}`,
52+
url: `${baseUrl}/${date}`,
4253
params: removeEmpty({
4354
api_key: this.apiKey,
4455
base: base,
@@ -47,9 +58,24 @@
4758
});
4859
};
4960

61+
exports.hourly = function(base, currency, startDate, endDate, math, dateType) {
62+
return axios({
63+
url: `${baseUrl}/hourly`,
64+
params: removeEmpty({
65+
api_key: this.apiKey,
66+
base: base,
67+
currency: currency,
68+
start_date: startDate,
69+
end_date: endDate,
70+
math: math,
71+
date_type: dateType,
72+
}),
73+
});
74+
};
75+
5076
exports.ohlc = function(base, currency, date, dateType) {
5177
return axios({
52-
url: 'https://api.forexrateapi.com/v1/ohlc',
78+
url: `${baseUrl}/ohlc`,
5379
params: removeEmpty({
5480
api_key: this.apiKey,
5581
base: base,
@@ -62,7 +88,7 @@
6288

6389
exports.convert = function(from, to, amount, date) {
6490
return axios({
65-
url: 'https://api.forexrateapi.com/v1/convert',
91+
url: `${baseUrl}/convert`,
6692
params: removeEmpty({
6793
api_key: this.apiKey,
6894
from: from,
@@ -75,7 +101,7 @@
75101

76102
exports.timeframe = function(startDate, endDate, base, currencies) {
77103
return axios({
78-
url: 'https://api.forexrateapi.com/v1/timeframe',
104+
url: `${baseUrl}/timeframe`,
79105
params: removeEmpty({
80106
api_key: this.apiKey,
81107
start_date: startDate,
@@ -86,22 +112,23 @@
86112
});
87113
};
88114

89-
exports.change = function(startDate, endDate, base, currencies) {
115+
exports.change = function(startDate, endDate, base, currencies, dateType) {
90116
return axios({
91-
url: 'https://api.forexrateapi.com/v1/change',
117+
url: `${baseUrl}/change`,
92118
params: removeEmpty({
93119
api_key: this.apiKey,
94120
start_date: startDate,
95121
end_date: endDate,
96122
base: base,
97123
currencies: (currencies || []).join(','),
124+
date_type: dateType,
98125
}),
99126
});
100127
}
101128

102-
exports.usage = function(base, currency) {
129+
exports.usage = function() {
103130
return axios({
104-
url: 'https://api.forexrateapi.com/v1/usage',
131+
url: `${baseUrl}/usage`,
105132
params: removeEmpty({
106133
api_key: this.apiKey,
107134
}),

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "forexrateapi",
3-
"version": "1.1.2",
3+
"version": "1.3.0",
44
"description": "Official Node.js Library for ForexRateAPI",
55
"main": "index.js",
66
"scripts": {
@@ -9,7 +9,7 @@
99
"author": "ForexRateAPI",
1010
"license": "MIT",
1111
"dependencies": {
12-
"axios": "^1.6.7"
12+
"axios": "^1.13.5"
1313
},
1414
"homepage": "https://forexrateapi.com",
1515
"repository": {
@@ -19,7 +19,7 @@
1919
"keywords": [
2020
"foreign exchange rates",
2121
"forex",
22-
"currency coversion",
22+
"currency conversion",
2323
"currency price",
2424
"currency rates",
2525
"api"

0 commit comments

Comments
 (0)