Skill v1.0.1
currentAutomated scan100/100+4 new
version: "1.0.1" name: azure-eventgrid-py description: Azure Event Grid SDK for Python. Use for publishing events, handling CloudEvents, and event-driven architectures. risk: critical source: community date_added: '2026-02-27'
Azure Event Grid SDK for Python
Event routing service for building event-driven applications with pub/sub semantics.
Installation
pip install azure-eventgrid azure-identity
Environment Variables
EVENTGRID_TOPIC_ENDPOINT=https://<topic-name>.<region>.eventgrid.azure.net/api/eventsEVENTGRID_NAMESPACE_ENDPOINT=https://<namespace>.<region>.eventgrid.azure.net
Authentication
from azure.identity import DefaultAzureCredentialfrom azure.eventgrid import EventGridPublisherClientcredential = DefaultAzureCredential()endpoint = "https://<topic-name>.<region>.eventgrid.azure.net/api/events"client = EventGridPublisherClient(endpoint, credential)
Event Types
| Format | Class | Use Case | |
|---|---|---|---|
| Cloud Events 1.0 | CloudEvent | Standard, interoperable (recommended) | |
| Event Grid Schema | EventGridEvent | Azure-native format |
Publish CloudEvents
from azure.eventgrid import EventGridPublisherClientfrom azure.core.messaging import CloudEventfrom azure.identity import DefaultAzureCredentialclient = EventGridPublisherClient(endpoint, DefaultAzureCredential())# Single eventevent = CloudEvent(type="MyApp.Events.OrderCreated",source="/myapp/orders",data={"order_id": "12345", "amount": 99.99})client.send(event)# Multiple eventsevents = [CloudEvent(type="MyApp.Events.OrderCreated",source="/myapp/orders",data={"order_id": f"order-{i}"})for i in range(10)]client.send(events)
Publish EventGridEvents
from azure.eventgrid import EventGridEventfrom datetime import datetime, timezoneevent = EventGridEvent(subject="/myapp/orders/12345",event_type="MyApp.Events.OrderCreated",data={"order_id": "12345", "amount": 99.99},data_version="1.0")client.send(event)
Event Properties
CloudEvent Properties
event = CloudEvent(type="MyApp.Events.ItemCreated", # Required: event typesource="/myapp/items", # Required: event sourcedata={"key": "value"}, # Event payloadsubject="items/123", # Optional: subject/pathdatacontenttype="application/json", # Optional: content typedataschema="https://schema.example", # Optional: schema URLtime=datetime.now(timezone.utc), # Optional: timestampextensions={"custom": "value"} # Optional: custom attributes)
EventGridEvent Properties
event = EventGridEvent(subject="/myapp/items/123", # Required: subjectevent_type="MyApp.ItemCreated", # Required: event typedata={"key": "value"}, # Required: event payloaddata_version="1.0", # Required: schema versiontopic="/subscriptions/.../topics/...", # Optional: auto-setevent_time=datetime.now(timezone.utc) # Optional: timestamp)
Async Client
from azure.eventgrid.aio import EventGridPublisherClientfrom azure.identity.aio import DefaultAzureCredentialasync def publish_events():async with DefaultAzureCredential() as credential, \EventGridPublisherClient(endpoint, credential) as client:event = CloudEvent(type="MyApp.Events.Test",source="/myapp",data={"message": "hello"})await client.send(event)import asyncioasyncio.run(publish_events())
Namespace Topics (Event Grid Namespaces)
For Event Grid Namespaces (pull delivery):
from azure.eventgrid.aio import EventGridPublisherClient# Namespace endpoint (different from custom topic)namespace_endpoint = "https://<namespace>.<region>.eventgrid.azure.net"topic_name = "my-topic"async with EventGridPublisherClient(endpoint=namespace_endpoint,credential=credential, # an open azure.identity.aio credentialnamespace_topic=topic_name,) as client:await client.send(event)
Namespace topics support CloudEvents; construct the namespace client with namespace_topic, not as a send-only keyword. Match examples to the installed SDK version. Use a test topic and synthetic payloads; publishing can trigger real subscribers. Authentication needs the scoped data-sender role, and application consumers must handle duplicate delivery/idempotency. Close both async clients and credentials. See official Python SDK guide.
Best Practices
- Use CloudEvents for new applications (industry standard)
- Batch events when publishing multiple events
- Include meaningful subjects for filtering
- Use async client for high-throughput scenarios
- Handle retries — Event Grid has built-in retry
- Set appropriate event types for routing and filtering
When to Use
This skill is applicable to execute the workflow or actions described in the overview.
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.