Symbol_bars = api.get_bars() returns Forbidden for url

api.get_bars()

import config

base_url = "https://paper-api.alpaca.markets"

import alpaca_trade_api as tradeapi
api = tradeapi.REST(config.API_KEY, config.SECRET_KEY, base_url=base_url, api_version='v2')
# api = tradeapi.REST()

symbol = 'AAPL'
symbol_bars = api.get_bars(symbol, 'minute', 1).df.iloc[0]

The last line returns.

Traceback (most recent call last):
  File "/home/bwilson/DL/github.com/BruceRayWilson/MarketPrediction/src/bad_001.py", line 10, in <module>
    symbol_bars = api.get_bars(symbol, 'minute', 1).df.iloc[0]
  File "/home/bwilson/dlvenv2/lib/python3.10/site-packages/alpaca_trade_api/rest.py", line 714, in get_bars
    bars = list(self.get_bars_iter(symbol,
  File "/home/bwilson/dlvenv2/lib/python3.10/site-packages/alpaca_trade_api/rest.py", line 698, in get_bars_iter
    for bar in bars:
  File "/home/bwilson/dlvenv2/lib/python3.10/site-packages/alpaca_trade_api/rest.py", line 585, in _data_get
    resp = self.data_get(path, data=data, feed=feed,
  File "/home/bwilson/dlvenv2/lib/python3.10/site-packages/alpaca_trade_api/rest.py", line 270, in data_get
    return self._request(
  File "/home/bwilson/dlvenv2/lib/python3.10/site-packages/alpaca_trade_api/rest.py", line 213, in _request
    return self._one_request(method, url, opts, retry)
  File "/home/bwilson/dlvenv2/lib/python3.10/site-packages/alpaca_trade_api/rest.py", line 234, in _one_request
    resp.raise_for_status()
  File "/home/bwilson/dlvenv2/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://data.alpaca.markets/v2/stocks/AAPL/bars?timeframe=minute&adjustment=raw&start=1

Items related to my account work, e.g. open positions and available balance.

This example is essentially from the docs. I did change barset to bars due to a user comment.

What is wrong?

@Z223I The get_bars method has different parameters than the get_barset method. You changed the method name but didn’t update the parameters. Here are is the parameter definition

  def get_bars(self,
                 symbol: Union[str, List[str]],
                 timeframe: TimeFrame,
                 start: Optional[str] = None,
                 end: Optional[str] = None,
                 adjustment: str = 'raw',
                 limit: int = None,
                 feed: Optional[str] = None,
                 asof: Optional[str] = None,
                 )

Basically a couple of things 1) change the timeframe to 1Min and 2) add the start and end times if desired (otherwise the default per the API see here)

So, try this

import config

base_url = "https://paper-api.alpaca.markets"

import alpaca_trade_api as tradeapi
api = tradeapi.REST(config.API_KEY, config.SECRET_KEY, base_url=base_url, api_version='v2')
# api = tradeapi.REST()

symbol = 'AAPL'
symbol_bars = api.get_bars(symbol, '1Min').df.iloc[0]

Thank you very much!

Is this the correct API_KEY?

"API_KEY": "PKICLKI2GTS5NFTBG34U"'

Sorry. That should be from my account.

@Z223I I can’t really check API keys (and certainly not secret keys). You can verify you have the correct ones buy going to the Home screen in the web UI. There is a button on the left side labeled "View API Keys". Click that. A screen similar to the one below will pop up. You can verify your API key there. You won’t be able to see your Secret Key. That is only displayed once immediately after regenerating your key. If you forgot that, or are unsure if you have the correct one, you will need to regenerate your keys by clicking the button labeled "Regenerate".

Thank you very much for your assistance! Ultimately, it was a loose nut behind the keyboard.

1 Like