r/FastAPI • u/ageo89 • Feb 23 '26
Question A faster way to policy enforcement for FastAPI / OpenAPI endpoints?
Hi everyone—quick question from the trenches.
I’m building an API authorization layer for a FastAPI app wondering what the most reliable approach is for auto-discovering endpoints + enforcing policy.
Right now I’m dealing with one of these pain points:
Lots of routes, decorators, and routers
Repeated/duplicated authz checks everywhere
Policies are moving targets as endpoints evolve
Want to avoid brittle manual mapping of route → policy
Is there a practical pattern for:
Scanning FastAPI/OpenAPI endpoint definitions at runtime (or startup)
Auto-deriving policy context (path, method, tags, operationId, request shape, etc.)
Enforcing policy automatically in one place (middleware/dependency) with low latency
Failing closed when a policy is missing/ambiguous during tests / CICD
Any examples using pure FastAPI internals (app.router.routes, OpenAPI schema, dependencies, middleware), or with policy engines (like OPA/Casbin/etc.) are welcome.
Also open to thoughts on tradeoffs:
startup-time validation vs runtime checks
endpoint caching strategy for perf
testing strategy to keep this deterministic and safe
Has anyone solved this cleanly without introducing a lot of manual plumbing? Thanks!
3
u/coldflame563 Feb 23 '26
Casbin is your friend.