Add this skill
npx mdskills install sickn33/azure-eventgrid-pyWell-structured SDK reference with clear examples for both sync and async publishing patterns
1---2name: azure-eventgrid-py3description: |4 Azure Event Grid SDK for Python. Use for publishing events, handling CloudEvents, and event-driven architectures.5 Triggers: "event grid", "EventGridPublisherClient", "CloudEvent", "EventGridEvent", "publish events".6package: azure-eventgrid7---89# Azure Event Grid SDK for Python1011Event routing service for building event-driven applications with pub/sub semantics.1213## Installation1415```bash16pip install azure-eventgrid azure-identity17```1819## Environment Variables2021```bash22EVENTGRID_TOPIC_ENDPOINT=https://<topic-name>.<region>.eventgrid.azure.net/api/events23EVENTGRID_NAMESPACE_ENDPOINT=https://<namespace>.<region>.eventgrid.azure.net24```2526## Authentication2728```python29from azure.identity import DefaultAzureCredential30from azure.eventgrid import EventGridPublisherClient3132credential = DefaultAzureCredential()33endpoint = "https://<topic-name>.<region>.eventgrid.azure.net/api/events"3435client = EventGridPublisherClient(endpoint, credential)36```3738## Event Types3940| Format | Class | Use Case |41|--------|-------|----------|42| Cloud Events 1.0 | `CloudEvent` | Standard, interoperable (recommended) |43| Event Grid Schema | `EventGridEvent` | Azure-native format |4445## Publish CloudEvents4647```python48from azure.eventgrid import EventGridPublisherClient, CloudEvent49from azure.identity import DefaultAzureCredential5051client = EventGridPublisherClient(endpoint, DefaultAzureCredential())5253# Single event54event = CloudEvent(55 type="MyApp.Events.OrderCreated",56 source="/myapp/orders",57 data={"order_id": "12345", "amount": 99.99}58)59client.send(event)6061# Multiple events62events = [63 CloudEvent(64 type="MyApp.Events.OrderCreated",65 source="/myapp/orders",66 data={"order_id": f"order-{i}"}67 )68 for i in range(10)69]70client.send(events)71```7273## Publish EventGridEvents7475```python76from azure.eventgrid import EventGridEvent77from datetime import datetime, timezone7879event = EventGridEvent(80 subject="/myapp/orders/12345",81 event_type="MyApp.Events.OrderCreated",82 data={"order_id": "12345", "amount": 99.99},83 data_version="1.0"84)8586client.send(event)87```8889## Event Properties9091### CloudEvent Properties9293```python94event = CloudEvent(95 type="MyApp.Events.ItemCreated", # Required: event type96 source="/myapp/items", # Required: event source97 data={"key": "value"}, # Event payload98 subject="items/123", # Optional: subject/path99 datacontenttype="application/json", # Optional: content type100 dataschema="https://schema.example", # Optional: schema URL101 time=datetime.now(timezone.utc), # Optional: timestamp102 extensions={"custom": "value"} # Optional: custom attributes103)104```105106### EventGridEvent Properties107108```python109event = EventGridEvent(110 subject="/myapp/items/123", # Required: subject111 event_type="MyApp.ItemCreated", # Required: event type112 data={"key": "value"}, # Required: event payload113 data_version="1.0", # Required: schema version114 topic="/subscriptions/.../topics/...", # Optional: auto-set115 event_time=datetime.now(timezone.utc) # Optional: timestamp116)117```118119## Async Client120121```python122from azure.eventgrid.aio import EventGridPublisherClient123from azure.identity.aio import DefaultAzureCredential124125async def publish_events():126 credential = DefaultAzureCredential()127128 async with EventGridPublisherClient(endpoint, credential) as client:129 event = CloudEvent(130 type="MyApp.Events.Test",131 source="/myapp",132 data={"message": "hello"}133 )134 await client.send(event)135136import asyncio137asyncio.run(publish_events())138```139140## Namespace Topics (Event Grid Namespaces)141142For Event Grid Namespaces (pull delivery):143144```python145from azure.eventgrid.aio import EventGridPublisherClient146147# Namespace endpoint (different from custom topic)148namespace_endpoint = "https://<namespace>.<region>.eventgrid.azure.net"149topic_name = "my-topic"150151async with EventGridPublisherClient(152 endpoint=namespace_endpoint,153 credential=DefaultAzureCredential()154) as client:155 await client.send(156 event,157 namespace_topic=topic_name158 )159```160161## Best Practices1621631. **Use CloudEvents** for new applications (industry standard)1642. **Batch events** when publishing multiple events1653. **Include meaningful subjects** for filtering1664. **Use async client** for high-throughput scenarios1675. **Handle retries** — Event Grid has built-in retry1686. **Set appropriate event types** for routing and filtering169
Full transparency — inspect the skill content before installing.