{"id":13806858,"url":"https://github.com/sasha-tkachev/fastapi-cloudevents","last_synced_at":"2025-04-28T15:22:15.856Z","repository":{"id":53887937,"uuid":"521671793","full_name":"sasha-tkachev/fastapi-cloudevents","owner":"sasha-tkachev","description":"CloudEvents FastAPI plugin.  Allows to easily consume and produce events in CloudEvents format over REST API.","archived":false,"fork":false,"pushed_at":"2024-02-24T19:50:59.000Z","size":126,"stargazers_count":35,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-28T15:21:58.772Z","etag":null,"topics":["cloudevents","event","event-drive","event-driven-architecture","events","fastapi","fastapi-extension","pydantic","python","rest","rest-api"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sasha-tkachev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-05T14:37:41.000Z","updated_at":"2025-03-09T09:15:13.000Z","dependencies_parsed_at":"2024-08-04T01:06:47.511Z","dependency_job_id":"2087aa77-6112-46ab-bc96-3b322eedca43","html_url":"https://github.com/sasha-tkachev/fastapi-cloudevents","commit_stats":{"total_commits":196,"total_committers":2,"mean_commits":98.0,"dds":0.005102040816326481,"last_synced_commit":"a8dbb04f6e0e35caed74515474eb2252b83c0665"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sasha-tkachev%2Ffastapi-cloudevents","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sasha-tkachev%2Ffastapi-cloudevents/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sasha-tkachev%2Ffastapi-cloudevents/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sasha-tkachev%2Ffastapi-cloudevents/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sasha-tkachev","download_url":"https://codeload.github.com/sasha-tkachev/fastapi-cloudevents/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251336425,"owners_count":21573196,"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":["cloudevents","event","event-drive","event-driven-architecture","events","fastapi","fastapi-extension","pydantic","python","rest","rest-api"],"created_at":"2024-08-04T01:01:17.144Z","updated_at":"2025-04-28T15:22:15.830Z","avatar_url":"https://github.com/sasha-tkachev.png","language":"Python","readme":"# fastapi-cloudevents\n\n[![](https://github.com/sasha-tkachev/fastapi-cloudevents/actions/workflows/main.yaml/badge.svg)](https://github.com/sasha-tkachev/fastapi-cloudevents/actions/workflows/main.yaml)\n[![](https://img.shields.io/badge/coverage-100%25-brightgreen)](https://github.com/sasha-tkachev/fastapi-cloudevents/blob/main/tests/test_docs.py#L35)\n\n[FastAPI](https://fastapi.tiangolo.com/) plugin for [CloudEvents](https://cloudevents.io/) Integration\n\nAllows to easily consume and produce CloudEvents over REST API.\n\nAutomatically parses CloudEvents both in the binary and structured format and\nprovides an interface very similar to the regular FastAPI interface. No more\nhustling with `to_structured` and `from_http` function calls!\n\n```python\n@app.post(\"/\")\nasync def on_event(event: CloudEvent) -\u003e CloudEvent:\n   pass\n```\n\nSee more examples below\n\n### Install\n\n```shell script\npip install fastapi-cloudevents\n```\n\n## Examples\n\n### [Simple Example](examples/simple_server)\n\n```python\nimport uvicorn\nfrom fastapi import FastAPI\n\nfrom fastapi_cloudevents import CloudEvent, install_fastapi_cloudevents\n\napp = FastAPI()\napp = install_fastapi_cloudevents(app)\n\n\n@app.post(\"/\")\nasync def on_event(event: CloudEvent) -\u003e CloudEvent:\n    return CloudEvent(\n        type=\"my.response-type.v1\",\n        data=event.data,\n        datacontenttype=event.datacontenttype,\n    )\n\n\nif __name__ == \"__main__\":\n    uvicorn.run(app, host=\"0.0.0.0\", port=8000)\n```\n\nThe rout accepts both binary CloudEvents\n\n```shell script\ncurl http://localhost:8000 -i -X POST -d \"Hello World!\" \\\n  -H \"Content-Type: text/plain\" \\\n  -H \"ce-specversion: 1.0\" \\\n  -H \"ce-type: my.request-type.v1\" \\\n  -H \"ce-id: 123\" \\\n  -H \"ce-source: my-source\"\n```\n\nAnd structured CloudEvents\n\n```shell script\ncurl http://localhost:8000 -i -X POST -H \"Content-Type: application/json\" \\\n  -d '{\"data\":\"Hello World\", \"source\":\"my-source\", \"id\":\"123\", \"type\":\"my.request-type.v1\",\"specversion\":\"1.0\"}'\n```\n\nBoth of the requests will yield a response in the same format:\n\n```text\nHTTP/1.1 200 OK\ndate: Fri, 05 Aug 2022 23:50:52 GMT\nserver: uvicorn\ncontent-length: 13\ncontent-type: application/json\nce-specversion: 1.0\nce-id: 25cd28f0-0605-4a76-b1d8-cffbe3375413\nce-source: http://localhost:8000/\nce-type: my.response-type.v1\nce-time: 2022-08-05T23:50:52.809697+00:00\n\n\"Hello World\"\n```\n\n### [CloudEvent Type Routing](examples/type_routing)\n\n```python\nfrom typing import Literal, Union\n\nimport uvicorn\nfrom fastapi import FastAPI, Body\nfrom pydantic import Field\nfrom typing_extensions import Annotated\n\nfrom fastapi_cloudevents import (\n    CloudEvent,\n    CloudEventSettings,\n    ContentMode,\n    install_fastapi_cloudevents,\n)\n\napp = FastAPI()\napp = install_fastapi_cloudevents(\n    app, settings=CloudEventSettings(default_response_mode=ContentMode.structured)\n)\n\n\nclass MyEvent(CloudEvent):\n    type: Literal[\"my.type.v1\"]\n\n\nclass YourEvent(CloudEvent):\n    type: Literal[\"your.type.v1\"]\n\n\nOurEvent = Annotated[Union[MyEvent, YourEvent], Body(discriminator=\"type\")]\n\n_source = \"dummy:source\"\n\n\n@app.post(\"/\")\nasync def on_event(event: OurEvent) -\u003e CloudEvent:\n    if isinstance(event, MyEvent):\n        return CloudEvent(\n            type=\"my.response-type.v1\",\n            data=f\"got {event.data} from my event!\",\n            datacontenttype=\"text/plain\",\n        )\n    else:\n        return CloudEvent(\n            type=\"your.response-type.v1\",\n            data=f\"got {event.data} from your event!\",\n            datacontenttype=\"text/plain\",\n        )\n\n\nif __name__ == \"__main__\":\n    uvicorn.run(app, host=\"0.0.0.0\", port=8002)\n```\n\n### [Structured Response Example](examples/structured_response_server)\n\nTo send the response in the http CloudEvent structured format, you MAY use the\n`BinaryCloudEventResponse` class\n\n```python\nimport uvicorn\nfrom fastapi import FastAPI\n\nfrom fastapi_cloudevents import (CloudEvent, StructuredCloudEventResponse,\n                                 install_fastapi_cloudevents)\n\napp = FastAPI()\napp = install_fastapi_cloudevents(app)\n\n\n@app.post(\"/\", response_class=StructuredCloudEventResponse)\nasync def on_event(event: CloudEvent) -\u003e CloudEvent:\n    return CloudEvent(\n        type=\"com.my-corp.response.v1\",\n        data=event.data,\n        datacontenttype=event.datacontenttype,\n    )\n\n\nif __name__ == \"__main__\":\n    uvicorn.run(app, host=\"0.0.0.0\", port=8001)\n\n```\n\n```shell script\ncurl http://localhost:8001 -i -X POST -d \"Hello World!\" \\\n  -H \"Content-Type: text/plain\" \\\n  -H \"ce-specversion: 1.0\" \\\n  -H \"ce-type: my.request-type.v1\" \\\n  -H \"ce-id: 123\" \\\n  -H \"ce-source: my-source\"\n```\n\n```text\nHTTP/1.1 200 OK\ndate: Fri, 05 Aug 2022 23:51:26 GMT\nserver: uvicorn\ncontent-length: 247\ncontent-type: application/json\n\n{\"data\":\"Hello World!\",\"source\":\"http://localhost:8001/\",\"id\":\"3412321f-85b3-4f7f-a551-f4c23a05de3a\",\"type\":\"com.my-corp.response.v1\",\"specversion\":\"1.0\",\"time\":\"2022-08-05T23:51:26.878723+00:00\",\"datacontenttype\":\"text/plain\"}\n```\n\n## More Examples\n\n- [Custom Default Source](examples/custom_default_source)\n- [Mixed Usage of events and regular models](examples/events_and_basemodels_mixed)\n","funding_links":[],"categories":["Third-Party Extensions"],"sub_categories":["Utils"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsasha-tkachev%2Ffastapi-cloudevents","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsasha-tkachev%2Ffastapi-cloudevents","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsasha-tkachev%2Ffastapi-cloudevents/lists"}