FastAPI Route Middleware
A dependency free™ middleware system for adding middleware to specific FastAPI routes. I just like how node express works.
Installation
pip install fastapi_route_middleware
but actually I know you're using uv, so:
uv add fastapi_route_middleware
Usage
The add_middleware decorator allows you to apply middleware functions to specific FastAPI routes, giving you fine-grained control over which middleware runs on which endpoints. Middlewares can access FastAPI Request and Response even if the original route does not. If a middleware returns anything, it will trigger an early return and won't cause the original route handler to be called.
Basic Example
from fastapi import FastAPI
from fastapi_route_middleware import add_middleware
app = FastAPI()
# Define a simple middleware function
def log_request(request_id: str):
print(f"Processing request {request_id}")
# Apply middleware to specific route
@app.get("/items/{item_id}")
@add_middleware(log_request)
async def get_item(item_id: int, request_id: str):
return {"item_id": item_id}
Async Middleware Example
from fastapi import FastAPI
from fastapi_route_middleware import add_middleware
import asyncio
app = FastAPI()
# Define an async middleware function
async def async_auth_middleware(user_id: str):
# Simulate async authentication check
await asyncio.sleep(0.1)
print(f"Authenticated user {user_id}")
@app.get("/protected")
@add_middleware(async_auth_middleware)
async def protected_route(user_id: str, data: str):
return {"message": f"Hello {user_id}", "data": data}
Multiple Middleware Example
from fastapi import FastAPI
from fastapi_route_middleware import add_middleware
app = FastAPI()
def rate_limit_middleware(api_key: str):
print(f"Rate limiting for API key: {api_key}")
def audit_middleware(user_id: str):
print(f"Auditing action for user: {user_id}")
# Stack multiple middlewares
@app.post("/api/data")
@add_middleware(rate_limit_middleware)
@add_middleware(audit_middleware)
async def create_data(api_key: str, user_id: str, payload: dict):
return {"status": "created", "data": payload}
How It Works
The add_middleware decorator:
- Executes the middleware before calling the original route handler
- Supports both sync and async middleware functions
- Preserves the original function's signature for FastAPI's dependency injection
Requirements
- Python >= 3.8
- FastAPI >= 0.68.0
License
MIT
Metadata
Release files for fastapi_route_middleware 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_route_middleware-0.1.0.tar.gz | 29.8 kB | Without provenance | Details for fastapi_route_middleware-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_route_middleware-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for fastapi_route_middleware-0.1.0-py3-none-any.whl |
Total release size: 33.8 kB
Release files / fastapi_route_middleware-0.1.0.tar.gz
| Download URL | fastapi_route_middleware-0.1.0.tar.gz |
|---|---|
| Size | 29.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e5d2898c327551c02bc92327667ca904187551fab63b8250106bc2f66676cc68
|
|
BLAKE2b-256 checksum How to use checksums |
06be97d8e50c4e630fb843e04afced84512aef145064292067d6c067dce5639f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.8
|
Release files / fastapi_route_middleware-0.1.0-py3-none-any.whl
| Download URL | fastapi_route_middleware-0.1.0-py3-none-any.whl |
|---|---|
| Size | 3.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
78d23e3ccab0612ac2ec3413e038d8fc6f5e55198219c594f0d841dc833ff38a
|
|
BLAKE2b-256 checksum How to use checksums |
cf8446258ec6f7dbf1856f57cc0116e205e436418f1be747908c3e359c9d6a06
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.8
|
Security for fastapi_route_middleware 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.