---
title: "Python and FastAPI"
url: https://devlanes.run/examples/languages-python-fastapi/
description: "Run a FastAPI app under uv behind the HTTPS proxy, and call other HTTPS services from it."
---

# Python and FastAPI

Two FastAPI apps run with uv. `web` renders Jinja2 pages under uvicorn and calls `quotes` over HTTPS with httpx, which trusts the devlanes CA through `SSL_CERT_FILE`.

## Requirements

- devlanes, with `devlanes setup` run once
- uv and Python 3.12

## Run it

```sh
cd languages-python-fastapi
devlanes start
```

`devlanes start --no-ui` prints the start as text:

```text
example-python-fastapi@0 (main): 2/2 ready
ready web http://127.0.0.1:20591 https://example-python-fastapi.localhost
ready quotes http://127.0.0.1:20590 https://quotes.example-python-fastapi.localhost
```

Open https://example-python-fastapi.localhost. The page shows lane 0 of `example-python-fastapi`, the `request.url` uvicorn saw, the answer of the lane's `quotes` service, and whether your browser's own call to `quotes` passed CORS.

## During a start

1. On a lane's first start the `sync` task (`when: lane.create`) runs `uv sync --frozen`, which builds `.venv` from `uv.lock`. Later starts skip it.
2. `web` runs `uv run uvicorn acme.web.main:app` with `--port ${{ self.port }}` and serves https://example-python-fastapi.localhost. `quotes` runs `uv run quotes`, the entry point in `[project.scripts]`, which reads `PORT` and `HOST`, and serves https://quotes.example-python-fastapi.localhost.
3. Each service counts as ready once `/healthz` answers. The 3 minute timeout leaves room for `uv run` to sync again after a `uv.lock` change.

devlanes detects the `python` platform from `pyproject.toml` and gives both services `SSL_CERT_FILE` and `REQUESTS_CA_BUNDLE` (system roots plus the devlanes CA) and `PYTHONUNBUFFERED=1`. `web` gets `QUOTES_URL` from `${{ services.quotes.public_url }}`, so its httpx call goes through the proxy over HTTPS. The proxy sends `X-Forwarded-Proto: https` and `--proxy-headers` makes uvicorn use it, so `request.url` and the page's `url_for` links start with `https://`.

## Try next

- Start a second lane: `git worktree add ../examples-checkout -b feat/checkout`, then `cd ../examples-checkout/languages-python-fastapi && devlanes start`. https://example-python-fastapi1.localhost says lane 1, and its `quotes` answer comes from lane 1 too.
- Without a worktree, `devlanes lane new --name example-python-fastapi-review --start` gives this folder a second lane, and `devlanes lane clean example-python-fastapi-review --yes` removes it.
- `devlanes env show web --explain` lists `SSL_CERT_FILE`, `REQUESTS_CA_BUNDLE` and `PYTHONUNBUFFERED` in the `contributor` layer. Your own shell does not have them, so the same httpx call from there fails with `httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED]`:

  ```sh
  uv run python -c "import httpx; print(httpx.get('https://quotes.example-python-fastapi.localhost/healthz').text)"
  ```

- Change the footer in `src/acme/web/templates/base.html` and reload the page: Jinja reads the template again without a restart.
- Change `src/acme/web/main.py` and `--reload` restarts the server; `devlanes logs web --tail 10` shows `StatReload detected changes in 'src\acme\web\main.py'. Reloading...` (on Windows).
- `quotes` sends CORS headers only to this lane's origins. This prints `access-control-allow-origin: https://example-python-fastapi.localhost`, and prints nothing with lane 1's origin, `https://example-python-fastapi1.localhost` (on Windows add `--ssl-revoke-best-effort`):

  ```sh
  curl -sS -o /dev/null -D - -H "Origin: https://example-python-fastapi.localhost" https://quotes.example-python-fastapi.localhost/api/quotes | grep -i access-control
  ```

## Files

- [`devlanes.yaml`](https://github.com/devlanes-run/examples/blob/d2a99f076179db99581cef85453a58b788f3970b/languages-python-fastapi/devlanes.yaml): the `sync` task, `web` as a `run` string, `quotes` as a `run_script`, each with `hosts` and an `http` ready check.
- [`pyproject.toml`](https://github.com/devlanes-run/examples/blob/d2a99f076179db99581cef85453a58b788f3970b/languages-python-fastapi/pyproject.toml): the pinned dependencies and the `quotes` entry point.
- [`src/acme/settings.py`](https://github.com/devlanes-run/examples/blob/d2a99f076179db99581cef85453a58b788f3970b/languages-python-fastapi/src/acme/settings.py): every variable the apps read, parsed once.
- [`src/acme/web/`](https://github.com/devlanes-run/examples/blob/d2a99f076179db99581cef85453a58b788f3970b/languages-python-fastapi/src/acme/web): the page, the httpx call in `quotes_client.py` and the Jinja templates.
- [`src/acme/quotes/`](https://github.com/devlanes-run/examples/blob/d2a99f076179db99581cef85453a58b788f3970b/languages-python-fastapi/src/acme/quotes): the API, with CORS from `DEVLANES_ORIGINS` and the `main()` that `uv run quotes` calls.

## Docs

- [Languages: Python](/docs/guides/languages/#python)
- [Run and run_script: pyproject.toml](/docs/guides/run-scripts/#pyprojecttoml)
- [Programs started by devlanes](/docs/guides/https/#programs-started-by-devlanes)
- [Lifecycle tasks](/docs/guides/lifecycle/#lifecycle-tasks)
