Trying to get live stock data via python

I’m trying to create a web app in python using Anvil and access Alpaca API for live stock data of AAPL

server code



@anvil.server.callable
def get_live_apple_prices():
  url = "https://data.alpaca.markets/v2/stocks/bars"

  now = datetime.now(timezone.utc).replace(microsecond=0)
  start = now - timedelta(minutes=15)

  # Format ISO timestamps with 'Z' instead of '+00:00'
  start_str = start.isoformat().replace("+00:00", "Z")
  end_str = now.isoformat().replace("+00:00", "Z")

  full_url = (
    f"{url}?symbols=AAPL"
    f"&start={start_str}"
    f"&end={end_str}"
    f"&timeframe=1Min"
  )

  print("Requesting:", full_url)

  r = requests.get(full_url, headers=headers)
  r.raise_for_status()

  bars = r.json().get("bars", {}).get("AAPL", [])

  return {
    "times": [b["t"] for b in bars],
    "prices": [b["c"] for b in bars]
  }


client code 

self.init_components(**properties)
    self.update_chart()

  def update_chart(self):
      data = anvil.server.call('get_live_apple_prices')
      
      x = [datetime.fromisoformat(t.rstrip("Z")) - timedelta(hours=4) for t in data["times"]]  # UTC → EST
      y = data["prices"]

      fig = go.Figure(
        data=[go.Scatter(x=x, y=y, mode="lines+markers")],
        layout=dict(
          title="AAPL – Live (Last 15 Minutes)",
          xaxis_title="Time (EST)",
          yaxis_title="Price (USD)"
        )
      )

      self.plot_1.figure = fig
      if hasattr(self, 'status_label'):
        self.status_label.text = "Live data updated"

  def timer_1_tick(self, **event_args):
    self.update_chart()

When I do this it returns a 403 HTTP error and my API KEY and Secret Key are correct, so im guessing its the url but I’m not sure how to go about this

I have never used this API, but have you printed out the URLs you are trying to get and then tried running the request through the python request module just on your computer? That would rule out the URL being a problem.

1 Like

Welcome to the forum!

Maybe I’m missing something, but you’re not sending anything in the headers. You should be creating a dictionary for the headers that has your API keys in it, according to the Alpaca docs.

And the 403 error from Alpaca is typically a key issue, so I’d start by creating the headers dict and passing it to requests.get.

2 Likes