Skip to main content

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.

Project description

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.

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.5.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

starlette_context-0.5.1-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file starlette_context-0.5.1.tar.gz.

File metadata

  • Download URL: starlette_context-0.5.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for starlette_context-0.5.1.tar.gz
Algorithm Hash digest
SHA256 f6025753f8ede041778b2f2c3823e2da9df3c2d94832575bcb59374216b7c2b2
MD5 96bbd98fba1eb774f7ae5b08874c4fee
BLAKE2b-256 29fdd28907759adf31f1112e71006b649c81a74a115b8c14645f87a5f5c83dd3

See more details on using hashes here.

File details

Details for the file starlette_context-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: starlette_context-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for starlette_context-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 df55da1d7ae3453e89874b9d5c1d40a98dd2f1293c37791dd7c1a907087c9b5e
MD5 b3b101953194d895c6469e29736c4dbf
BLAKE2b-256 879fc1d842f7a628a1aaa6754f3e7d28218978af8ed28170c7f9eec49dc6401a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page