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/events
EVENTGRID_NAMESPACE_ENDPOINT=https://<namespace>.<region>.eventgrid.azure.net
Authentication
from azure.identity import DefaultAzureCredential
from azure.eventgrid import EventGridPublisherClient
credential = 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 EventGridPublisherClient, CloudEvent
from azure.identity import DefaultAzureCredential
client = EventGridPublisherClient(endpoint, DefaultAzureCredential())
# Single event
event = CloudEvent(
type="MyApp.Events.OrderCreated",
source="/myapp/orders",
data={"order_id": "12345", "amount": 99.99}
)
client.send(event)
# Multiple events
events = [
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 EventGridEvent
from datetime import datetime, timezone
event = 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 type
source="/myapp/items", # Required: event source
data={"key": "value"}, # Event payload
subject="items/123", # Optional: subject/path
datacontenttype="application/json", # Optional: content type
dataschema="https://schema.example", # Optional: schema URL
time=datetime.now(timezone.utc), # Optional: timestamp
extensions={"custom": "value"} # Optional: custom attributes
)
EventGridEvent Properties
event = EventGridEvent(
subject="/myapp/items/123", # Required: subject
event_type="MyApp.ItemCreated", # Required: event type
data={"key": "value"}, # Required: event payload
data_version="1.0", # Required: schema version
topic="/subscriptions/.../topics/...", # Optional: auto-set
event_time=datetime.now(timezone.utc) # Optional: timestamp
)
Async Client
from azure.eventgrid.aio import EventGridPublisherClient
from azure.identity.aio import DefaultAzureCredential
async def publish_events():
credential = DefaultAzureCredential()
async with EventGridPublisherClient(endpoint, credential) as client:
event = CloudEvent(
type="MyApp.Events.Test",
source="/myapp",
data={"message": "hello"}
)
await client.send(event)
import asyncio
asyncio.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=DefaultAzureCredential()
) as client:
await client.send(
event,
namespace_topic=topic_name
)
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
Skill Information
- Source
- Microsoft
- Category
- Cloud & Azure
- Repository
- View on GitHub
Related Skills
agent-framework-azure-ai-py
Build Azure AI Foundry agents using the Microsoft Agent Framework Python SDK (agent-framework-azure-ai). Use when creating persistent agents with AzureAIAgentsProvider, using hosted tools (code interpreter, file search, web search), integrating MCP servers, managing conversation threads, or implementing streaming responses. Covers function tools, structured outputs, and multi-tool agents.
Microsoftazd-deployment
Deploy containerized applications to Azure Container Apps using Azure Developer CLI (azd). Use when setting up azd projects, writing azure.yaml configuration, creating Bicep infrastructure for Container Apps, configuring remote builds with ACR, implementing idempotent deployments, managing environment variables across local/.azure/Bicep, or troubleshooting azd up failures. Triggers on requests for azd configuration, Container Apps deployment, multi-service deployments, and infrastructure-as-code with Bicep.
Microsoftazure-ai-agents-persistent-dotnet
Azure AI Agents Persistent SDK for .NET. Low-level SDK for creating and managing AI agents with threads, messages, runs, and tools. Use for agent CRUD, conversation threads, streaming responses, function calling, file search, and code interpreter. Triggers: "PersistentAgentsClient", "persistent agents", "agent threads", "agent runs", "streaming agents", "function calling agents .NET".
Microsoftazure-ai-agents-persistent-java
Azure AI Agents Persistent SDK for Java. Low-level SDK for creating and managing AI agents with threads, messages, runs, and tools. Triggers: "PersistentAgentsClient", "persistent agents java", "agent threads java", "agent runs java", "streaming agents java".
Microsoftazure-ai-anomalydetector-java
Build anomaly detection applications with Azure AI Anomaly Detector SDK for Java. Use when implementing univariate/multivariate anomaly detection, time-series analysis, or AI-powered monitoring.
Microsoft