Skip to main content

Python SDK v2 for CloudEvents

PyPI version

Status

This SDK is still considered a work in progress, therefore things might (and will) break with every update.

This SDK current supports the following versions of CloudEvents:

  • v1.0
  • v0.3

Python SDK

Package cloudevents provides primitives to work with CloudEvents specification.

Installing

The CloudEvents SDK can be installed with pip:

pip install cloudevents

Sending CloudEvents

Below we will provide samples on how to send cloudevents using the popular requests library.

Binary HTTP CloudEvent

from cloudevents.core.v1.event import CloudEvent
from cloudevents.core.bindings.http import to_binary_event
import requests

# Create a CloudEvent
# - The CloudEvent "id" is generated if omitted. "specversion" defaults to "1.0".
attributes = {
  "type": "com.example.sampletype1",
  "source": "https://example.com/event-producer",
}
data = {"message": "Hello World!"}
event = CloudEvent(attributes, data)

# Creates the HTTP request representation of the CloudEvent in binary content mode
message = to_binary_event(event)

# POST
requests.post("<some-url>", data=message.body, headers=message.headers)

Structured HTTP CloudEvent

from cloudevents.core.v1.event import CloudEvent
from cloudevents.core.bindings.http import to_structured_event
import requests

# Create a CloudEvent
# - The CloudEvent "id" is generated if omitted. "specversion" defaults to "1.0".
attributes = {
  "type": "com.example.sampletype2",
  "source": "https://example.com/event-producer",
}
data = {"message": "Hello World!"}
event = CloudEvent(attributes, data)

# Creates the HTTP request representation of the CloudEvent in structured content mode
message = to_structured_event(event)

# POST
requests.post("<some-url>", data=message.body, headers=message.headers)

You can find a complete example of turning a CloudEvent into a HTTP request in the samples' directory.

Receiving CloudEvents

The code below shows how to consume a cloudevent using the popular python web framework flask:

from flask import Flask, request

from cloudevents.core.bindings.http import from_http_event, HTTPMessage

app = Flask(__name__)


# create an endpoint at http://localhost:/3000/
@app.route("/", methods=["POST"])
def home():
  # create a CloudEvent
  event = from_http_event(HTTPMessage(dict(request.headers), request.get_data()))

  # you can access cloudevent fields as seen below
  print(
    f"Found {event.get_id()} from {event.get_source()} with type "
    f"{event.get_type()} and specversion {event.get_specversion()}"
  )

  return "", 204


if __name__ == "__main__":
  app.run(port=3000)

You can find a complete example of turning a CloudEvent into a HTTP request in the samples' directory.

SDK versioning

The goal of this package is to provide support for all released versions of CloudEvents, ideally while maintaining the same API. It will use semantic versioning with following rules:

  • MAJOR version increments when backwards incompatible changes is introduced.
  • MINOR version increments when backwards compatible feature is introduced INCLUDING support for new CloudEvents version.
  • PATCH version increments when a backwards compatible bug fix is introduced.

Community

Each SDK may have its own unique processes, tooling and guidelines, common governance related material can be found in the CloudEvents docs directory. In particular, in there you will find information concerning how SDK projects are managed, guidelines for how PR reviews and approval, and our Code of Conduct information.

If there is a security concern with one of the CloudEvents specifications, or with one of the project's SDKs, please send an email to cncf-cloudevents-security@lists.cncf.io.

Additional SDK Resources

Maintenance

We use uv for dependency and package management, ruff and isort for autoformatting and pre-commit to automate those with commit hooks.

For information on releasing version bumps see RELEASING.md

Release files for cloudevents 2.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cloudevents 2.2.0
File Size Uploaded
cloudevents-2.2.0.tar.gz 48.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cloudevents 2.2.0
File Interpreter ABI Platform
cloudevents-2.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 146.6 kB

Release files / cloudevents-2.2.0.tar.gz

Download URL cloudevents-2.2.0.tar.gz
Size 48.4 kB
Tags Source
SHA-256 checksum
How to use checksums
fc52440a1fecde882b3b745361e1c7dd4e044e71a6b719cfec2e2c0aa1a19fa3
BLAKE2b-256 checksum
How to use checksums
45fe3bae2187abea2682a1079c5618e45baf4c9df7e35017e1b9b7940955db59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / cloudevents-2.2.0-py3-none-any.whl

Download URL cloudevents-2.2.0-py3-none-any.whl
Size 98.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9ff8adff01b997d251d0fb54076557daa9906330e16afc1712a6447122f8f7a4
BLAKE2b-256 checksum
How to use checksums
d0d4901b17ea39f4f1125fc92529cb842896309075406a78e13a7c544f0c5600
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12
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