Skip to main content

starlette-context

Test Suite Python PyPI version codecov Docs Downloads

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

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)

Source distribution for starlette-context 0.5.1
File Size Uploaded
starlette_context-0.5.1.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for starlette-context 0.5.1
File Interpreter ABI Platform
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}

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.1

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page