Skip to content

Commit cdb9319

Browse files
authored
Merge pull request #93 from alpacahq/feature/polygon-key-config
Add ability to override key used to authenticate with Polygon
2 parents fec9392 + b254c85 commit cdb9319

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ The Alpaca SDK will check the environment for a number of variables which can be
6363
| APCA_RETRY_WAIT=3 | 3 | seconds to wait between each retry attempt |
6464
| APCA_RETRY_CODES=429,504 | 429,504 | comma-separated HTTP status code for which retry is attempted |
6565
| POLYGON_WS_URL | wss://alpaca.socket.polygon.io/stocks | Endpoint for streaming polygon data. You likely don't need to change this unless you want to proxy it for example |
66+
| POLYGON_KEY_ID | | Your Polygon key, if it's not the same as your Alpaca API key. Most users will not need to set this to access Polygon.|
6667

6768

6869
## REST
@@ -238,9 +239,10 @@ Deregisters the event handler function that was previously registered via `on` o
238239
---
239240
# Polygon API Service
240241

241-
Alpaca's API key ID can be used to access Polygon API whose document is found [here](https://polygon.io/docs/).
242-
This python SDK wraps their API service and seamlessly integrates with Alpaca API.
243-
`alpaca_trade_api.REST.polygon` will be the `REST` object for Polygon.
242+
Alpaca's API key ID can be used to access Polygon API, the documentation for
243+
which is found [here](https://polygon.io/docs/).
244+
This python SDK wraps their API service and seamlessly integrates it with the Alpaca
245+
API. `alpaca_trade_api.REST.polygon` will be the `REST` object for Polygon.
244246

245247
The example below gives AAPL daily OHLCV data in a DataFrame format.
246248

alpaca_trade_api/common.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ def get_credentials(key_id=None, secret_key=None):
2323
return key_id, secret_key
2424

2525

26+
def get_polygon_credentials(alpaca_key=None):
27+
try:
28+
alpaca_key, _ = get_credentials(alpaca_key)
29+
except ValueError:
30+
pass
31+
key_id = os.environ.get('POLYGON_KEY_ID') or alpaca_key
32+
if key_id is None:
33+
raise ValueError('Key ID must be given to access Polygon API')
34+
return key_id
35+
36+
2637
def get_api_version(api_version):
2738
api_version = api_version or os.environ.get('APCA_API_VERSION')
2839
if api_version is None:

alpaca_trade_api/polygon/rest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Exchange, SymbolTypeMap, ConditionMap,
77
Company, Dividends, Splits, Earnings, Financials, NewsList, Ticker
88
)
9+
from alpaca_trade_api.common import get_polygon_credentials
910

1011

1112
def _is_list_like(o):
@@ -15,6 +16,7 @@ def _is_list_like(o):
1516
class REST(object):
1617

1718
def __init__(self, api_key, staging=False):
19+
self._api_key = get_polygon_credentials(api_key)
1820
self._api_key = api_key
1921
self._staging = staging
2022
self._session = requests.Session()

alpaca_trade_api/polygon/stream2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
from .entity import (
88
Quote, Trade, Agg, Entity,
99
)
10+
from alpaca_trade_api.common import get_polygon_credentials
1011
import logging
1112

1213

1314
class StreamConn(object):
1415
def __init__(self, key_id=None):
15-
self._key_id = key_id or os.environ.get('APCA_API_KEY_ID')
16+
self._key_id = get_polygon_credentials(key_id)
1617
self._endpoint = os.environ.get(
1718
'POLYGON_WS_URL',
1819
'wss://alpaca.socket.polygon.io/stocks'

0 commit comments

Comments
 (0)