Skip to main content

Access context in Starlette

Project description

Test Suite Python PyPI version codecov Docs Downloads Language grade: Python

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.



$ pip install starlette-context


Python 3.7+


  • starlette

All other dependencies from requirements-dev.txt are only needed to run tests or examples.


import uvicorn

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.responses import JSONResponse

from starlette_context import context, plugins
from starlette_context.middleware import RawContextMiddleware

middleware = [

app = Starlette(middleware=middleware)

async def index(request: Request):
    return JSONResponse(, host="")

In this example the response contains a json with


Context can be updated and accessed at anytime if it's created in the middleware.


A huge thank you to Adverity for sponsoring the development of this OSS library in 2022.


See the guide on read the docs.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette_context-0.3.5.tar.gz (9.2 kB view hashes)

Uploaded source

Built Distribution

starlette_context-0.3.5-py37-none-any.whl (12.7 kB view hashes)

Uploaded py37

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page