I just started using Alpaca API. I had no problem using the old, soon-deprecated alpaca_trade_api, but when trying the new alpaca-py package, I got the error {“message”: “forbidden”}
The code looks like:
client = StockHistoricalDataClient(‘ALPACA_API_KEY’, ‘ALPACA_SECRET_KEY’)
request_params = StockBarsRequest(
symbol_or_symbols=[“AAPL”],
timeframe=TimeFrame.Day,
start=datetime.datetime.strptime(“2022-07-01”, ‘%Y-%m-%d’),
end=datetime.datetime.strptime(“2022-07-07”, ‘%Y-%m-%d’)
)
D = client.get_stock_bars(request_params)
The explicit error message is:
HTTPError: 403 Client Error: Forbidden for url: https://data.alpaca.markets/v2/stocks/bars?start=2022-07-01T00%3A00%3A00Z&end=2022-07-07T00%3A00%3A00Z&timeframe=1Day&symbols=AAPL
Curiously, I got no error with crypto data client if I don’t use keys and secrets:
client = StockHistoricalDataClient()
request_params = StockBarsRequest(
symbol_or_symbols=[“AAPL”],
timeframe=TimeFrame.Day,
start=datetime.datetime.strptime(“2022-07-01”, ‘%Y-%m-%d’),
end=datetime.datetime.strptime(“2022-07-07”, ‘%Y-%m-%d’)
)
D = client.get_stock_bars(request_params)
But, if I do use my key and secret, I got a similar forbidden error message. The only difference seems to be that the crypto function calls a v1beta2 URL, not the V2 url:
HTTPError: 403 Client Error: Forbidden for url: https://data.alpaca.markets/v1beta2/crypto/bars?start=2022-09-01T00%3A00%3A00Z&end=2022-09-07T00%3A00%3A00Z&timeframe=1Day&symbols=BTC%2FUSD
Can Alpaca please explain what is going on? Thanks.