Microsoft Azure API Management Client Library for Python
The Azure API Management Client Library for Python provides programmatic control over Azure API Management (APIM) resources. It enables developers and DevOps engineers to create, update, and manage APIM services, APIs, products, subscriptions, and policies. Currently at version 5.0.0, it is actively maintained as part of the Azure SDK for Python, with a regular release cadence.
Warnings
- breaking Starting June 1, 2024, all Azure API Management service API versions prior to 2021-08-01 are being retired (disabled). Management operations (creation, update, etc.) using older API versions via this SDK will fail. Data plane operations (API gateway) are unaffected. [23]
- breaking Trusted service connectivity in API Management gateway will be retired on March 15, 2026. Review Azure guidance to determine if your API Management service is affected. [12]
- gotcha `DefaultAzureCredential` relies on specific environment variables (`AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_SUBSCRIPTION_ID`) for token authentication. Misconfiguration of these variables is a common source of authorization errors. [1, 2, 7]
- breaking Version 5.0.0 introduced several new operation groups (e.g., `api_gateway`, `api_management_gateway_skus`, `all_policies`). Existing code interacting with these areas in previous versions may require updates to use the new client methods and structures. [1]
Install
-
pip install azure-mgmt-apimanagement azure-identity
Imports
- ApiManagementClient
from azure.mgmt.apimanagement import ApiManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.apimanagement import ApiManagementClient
# Set environment variables or replace with your actual values
# AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET must be set for DefaultAzureCredential
# AZURE_SUBSCRIPTION_ID is also required for the client
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'YOUR_SUBSCRIPTION_ID')
# Authenticate and create API Management client
try:
credential = DefaultAzureCredential()
client = ApiManagementClient(credential, subscription_id)
print(f"Successfully authenticated for subscription: {subscription_id}")
# List all API Management services in the subscription
print("Listing API Management services...")
for service in client.api_management_service.list():
print(f" - Name: {service.name}, Location: {service.location}, SKU: {service.sku.name}")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure AZURE_SUBSCRIPTION_ID and Azure authentication environment variables (AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET) are correctly set.")