alpacahq / alpaca-py

The Official Python SDK for Alpaca API
https://alpaca.markets/sdks/python/getting_started.html
Apache License 2.0
578 stars 146 forks source link

[Question]: Can broker keys be used with market data clients? #331

Closed builder173 closed 1 year ago

builder173 commented 1 year ago

Question form pre-submit checklist.

Question

The API reference here indicates that the Broker API key can be used to access IEX market data. Does that hold for the python library as well?

I attempted to follow the historical stock price example here using my broker api key/secret.

alp_stock_client = StockHistoricalDataClient( 
api_key=os.getenv('ALPACA_API_KEY'), 
secret_key=os.getenv('ALPACA_API_SECRET'))
multisymbol_request_params = StockLatestQuoteRequest(symbol_or_symbols=["SPY", "GLD", "TLT"])
latest_multisymbol_quotes = alp_stock_client.get_stock_latest_quote(multisymbol_request_params)
gld_latest_ask_price = latest_multisymbol_quotes["GLD"].ask_price

However I'm getting this error: HTTPError: 403 Client Error: Forbidden for url: https://data.alpaca.markets/v2/stocks/quotes/latest?symbols=SPY%2CGLD%2CTLT

If I add the url_override parameter with value 'https://data.sandbox.alpaca.markets' I get the same error.

I can setup a trading account too if necessary, just don't want to acquire another set of keys if the broker api keys are supposed to work here.

Thanks

haxdds commented 1 year ago

Yes it is possible to0 use your Broker API keys for Market Data. You need to make sure to set the use_basic_auth parameter to False within StockHistoricalData (Relevant Line). This is unlike the Trading/Data API, Broker API uses basic auth.

builder173 commented 1 year ago

@haxdds Thanks, I assume you meant I should set use_basic_auth to True since False is the default?

Although use_basic_auth=True didn't work on its own, it did work in combination with: url_override='https://data.sandbox.alpaca.markets'