Skip to main content

Access context in Starlette

Project description

Build Status Python PyPI version codecov Code style: black Docs Downloads

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. Test/dev env is dockerized if you want to try them yourself.


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.


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.

Files for starlette-context, version 0.3.1
Filename, size File type Python version Upload date Hashes
Filename, size starlette_context-0.3.1-py37-none-any.whl (10.8 kB) File type Wheel Python version py37 Upload date Hashes View
Filename, size starlette_context-0.3.1.tar.gz (8.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page