Azure Event Grid Management Client Library

10.4.0 · active · verified Thu Apr 09

The Microsoft Azure Event Grid Management Client Library for Python allows developers to programmatically manage Event Grid resources such as topics, domains, and event subscriptions within Azure. It is part of the Azure SDK for Python (Track 2) and follows a regular release cadence, often aligning with updates to the underlying Azure REST APIs. The current stable version is 10.4.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with Azure using `DefaultAzureCredential` and then list all Event Grid topics within a specified Azure subscription using the `EventGridManagementClient`. Ensure you have the `AZURE_SUBSCRIPTION_ID` environment variable set and have appropriate permissions in Azure.

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.eventgrid import EventGridManagementClient

# Set your Azure Subscription ID as an environment variable or replace directly
# Example: export AZURE_SUBSCRIPTION_ID="<your-subscription-id>"
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "")

if not subscription_id:
    raise ValueError("AZURE_SUBSCRIPTION_ID environment variable not set.")

# Acquire a credential object using DefaultAzureCredential
# This will attempt to authenticate via various methods:
# Environment variables, Managed Identity, Azure CLI, VS Code, etc.
# For local development, ensure you are logged in via Azure CLI (`az login`)
credential = DefaultAzureCredential()

# Create the Event Grid Management Client
client = EventGridManagementClient(credential, subscription_id)

print(f"Listing Event Grid topics in subscription: {subscription_id}")

try:
    # List all Event Grid topics in the subscription
    topics = client.topics.list_by_subscription()
    for topic in topics:
        print(f"- Topic Name: {topic.name}, Location: {topic.location}")
except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure you have 'Contributor' or 'Owner' role on the subscription.")

view raw JSON →