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
# Python 3.10+
pip install starlette-context
Dependencies
starlette>=0.27.0
Example
import uvicorn
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route
from starlette_context import context, plugins
from starlette_context.middleware import ContextMiddleware
async 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:
{
"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 for sponsoring the development of this OSS library.
Contribution
See the contributing guide.
Release files for starlette-context 0.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| starlette_context-0.5.1.tar.gz | 8.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| starlette_context-0.5.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.2 kB
Release files / starlette_context-0.5.1.tar.gz
| Download URL | starlette_context-0.5.1.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f6025753f8ede041778b2f2c3823e2da9df3c2d94832575bcb59374216b7c2b2
|
|
BLAKE2b-256 checksum How to use checksums |
29fdd28907759adf31f1112e71006b649c81a74a115b8c14645f87a5f5c83dd3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / starlette_context-0.5.1-py3-none-any.whl
| Download URL | starlette_context-0.5.1-py3-none-any.whl |
|---|---|
| Size | 12.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
df55da1d7ae3453e89874b9d5c1d40a98dd2f1293c37791dd7c1a907087c9b5e
|
|
BLAKE2b-256 checksum How to use checksums |
879fc1d842f7a628a1aaa6754f3e7d28218978af8ed28170c7f9eec49dc6401a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|