CodeSampleX

Sample

fastapi 0.141.1: Configure FastAPI behind a reverse proxy prefix with root_path and root_path_in_servers without route matching 404s or broken Swagger UI docs

Verified sample for pypi fastapi 0.141.1: Configure FastAPI behind a reverse proxy prefix with root_path and root_path_in_servers without route matching 404s…

sha256:fc03836e42df24fc9ebcc02153f8212390e8abec2d04d133be4cc5598abd71d4

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
2
Signing keys that built it
2
Declared environment python linux x64 python python pip

Verification-run environments

Environment Contract Stages Run
python 3.12 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-16
python 3.12 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-18

Case

HOW
Goal
Configure FastAPI behind a reverse proxy prefix with root_path and root_path_in_servers without route matching 404s or broken Swagger UI docs
Packages
Symbols
  • fastapi.FastAPI
  • root_path
  • root_path_in_servers
  • fastapi.testclient.TestClient
  • starlette.routing.get_route_path
Environment
python
Created
2026-08-16T06:22:44Z

Contract

  1. assert Starlette get_route_path strips root_path from ASGI scope path, dispatching /api/v1/items/42 to @app.get('/items/{item_id}')
  2. assert defining route decorators with root_path prefix as @app.get('/api/v1/trapped') fails matching and 404s on /api/v1/trapped
  3. assert a trailing slash on root_path='/api/v1/' prevents prefix stripping in get_route_path, returning 404 on proxied requests
  4. assert request.url_for automatically includes root_path without manual string concatenation
  5. assert static app.openapi() contains no servers, while the ASGI GET /openapi.json endpoint dynamically injects [{'url': '/api/v1'}]
  6. assert FastAPI(root_path_in_servers=False) suppresses OpenAPI servers injection
  7. assert Swagger UI /docs dynamically configures the JS bundle to fetch /api/v1/openapi.json
  8. assert mounting a sub-app at /v2 composes root_path to /api/v1/v2 for routing, url_for, and OpenAPI documentation

Files

  • NOTES.md
  • csx.json
  • requirements.txt
  • src/__init__.py
  • src/app.py
  • test/contract.py

Download the source artifact (tar.gz)

Origin Seeder

csx-seed