Skip to content

Commit ac09d4b

Browse files
v1.3.0
1 parent 4d39fca commit ac09d4b

4 files changed

Lines changed: 129 additions & 38 deletions

File tree

README.md

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,30 @@ from metalpriceapi.client import Client
1616

1717
api_key = 'SET_YOUR_API_KEY_HERE'
1818
client = Client(api_key)
19+
20+
# Or use EU server:
21+
# client = Client(api_key, server='eu')
1922
```
23+
---
24+
## Server Regions
25+
26+
MetalpriceAPI 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.metalpriceapi.com/v1` |
31+
| Europe | `https://api-eu.metalpriceapi.com/v1` |
32+
33+
```python
34+
from metalpriceapi.client import Client
35+
36+
# Default (US)
37+
client = Client('SET_YOUR_API_KEY_HERE')
38+
39+
# Europe
40+
client = Client('SET_YOUR_API_KEY_HERE', server='eu')
41+
```
42+
2043
---
2144
## Documentation
2245

@@ -28,32 +51,62 @@ client.fetchSymbols()
2851
[Link](https://metalpriceapi.com/documentation#api_symbol)
2952

3053
---
31-
#### fetchLive(base, currencies)
54+
#### setServer(server)
55+
56+
- `server` <[string]> Pass `'eu'` to use the EU server (`api-eu.metalpriceapi.com`), or `'us'` for the US server. Defaults to US if not specified.
57+
58+
```python
59+
client.setServer('eu')
60+
```
61+
62+
---
63+
#### fetchLive(base, currencies, unit, purity, math)
3264

3365
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
34-
- `currencies` <[List]<[string]>> Optional. Pass in an list of currencies to return values for.
66+
- `currencies` <[List]<[string]>> Optional. Pass in a list of currencies to return values for.
67+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
68+
- `purity` <[string]> Optional. Pass in a purity level for metal prices.
69+
- `math` <[string]> Optional. Pass in a math expression to apply to the rates.
3570

3671
```python
37-
client.fetchLive(base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'])
72+
client.fetchLive(base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'], unit='troy_oz')
3873
```
3974

4075
[Link](https://metalpriceapi.com/documentation#api_realtime)
4176

4277
---
43-
#### fetchHistorical(date, base, currencies)
78+
#### fetchHistorical(date, base, currencies, unit)
4479

4580
- `date` <[string]> Required. Pass in a string with format `YYYY-MM-DD`
4681
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
47-
- `currencies` <[List]<[string]>> Optional. Pass in an list of currencies to return values for.
82+
- `currencies` <[List]<[string]>> Optional. Pass in a list of currencies to return values for.
83+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
4884

4985
```python
50-
client.fetchHistorical(date='2024-02-05', base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'])
86+
client.fetchHistorical(date='2024-02-05', base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'], unit='troy_oz')
5187
```
5288

5389
[Link](https://metalpriceapi.com/documentation#api_historical)
5490

5591
---
56-
#### fetchOHLC(base, currency, date, unit, dateType)
92+
#### hourly(base, currency, unit, start_date, end_date, math, date_type)
93+
94+
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
95+
- `currency` <[string]> Required. Specify currency you would like to get hourly rates for.
96+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
97+
- `start_date` <[string]> Required. Specify the start date using the format `YYYY-MM-DD`.
98+
- `end_date` <[string]> Required. Specify the end date using the format `YYYY-MM-DD`.
99+
- `math` <[string]> Optional. Pass in a math expression to apply to the rates.
100+
- `date_type` <[string]> Optional. Pass in a date type, overrides date parameters if passed in.
101+
102+
```python
103+
client.hourly(base='USD', currency='XAU', unit='troy_oz', start_date='2025-11-03', end_date='2025-11-03')
104+
```
105+
106+
[Link](https://metalpriceapi.com/documentation#api_hourly)
107+
108+
---
109+
#### fetchOHLC(base, currency, date, unit, date_type)
57110

58111
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
59112
- `currency` <[string]> Required. Specify currency you would like to get OHLC for.
@@ -62,18 +115,19 @@ client.fetchHistorical(date='2024-02-05', base='USD', currencies=['XAU', 'XAG',
62115
- `date_type` <[string]> Optional. Pass in a date type, overrides date parameter if passed in.
63116

64117
```python
65-
client.fetchOHLC(base='USD', currency='XAU', date='2024-02-05', unit='troy_oz', date_type=None)
118+
client.fetchOHLC(base='USD', currency='XAU', date='2024-02-05', unit='troy_oz')
66119
```
67120

68121
[Link](https://metalpriceapi.com/documentation#api_ohlc)
69122

70123
---
71-
#### convert(from_currency, to_currency, amount, date)
124+
#### convert(from_currency, to_currency, amount, date, unit)
72125

73126
- `from_currency` <[string]> Optional. Pass in a base currency, defaults to USD.
74127
- `to_currency` <[string]> Required. Specify currency you would like to convert to.
75128
- `amount` <[number]> Required. The amount to convert.
76129
- `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.
130+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
77131

78132
```python
79133
client.convert(from_currency='USD', to_currency='EUR', amount=100, date='2024-02-05')
@@ -82,26 +136,28 @@ client.convert(from_currency='USD', to_currency='EUR', amount=100, date='2024-02
82136
[Link](https://metalpriceapi.com/documentation#api_convert)
83137

84138
---
85-
#### timeframe(start_date, end_date, base, currencies)
139+
#### timeframe(start_date, end_date, base, currencies, unit)
86140

87141
- `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
88142
- `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
89143
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
90-
- `currencies` <[List]<[string]>> Optional. Pass in an list of currencies to return values for.
144+
- `currencies` <[List]<[string]>> Optional. Pass in a list of currencies to return values for.
145+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
91146

92147
```python
93-
client.timeframe(start_date='2024-02-05', end_date='2024-02-06', base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'])
148+
client.timeframe(start_date='2024-02-05', end_date='2024-02-06', base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'], unit='troy_oz')
94149
```
95150

96151
[Link](https://metalpriceapi.com/documentation#api_timeframe)
97152

98153
---
99-
#### change(start_date, end_date, base, currencies)
154+
#### change(start_date, end_date, base, currencies, date_type)
100155

101156
- `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
102157
- `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
103158
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
104-
- `currencies` <[List]<[string]>> Optional. Pass in an list of currencies to return values for.
159+
- `currencies` <[List]<[string]>> Optional. Pass in a list of currencies to return values for.
160+
- `date_type` <[string]> Optional. Pass in a date type, overrides date parameters if passed in.
105161

106162
```python
107163
client.change(start_date='2024-02-05', end_date='2024-02-06', base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'])
@@ -110,13 +166,14 @@ client.change(start_date='2024-02-05', end_date='2024-02-06', base='USD', curren
110166
[Link](https://metalpriceapi.com/documentation#api_change)
111167

112168
---
113-
#### carat(base, date)
169+
#### carat(base, currency, date)
114170

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

118175
```python
119-
client.carat(base='USD', date='2024-02-05')
176+
client.carat(base='USD', currency='XAU', date='2024-02-05')
120177
```
121178

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

example.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,34 @@
33
api_key = 'REPLACE_ME'
44
client = Client(api_key)
55

6+
# Or use EU server:
7+
# client = Client(api_key, server='eu')
8+
69
result = client.fetchSymbols()
710
print(result)
811

9-
result = client.fetchLive(base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'])
12+
result = client.fetchLive(base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'], unit='troy_oz')
13+
print(result)
14+
15+
result = client.fetchHistorical(date='2024-02-05', base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'], unit='troy_oz')
1016
print(result)
1117

12-
result = client.fetchHistorical(date='2024-02-05', base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'])
18+
result = client.hourly(base='USD', currency='XAU', unit='troy_oz', start_date='2025-11-03', end_date='2025-11-03')
1319
print(result)
1420

15-
result = client.fetchOHLC(base='USD', currency='XAU', date='2024-02-06', unit='troy_oz', date_type=None)
21+
result = client.fetchOHLC(base='USD', currency='XAU', date='2024-02-06', unit='troy_oz')
1622
print(result)
1723

1824
result = client.convert(from_currency='USD', to_currency='EUR', amount=100, date='2024-02-05')
1925
print(result)
2026

21-
result = client.timeframe(start_date='2024-02-05', end_date='2024-02-06', base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'])
27+
result = client.timeframe(start_date='2024-02-05', end_date='2024-02-06', base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'], unit='troy_oz')
2228
print(result)
2329

2430
result = client.change(start_date='2024-02-05', end_date='2024-02-06', base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'])
2531
print(result)
2632

27-
result = client.carat(base='USD', date='2024-02-06')
33+
result = client.carat(base='USD', currency='XAU', date='2024-02-06')
2834
print(result)
2935

3036
result = client.usage()

metalpriceapi/client.py

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,107 @@
11
import requests
22

33
class Client(object):
4-
base_url = 'https://api.metalpriceapi.com/v1'
4+
SERVERS = {
5+
'us': 'https://api.metalpriceapi.com/v1',
6+
'eu': 'https://api-eu.metalpriceapi.com/v1',
7+
}
58

69
def _removeEmpty(self, params):
7-
return {key:value for key,value in params.items() if value != ''}
10+
return {key: value for key, value in params.items() if value is not None and value != ''}
811

9-
def __init__(self, api_key):
12+
def __init__(self, api_key, server='us'):
13+
self.base_url = self.SERVERS.get(server, self.SERVERS['us'])
1014
self.client = requests.Session()
1115
self.client.params.update({'api_key': api_key})
1216

17+
def setServer(self, server):
18+
self.base_url = self.SERVERS.get(server, self.SERVERS['us'])
19+
1320
def fetchSymbols(self):
1421
response = self.client.get(f'{self.base_url}/symbols')
1522
return response.json()
1623

17-
def fetchLive(self, base='', currencies=[]):
24+
def fetchLive(self, base='', currencies=None, unit='', purity='', math=''):
1825
params = self._removeEmpty({
1926
'base': base,
20-
'currencies': ','.join(currencies)
27+
'currencies': ','.join(currencies or []),
28+
'unit': unit,
29+
'purity': purity,
30+
'math': math,
2131
})
2232
response = self.client.get(f'{self.base_url}/latest', params=params)
2333
return response.json()
2434

25-
def fetchHistorical(self, date, base='', currencies=[]):
35+
def fetchHistorical(self, date, base='', currencies=None, unit=''):
2636
params = self._removeEmpty({
2737
'base': base,
28-
'currencies': ','.join(currencies)
38+
'currencies': ','.join(currencies or []),
39+
'unit': unit,
2940
})
3041
response = self.client.get(f'{self.base_url}/{date}', params=params)
3142
return response.json()
3243

44+
def hourly(self, base='', currency='', unit='', start_date='', end_date='', math='', date_type=''):
45+
params = self._removeEmpty({
46+
'base': base,
47+
'currency': currency,
48+
'unit': unit,
49+
'start_date': start_date,
50+
'end_date': end_date,
51+
'math': math,
52+
'date_type': date_type,
53+
})
54+
response = self.client.get(f'{self.base_url}/hourly', params=params)
55+
return response.json()
56+
3357
def fetchOHLC(self, base='', currency='', date='', unit='', date_type=''):
3458
params = self._removeEmpty({
3559
'base': base,
3660
'currency': currency,
3761
'date': date,
3862
'unit': unit,
39-
'date_type': date_type
63+
'date_type': date_type,
4064
})
4165
response = self.client.get(f'{self.base_url}/ohlc', params=params)
4266
return response.json()
4367

44-
def convert(self, to_currency, amount, from_currency='', date=''):
68+
def convert(self, from_currency='', to_currency='', amount='', date='', unit=''):
4569
params = self._removeEmpty({
4670
'from': from_currency,
4771
'to': to_currency,
4872
'amount': amount,
49-
'date': date
73+
'date': date,
74+
'unit': unit,
5075
})
5176
response = self.client.get(f'{self.base_url}/convert', params=params)
5277
return response.json()
5378

54-
def timeframe(self, start_date, end_date, base='', currencies=[]):
79+
def timeframe(self, start_date, end_date, base='', currencies=None, unit=''):
5580
params = self._removeEmpty({
5681
'start_date': start_date,
5782
'end_date': end_date,
5883
'base': base,
59-
'currencies': ','.join(currencies)
84+
'currencies': ','.join(currencies or []),
85+
'unit': unit,
6086
})
6187
response = self.client.get(f'{self.base_url}/timeframe', params=params)
6288
return response.json()
6389

64-
def change(self, start_date, end_date, base='', currencies=[]):
90+
def change(self, start_date, end_date, base='', currencies=None, date_type=''):
6591
params = self._removeEmpty({
6692
'start_date': start_date,
6793
'end_date': end_date,
6894
'base': base,
69-
'currencies': ','.join(currencies)
95+
'currencies': ','.join(currencies or []),
96+
'date_type': date_type,
7097
})
7198
response = self.client.get(f'{self.base_url}/change', params=params)
7299
return response.json()
73100

74-
def carat(self, base='', date = ''):
101+
def carat(self, base='', currency='', date=''):
75102
params = self._removeEmpty({
76103
'base': base,
104+
'currency': currency,
77105
'date': date,
78106
})
79107
response = self.client.get(f'{self.base_url}/carat', params=params)

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
setuptools.setup(
44
name="metalpriceapi",
5-
version="1.1.2",
5+
version="1.3.0",
66
url="https://github.com/metalpriceapi/metalpriceapi-python",
77

8-
license_files=('LICENSE'),
8+
license_files=('LICENSE',),
99

1010
author="MetalpriceAPI",
1111
author_email="contact@metalpriceapi.com",

0 commit comments

Comments
 (0)