Skip to content

Commit 46e73e6

Browse files
version 1.2.0
1 parent 3a20823 commit 46e73e6

File tree

4 files changed

+83
-31
lines changed

4 files changed

+83
-31
lines changed

README.md

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,51 @@ await api.fetchSymbols();
4141
[Link](https://metalpriceapi.com/documentation#api_symbol)
4242

4343
---
44-
#### fetchLive(base, currencies)
44+
#### fetchLive(base, currencies, unit, purity, math)
4545

4646
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
4747
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
48+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
49+
- `purity` <[string]> Optional. Pass in a purity level for metal prices.
50+
- `math` <[string]> Optional. Pass in a math expression to apply to the rates.
4851

4952
```js
50-
await api.fetchLive('USD', ['XAU', 'XAG', 'XPD', 'XPT']);
53+
await api.fetchLive('USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz', null, null);
5154
```
5255

5356
[Link](https://metalpriceapi.com/documentation#api_realtime)
5457

5558
---
56-
#### fetchHistorical(date, base, currencies)
59+
#### fetchHistorical(date, base, currencies, unit)
5760

5861
- `date` <[string]> Required. Pass in a string with format `YYYY-MM-DD`
5962
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
6063
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
64+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
6165

6266
```js
63-
await api.fetchHistorical('2024-02-05', 'USD', ['XAU', 'XAG', 'XPD', 'XPT']);
67+
await api.fetchHistorical('2024-02-05', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz');
6468
```
6569

6670
[Link](https://metalpriceapi.com/documentation#api_historical)
6771

72+
---
73+
#### hourly(base, currency, unit, startDate, endDate, math, dateType)
74+
75+
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
76+
- `currency` <[string]> Required. Specify currency you would like to get hourly rates for.
77+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
78+
- `startDate` <[string]> Required. Specify the start date using the format `YYYY-MM-DD`.
79+
- `endDate` <[string]> Required. Specify the end date using the format `YYYY-MM-DD`.
80+
- `math` <[string]> Optional. Pass in a math expression to apply to the rates.
81+
- `dateType` <[string]> Optional. Pass in a date type, overrides date parameters if passed in.
82+
83+
```js
84+
await api.hourly('USD', 'XAU', 'troy_oz', '2025-11-03', '2025-11-03', null, null);
85+
```
86+
87+
[Link](https://metalpriceapi.com/documentation#api_hourly)
88+
6889
---
6990
#### ohlc(base, currency, date, unit, dateType)
7091

@@ -81,55 +102,59 @@ await api.ohlc('USD', 'XAU', '2024-02-06', 'troy_oz', null);
81102
[Link](https://metalpriceapi.com/documentation#api_ohlc)
82103

83104
---
84-
#### convert(from, to, amount, date)
105+
#### convert(from, to, amount, date, unit)
85106

86107
- `from` <[string]> Optional. Pass in a base currency, defaults to USD.
87108
- `to` <[string]> Required. Specify currency you would like to convert to.
88109
- `amount` <[number]> Required. The amount to convert.
89110
- `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.
111+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
90112

91113
```js
92-
await api.convert('USD', 'EUR', 100, '2024-02-05');
114+
await api.convert('USD', 'EUR', 100, '2024-02-05', null);
93115
```
94116

95117
[Link](https://metalpriceapi.com/documentation#api_convert)
96118

97119
---
98-
#### timeframe(start_date, end_date, base, currencies)
120+
#### timeframe(startDate, endDate, base, currencies, unit)
99121

100-
- `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
101-
- `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
122+
- `startDate` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
123+
- `endDate` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
102124
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
103125
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
126+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
104127

105128
```js
106-
await api.timeframe('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT']);
129+
await api.timeframe('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz');
107130
```
108131

109132
[Link](https://metalpriceapi.com/documentation#api_timeframe)
110133

111134
---
112-
#### change(start_date, end_date, base, currencies)
135+
#### change(startDate, endDate, base, currencies, dateType)
113136

114-
- `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
115-
- `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
137+
- `startDate` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
138+
- `endDate` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
116139
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
117140
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
141+
- `dateType` <[string]> Optional. Pass in a date type, overrides date parameters if passed in.
118142

119143
```js
120-
await api.change('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT']);
144+
await api.change('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], null);
121145
```
122146

123147
[Link](https://metalpriceapi.com/documentation#api_change)
124148

125149
---
126-
#### carat(base, date)
150+
#### carat(base, currency, date)
127151

128152
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
153+
- `currency` <[string]> Optional. Pass in a metal code to get carat prices for (defaults to XAU).
129154
- `date` <[string]> Optional. Specify date to get Carat for specific date using format `YYYY-MM-DD`. If not specified, uses live rates.
130155

131156
```js
132-
await api.carat('USD', '2024-02-06');
157+
await api.carat('USD', 'XAU', '2024-02-06');
133158
```
134159

135160
[Link](https://metalpriceapi.com/documentation#api_carat)

example/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,28 @@ const apiKey = 'REPLACE_ME';
1111
result = await api.fetchSymbols();
1212
console.log(result.data);
1313

14-
result = await api.fetchLive('USD', ['XAU', 'XAG', 'XPD', 'XPT']);
14+
result = await api.fetchLive('USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz', null, null);
1515
console.log(result.data);
1616

17-
result = await api.fetchHistorical('2024-02-05', 'USD', ['XAU', 'XAG', 'XPD', 'XPT']);
17+
result = await api.fetchHistorical('2024-02-05', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz');
18+
console.log(result.data);
19+
20+
result = await api.hourly('USD', 'XAU', 'troy_oz', '2025-11-03', '2025-11-03', null, null);
1821
console.log(result.data);
1922

2023
result = await api.ohlc('USD', 'XAU', '2024-02-06', 'troy_oz', null);
2124
console.log(result.data);
2225

23-
result = await api.convert('USD', 'EUR', 100, '2024-02-05');
26+
result = await api.convert('USD', 'EUR', 100, '2024-02-05', null);
2427
console.log(result.data);
2528

26-
result = await api.timeframe('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT']);
29+
result = await api.timeframe('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz');
2730
console.log(result.data);
2831

29-
result = await api.change('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT']);
32+
result = await api.change('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], null);
3033
console.log(result.data);
3134

32-
result = await api.carat('USD', '2024-02-06');
35+
result = await api.carat('USD', 'XAU', '2024-02-06');
3336
console.log(result.data);
3437

3538
result = await api.usage();

index.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
function removeEmpty(obj) {
77
for (var propName in obj) {
8-
if (obj[propName] === null || obj[propName] === undefined || obj[propName] == '') {
8+
if (obj[propName] === null || obj[propName] === undefined || obj[propName] === '') {
99
delete obj[propName];
1010
}
1111
}
@@ -25,28 +25,48 @@
2525
});
2626
};
2727

28-
exports.fetchLive = function(base, currencies) {
28+
exports.fetchLive = function(base, currencies, unit, purity, math) {
2929
return axios({
3030
url: 'https://api.metalpriceapi.com/v1/latest',
3131
params: removeEmpty({
3232
api_key: this.apiKey,
3333
base: base,
3434
currencies: (currencies || []).join(','),
35+
unit: unit,
36+
purity: purity,
37+
math: math,
3538
}),
3639
});
3740
};
3841

39-
exports.fetchHistorical = function(date, base, currencies) {
42+
exports.fetchHistorical = function(date, base, currencies, unit) {
4043
return axios({
4144
url: `https://api.metalpriceapi.com/v1/${date}`,
4245
params: removeEmpty({
4346
api_key: this.apiKey,
4447
base: base,
4548
currencies: (currencies || []).join(','),
49+
unit: unit,
4650
}),
4751
});
4852
};
4953

54+
exports.hourly = function(base, currency, unit, startDate, endDate, math, dateType) {
55+
return axios({
56+
url: 'https://api.metalpriceapi.com/v1/hourly',
57+
params: removeEmpty({
58+
api_key: this.apiKey,
59+
base: base,
60+
currency: currency,
61+
unit: unit,
62+
start_date: startDate,
63+
end_date: endDate,
64+
math: math,
65+
date_type: dateType,
66+
}),
67+
});
68+
}
69+
5070
exports.ohlc = function(base, currency, date, unit, dateType) {
5171
return axios({
5272
url: 'https://api.metalpriceapi.com/v1/ohlc',
@@ -61,7 +81,7 @@
6181
});
6282
}
6383

64-
exports.convert = function(from, to, amount, date) {
84+
exports.convert = function(from, to, amount, date, unit) {
6585
return axios({
6686
url: 'https://api.metalpriceapi.com/v1/convert',
6787
params: removeEmpty({
@@ -70,11 +90,12 @@
7090
to: to,
7191
amount: amount,
7292
date: date,
93+
unit: unit,
7394
}),
7495
});
7596
};
7697

77-
exports.timeframe = function(startDate, endDate, base, currencies) {
98+
exports.timeframe = function(startDate, endDate, base, currencies, unit) {
7899
return axios({
79100
url: 'https://api.metalpriceapi.com/v1/timeframe',
80101
params: removeEmpty({
@@ -83,11 +104,12 @@
83104
end_date: endDate,
84105
base: base,
85106
currencies: (currencies || []).join(','),
107+
unit: unit,
86108
}),
87109
});
88110
};
89111

90-
exports.change = function(startDate, endDate, base, currencies) {
112+
exports.change = function(startDate, endDate, base, currencies, dateType) {
91113
return axios({
92114
url: 'https://api.metalpriceapi.com/v1/change',
93115
params: removeEmpty({
@@ -96,16 +118,18 @@
96118
end_date: endDate,
97119
base: base,
98120
currencies: (currencies || []).join(','),
121+
date_type: dateType,
99122
}),
100123
});
101124
}
102125

103-
exports.carat = function(base, date) {
126+
exports.carat = function(base, currency, date) {
104127
return axios({
105128
url: 'https://api.metalpriceapi.com/v1/carat',
106129
params: removeEmpty({
107130
api_key: this.apiKey,
108131
base: base,
132+
currency: currency,
109133
date: date,
110134
}),
111135
});

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "metalpriceapi",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "Official Node.js Library for MetalpriceAPI",
55
"main": "index.js",
66
"scripts": {
@@ -9,7 +9,7 @@
99
"author": "MetalpriceAPI",
1010
"license": "MIT",
1111
"dependencies": {
12-
"axios": "^1.6.7"
12+
"axios": "^1.13.5"
1313
},
1414
"homepage": "https://metalpriceapi.com",
1515
"repository": {

0 commit comments

Comments
 (0)