Skip to main content
A clean, reusable pattern using FastAPI’s dependency injection system.

Install

Basic Dependency

dependencies/auth.py

Use in Routes

main.py

Typed Response Model

Create a Pydantic model for the auth result:
models/auth.py
dependencies/auth.py

Permission-Based Access

Create dependencies for different permission levels:
dependencies/auth.py
Use in routes:
main.py

Rate Limit Headers

Return rate limit info in response headers:
dependencies/auth.py

Async Client

For better performance with async FastAPI:
dependencies/auth.py
main.py

Full Example

main.py
Run with:
Last modified on April 7, 2026