https://github.com/tomwojcik/starlette-context
Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automatically use request headers such as x-request-id or x-correlation-id.
https://github.com/tomwojcik/starlette-context
fastapi middleware python python3 starlette starlette-context
Last synced: about 1 month ago
JSON representation
Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automatically use request headers such as x-request-id or x-correlation-id.
- Host: GitHub
- URL: https://github.com/tomwojcik/starlette-context
- Owner: tomwojcik
- License: mit
- Created: 2019-11-01T16:32:04.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-04-14T04:05:59.000Z (2 months ago)
- Last Synced: 2025-05-08T19:13:12.752Z (about 1 month ago)
- Topics: fastapi, middleware, python, python3, starlette, starlette-context
- Language: Python
- Homepage: https://starlette-context.readthedocs.io/en/latest/
- Size: 624 KB
- Stars: 491
- Watchers: 7
- Forks: 25
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Authors: AUTHORS
Awesome Lists containing this project
- awesome-fastapi - Starlette Context - Allows you to store and access the request data anywhere in your project, useful for logging. (Third-Party Extensions / Utils)
- awesome-fastapi - Starlette Context - Allows you to store and access the request data anywhere in your project, useful for logging. (Third-Party Extensions / Utils)
- awesome-starlette - Starlette Context - Middleware to store and access request context data easily, useful for logging request IDs. (Extensions / Core Utilities)
- best-of-web-python - GitHub - 28% open · ⏱️ 07.11.2023): (Monitoring)
README
# starlette-context
[](https://github.com/tomwojcik/starlette-context/actions/workflows/test-suite.yml)
[](https://www.python.org/downloads/release/python-390/)
[](https://badge.fury.io/py/starlette-context)
[](https://codecov.io/gh/tomwojcik/starlette-context)
[](https://starlette-context.readthedocs.io/)
[](https://pypi.org/project/starlette-context/)Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automatically use request headers such as x-request-id or x-correlation-id.
## Resources
- **Source**: https://github.com/tomwojcik/starlette-context
- **Documentation**: https://starlette-context.readthedocs.io/
- **Changelog**: https://starlette-context.readthedocs.io/en/latest/changelog.html## Installation
```bash
# Python 3.9+
pip install starlette-context
```## Dependencies
- `starlette>=0.27.0`
## Example
```python
import uvicornfrom starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Routefrom starlette_context import context, plugins
from starlette_context.middleware import ContextMiddlewareasync def index(request: Request):
# Access and store data in context
context["custom_value"] = "example"
return JSONResponse(context.data)# Define routes
routes = [
Route("/", endpoint=index)
]# Configure middleware
middleware = [
Middleware(
ContextMiddleware,
plugins=(
plugins.RequestIdPlugin(),
plugins.CorrelationIdPlugin()
)
)
]# Create application with routes and middleware
app = Starlette(
routes=routes,
middleware=middleware
)if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0")
```In this example the response contains a JSON with:
```json
{
"X-Correlation-ID": "5ca2f0b43115461bad07ccae5976a990",
"X-Request-ID": "21f8d52208ec44948d152dc49a713fdd",
"custom_value": "example"
}
```Context can be updated and accessed at anytime if it's created in the middleware.
## Sponsorship
A huge thank you to [Adverity](https://www.adverity.com/) for sponsoring the development of this OSS library.
## Contribution
See the [contributing guide](https://starlette-context.readthedocs.io/en/latest/contributing.html).