Cannot fetch bars


This is my code:


class AlpacaClient:
def init(self):
self.stockHistoricalDataClient = StockHistoricalDataClient(api_key=connectionsettings.ALPACA_API_KEY,

def get_stock_bars(self, request_params):
    bars = self.stockHistoricalDataClient.get_stock_bars(request_params)

    return HttpResponse(bars, content_type="application/json")

symbol = “AAPL” # Example stock symbol

timeframee = TimeFrame(5, TimeFrameUnit.Minute)
start_date = datetime(2024, 5, 26)
end_date = datetime(2024, 5, 26)

alpacaClient = AlpacaClient()
request_params = StockBarsRequest(
    bars = alpacaClient.get_stock_bars(request_params)
except Exception as e:
    print(f"An error occurred: {e}")

I get : An error occurred: Not Found
I have tried different variations such as:

  • timeframee = TimeFrame(1, TimeFrameUnit.Minute)

    start_date = datetime(2024, 5, 26, 18, 15, 0)
    end_date = datetime(2024, 5, 26, 18, 10, 0) (since basic subscription plan allows last 5 minutes) → I get : An error occurred: Not Found

  • If i use ALPACA_MARKET_DATA_URL instead then i get :
    An error occurred:

403 Forbidden

403 Forbidden


Any idea?

@jimcomccabe3 Basically, do not provide a url_override parameter when instantiating StockHistoricalDataClient. Let the defaults do their thing. It will default to which is what you want.

Specifically, in the first instance, you overrode the default so the url was
which is not found because the bars endpoint doesn’t exist under that base url.

In the second instance, you overrode the default so the url was
which was found (it’s the market data for the broker API sandbox) but you received forbidden because the API key and secret key you provided are for a trading account and not a broker account.

So, just remove url_override=connectionsettings.ALPACA_BASE_URL and it should work.

Thank you but that default url is production . I want to run in test .

@jimcomccabe3 Not exactly sure what you mean when you say “I want to run in test”. There really is exactly one market data environment. There isn’t a ‘test’ market data and ‘production’ market data. Historical data endpoints all return the same values. The two different base URLs (ie and are simply provided so one can authenticate with either trading API keys or broker API keys. If one is an individual trader with a paper and/or live account, then you will be using your trading API keys to authenticate calls to endpoints with the base URL

Does that make sense? What is it you are trying to do which made you feel you want to “run in test”?

I am saying this because it says (production and sandbox) when i select base url in Historical bars. . so this makes me feel like sandbox is the opposite of production , ie test.

when i run below i get empty


@jimcomccabe3 It doesn’t look like you included the start and end dates (notice how those fields are gray and not black text). Enter the default dates and you should get this and then the associated response.

import requests

url = ""

headers = {
    "accept": "application/json",
    "APCA-API-KEY-ID": "xxxxxx",
    "APCA-API-SECRET-KEY": "xxxxxxx"

response = requests.get(url, headers=headers)


thank you !it worked!

1 Like