{"id":13424760,"url":"https://github.com/tomwojcik/starlette-context","last_synced_at":"2025-05-14T09:13:14.171Z","repository":{"id":37732989,"uuid":"219023167","full_name":"tomwojcik/starlette-context","owner":"tomwojcik","description":"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.","archived":false,"fork":false,"pushed_at":"2025-04-14T04:05:59.000Z","size":639,"stargazers_count":491,"open_issues_count":15,"forks_count":25,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-05-08T19:13:12.752Z","etag":null,"topics":["fastapi","middleware","python","python3","starlette","starlette-context"],"latest_commit_sha":null,"homepage":"https://starlette-context.readthedocs.io/en/latest/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tomwojcik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["tomwojcik"]}},"created_at":"2019-11-01T16:32:04.000Z","updated_at":"2025-05-06T12:43:22.000Z","dependencies_parsed_at":"2024-01-07T10:50:40.887Z","dependency_job_id":"0a685f55-b305-412e-a14e-5012ff55bd16","html_url":"https://github.com/tomwojcik/starlette-context","commit_stats":{"total_commits":148,"total_committers":9,"mean_commits":"16.444444444444443","dds":0.75,"last_synced_commit":"f46610a2aaae3d542a1f2a8e9f6442bcce06e0ec"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomwojcik%2Fstarlette-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomwojcik%2Fstarlette-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomwojcik%2Fstarlette-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomwojcik%2Fstarlette-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomwojcik","download_url":"https://codeload.github.com/tomwojcik/starlette-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254110377,"owners_count":22016391,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastapi","middleware","python","python3","starlette","starlette-context"],"created_at":"2024-07-31T00:00:58.883Z","updated_at":"2025-05-14T09:13:14.160Z","avatar_url":"https://github.com/tomwojcik.png","language":"Python","funding_links":["https://github.com/sponsors/tomwojcik"],"categories":["Third-Party Extensions","Python","Extensions","Monitoring"],"sub_categories":["Utils","Other","Core Utilities"],"readme":"# starlette-context\n\n[![Test Suite](https://github.com/tomwojcik/starlette-context/actions/workflows/test-suite.yml/badge.svg)](https://github.com/tomwojcik/starlette-context/actions/workflows/test-suite.yml)\n[![Python](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/release/python-390/)\n[![PyPI version](https://badge.fury.io/py/starlette-context.svg)](https://badge.fury.io/py/starlette-context)\n[![codecov](https://codecov.io/gh/tomwojcik/starlette-context/branch/master/graph/badge.svg)](https://codecov.io/gh/tomwojcik/starlette-context)\n[![Docs](https://readthedocs.org/projects/pip/badge/?version=latest)](https://starlette-context.readthedocs.io/)\n[![Downloads](https://img.shields.io/pypi/dm/starlette-context)](https://pypi.org/project/starlette-context/)\n\nMiddleware 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.\n\n## Resources\n\n- **Source**: https://github.com/tomwojcik/starlette-context\n- **Documentation**: https://starlette-context.readthedocs.io/\n- **Changelog**: https://starlette-context.readthedocs.io/en/latest/changelog.html\n\n## Installation\n\n```bash\n# Python 3.9+\npip install starlette-context\n```\n\n## Dependencies\n\n- `starlette\u003e=0.27.0`\n\n## Example\n\n```python\nimport uvicorn\n\nfrom starlette.applications import Starlette\nfrom starlette.middleware import Middleware\nfrom starlette.requests import Request\nfrom starlette.responses import JSONResponse\nfrom starlette.routing import Route\n\nfrom starlette_context import context, plugins\nfrom starlette_context.middleware import ContextMiddleware\n\nasync def index(request: Request):\n    # Access and store data in context\n    context[\"custom_value\"] = \"example\"\n    return JSONResponse(context.data)\n\n# Define routes\nroutes = [\n    Route(\"/\", endpoint=index)\n]\n\n# Configure middleware\nmiddleware = [\n    Middleware(\n        ContextMiddleware,\n        plugins=(\n            plugins.RequestIdPlugin(),\n            plugins.CorrelationIdPlugin()\n        )\n    )\n]\n\n# Create application with routes and middleware\napp = Starlette(\n    routes=routes,\n    middleware=middleware\n)\n\nif __name__ == \"__main__\":\n    uvicorn.run(app, host=\"0.0.0.0\")\n```\n\nIn this example the response contains a JSON with:\n\n```json\n{\n  \"X-Correlation-ID\": \"5ca2f0b43115461bad07ccae5976a990\",\n  \"X-Request-ID\": \"21f8d52208ec44948d152dc49a713fdd\",\n  \"custom_value\": \"example\"\n}\n```\n\nContext can be updated and accessed at anytime if it's created in the middleware.\n\n## Sponsorship\n\nA huge thank you to [Adverity](https://www.adverity.com/) for sponsoring the development of this OSS library.\n\n## Contribution\n\nSee the [contributing guide](https://starlette-context.readthedocs.io/en/latest/contributing.html).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomwojcik%2Fstarlette-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomwojcik%2Fstarlette-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomwojcik%2Fstarlette-context/lists"}