FastAPI middleware for request ID propagation with support for httpx, aiohttp, and requests
Project description
FastAPI Request ID
A FastAPI middleware for request ID propagation and tracing across microservices.
Quick Start
1. Add Middleware
from fastapi import FastAPI
from fastapi_reqid import RequestIDMiddleware
app = FastAPI()
# Add middleware with default settings
app.add_middleware(RequestIDMiddleware)
# Or customize it
app.add_middleware(
RequestIDMiddleware,
header_name="X-Correlation-ID", # Custom header name
generator=lambda: str(uuid.uuid4()) # Custom ID generator
)
2. Access Request ID
from fastapi import Request
from fastapi_reqid import get_request_id
@app.get("/")
async def root(request: Request):
# Access via request.state
request_id = request.state.request_id
# Or use the context helper
request_id = get_request_id()
return {"request_id": request_id}
HTTP Client Decorators
Automatically inject request IDs into outgoing HTTP requests.
httpx
from fastapi import Request
from fastapi_reqid import inject_httpx_requestid
@app.get("/external")
@inject_httpx_requestid
async def call_external(request: Request):
client = request.state.httpx_client
response = await client.get("https://api.example.com")
return response.json()
aiohttp
from fastapi import Request
from fastapi_reqid import inject_aiohttp_requestid
@app.get("/external")
@inject_aiohttp_requestid
async def call_external(request: Request):
session = request.state.aiohttp_session
async with session.get("https://api.example.com") as resp:
return await resp.json()
requests
from fastapi import Request
from fastapi_reqid import inject_requests_requestid
@app.get("/external")
@inject_requests_requestid
def call_external(request: Request):
session = request.state.requests_session
response = session.get("https://api.example.com")
return response.json()
Manual Request ID Management
Use these functions to manually get or set request IDs:
from fastapi_reqid import get_request_id, set_request_id, request_id_context
# Get current request ID
request_id = get_request_id()
# Set request ID manually
token = set_request_id("custom-request-id")
try:
# Do work with this request ID
pass
finally:
# Reset to previous value
request_id_context.reset(token)
Building and Publishing
Building the Library
This project uses uv for building. To build the distribution packages:
# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh
# Build the package (creates wheel and source distribution)
uv build
This will create two files in the dist/ directory:
fastapi_reqid-{version}-py3-none-any.whl- Python wheelfastapi_reqid-{version}.tar.gz- Source distribution
Publishing to PyPI
Test on TestPyPI First (Recommended)
Before publishing to the main PyPI, test your package on TestPyPI:
# Publish to TestPyPI
uv publish --publish-url https://test.pypi.org/legacy/
You'll need a TestPyPI account and API token from test.pypi.org.
Publish to PyPI
Once you've verified the package works correctly:
# Publish to PyPI
uv publish
You'll need a PyPI account and API token from pypi.org.
Development Setup
To set up a development environment:
# Create virtual environment
uv venv
# Install package in editable mode with dev dependencies
uv pip install -e ".[dev]"
# Run tests
pytest
How It Works
- The middleware extracts the request ID from incoming headers (default:
X-Request-ID) - If no request ID is found, it generates one using UUID v4
- The request ID is stored in both
request.stateand a context variable - The request ID is automatically added to response headers
- Context is automatically cleaned up after each request
License
MIT License - see LICENSE file for details
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_reqid-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_reqid-0.1.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78897ba7578dfef9c9eda142bd606c9d28ab4e2d07adaaab23a23e2291b3e2f3
|
|
| MD5 |
a9a1e94d779edb265b0bbc8670ba0902
|
|
| BLAKE2b-256 |
ab909975acd8e5e861ba738210df70f0a0ab99c14804bf1e48c0671a53abe197
|
File details
Details for the file fastapi_reqid-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_reqid-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3ba5db518b573ce4ed5868a27aae93dd7c338b935d8083c821eee46797d1da8
|
|
| MD5 |
0065726c425b113fb5304c0d0251a574
|
|
| BLAKE2b-256 |
101e2884f4e937e832dde3f90ebeddfb95b49996592efc247d55d24201615ef8
|