--- id: starlette-context version: "0.5.1" license: MIT license_treatment: permissive maintenance: active --- # 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. License: permissive · Maintenance: active · Downloads: 2.4M/mo ## What it is and what it does starlette-context is middleware for the Starlette web framework that manages request-scoped context—data that belongs to a single HTTP request and should be accessible throughout its lifecycle. It wraps Starlette's request handling to provide a thread-safe or async-safe context store, populated by configurable plugins that extract headers (like x-request-id or x-correlation-id) and make them available to your route handlers, logging, and other request-processing code without passing them explicitly through function parameters. The package ships with built-in plugins for common patterns like request ID and correlation ID extraction, and you can write custom plugins to populate context with application-specific data. This is particularly useful for distributed tracing, structured logging, and audit trails where you want request identifiers to flow automatically through logs and downstream services. It integrates cleanly with Starlette's middleware stack and works with FastAPI applications built on Starlette. Use it for: - Automatic request ID injection into logs so all log entries for a request share a common identifier without manual threading. - Distributed tracing: propagate correlation IDs through microservices by extracting them from incoming headers and making them available to outbound calls. - Audit logging: store user ID or tenant ID in context during request processing and access it in database operations or business logic without parameter passing. - Multi-tenant applications: extract and store tenant context from request headers to enforce isolation in downstream database queries or API calls. - Request metadata enrichment: populate context with parsed authentication tokens, feature flags, or client metadata for use across handlers. ## Worth the install? AI-flagged interpretation of the facts above — verify before relying. Middleware for Starlette that stores and accesses request context data, enabling automatic injection of request headers like x-request-id or x-correlation-id into logs and application state. Yes. The package is actively maintained, has no known vulnerabilities, low install friction, and solves a real problem in web applications—making request-scoped data available throughout the request lifecycle without manual parameter threading. It is well-suited for any Starlette or FastAPI application that needs structured logging, distributed tracing, or request-scoped state management. ## Install pip install starlette-context uv add starlette-context poetry add starlette-context ## Installing starlette-context Before you install: Low friction: pure Python wheel with a single runtime dependency on starlette>=0.27.0. Actively maintained with a recent release and no known vulnerabilities. License in practice: MIT license permits commercial and private use with minimal restrictions; you may use, modify, and distribute this package freely provided you include the license notice. Quickstart: pip install starlette-context from starlette_context import context, plugins from starlette_context.middleware import ContextMiddleware from starlette.applications import Starlette from starlette.middleware import Middleware middleware = [ Middleware( ContextMiddleware, plugins=(plugins.RequestIdPlugin(), plugins.CorrelationIdPlugin()) ) ] app = Starlette(middleware=middleware) context["key"] = "value" # Access in request handlers Requires Python 3.10 or later and starlette>=0.27.0. Verify before relying: - Whether the package supports async context isolation across concurrent requests without thread-local or context-var conflicts. - Performance overhead of middleware on high-throughput applications. - Compatibility with other context-aware middleware or frameworks that also manage request state. ## Package facts - License: MIT (permissive) - Python support: supports_current - Install friction: low - Maintenance: active - Downloads: 2.4M/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags starlette request context middleware, request id correlation id logging, starlette context storage, request header injection middleware, fastapi request context, starlette middleware plugins, request tracing headers, request-context, distributed-tracing, logging-integration [View on SkillFed](https://skillfed.io/packages/starlette-context) · [View on PyPI](https://pypi.org/project/starlette-context/)