Azure API Management SDK for Python
Manage Azure API Management services, APIs, products, and policies.
Installation
pip
install
azure-mgmt-apimanagement
pip
install
azure-identity
Environment Variables
AZURE_SUBSCRIPTION_ID
=
your-subscription-id
Authentication
from
azure
.
identity
import
DefaultAzureCredential
from
azure
.
mgmt
.
apimanagement
import
ApiManagementClient
import
os
client
=
ApiManagementClient
(
credential
=
DefaultAzureCredential
(
)
,
subscription_id
=
os
.
environ
[
"AZURE_SUBSCRIPTION_ID"
]
)
Create APIM Service
from
azure
.
mgmt
.
apimanagement
.
models
import
(
ApiManagementServiceResource
,
ApiManagementServiceSkuProperties
,
SkuType
)
service
=
client
.
api_management_service
.
begin_create_or_update
(
resource_group_name
=
"my-resource-group"
,
service_name
=
"my-apim"
,
parameters
=
ApiManagementServiceResource
(
location
=
"eastus"
,
publisher_email
=
"admin@example.com"
,
publisher_name
=
"My Organization"
,
sku
=
ApiManagementServiceSkuProperties
(
name
=
SkuType
.
DEVELOPER
,
capacity
=
1
)
)
)
.
result
(
)
print
(
f"Created APIM:
{
service
.
name
}
"
)
Import API from OpenAPI
from
azure
.
mgmt
.
apimanagement
.
models
import
(
ApiCreateOrUpdateParameter
,
ContentFormat
,
Protocol
)
api
=
client
.
api
.
begin_create_or_update
(
resource_group_name
=
"my-resource-group"
,
service_name
=
"my-apim"
,
api_id
=
"my-api"
,
parameters
=
ApiCreateOrUpdateParameter
(
display_name
=
"My API"
,
path
=
"myapi"
,
protocols
=
[
Protocol
.
HTTPS
]
,
format
=
ContentFormat
.
OPENAPI_JSON
,
value
=
'{"openapi": "3.0.0", "info": {"title": "My API", "version": "1.0"}, "paths": {"/health": {"get": {"responses": {"200": {"description": "OK"}}}}}}'
)
)
.
result
(
)
print
(
f"Imported API:
{
api
.
display_name
}
"
)
Import API from URL
api
=
client
.
api
.
begin_create_or_update
(
resource_group_name
=
"my-resource-group"
,
service_name
=
"my-apim"
,
api_id
=
"petstore"
,
parameters
=
ApiCreateOrUpdateParameter
(
display_name
=
"Petstore API"
,
path
=
"petstore"
,
protocols
=
[
Protocol
.
HTTPS
]
,
format
=
ContentFormat
.
OPENAPI_LINK
,
value
=
"https://petstore.swagger.io/v2/swagger.json"
)
)
.
result
(
)
List APIs
apis
=
client
.
api
.
list_by_service
(
resource_group_name
=
"my-resource-group"
,
service_name
=
"my-apim"
)
for
api
in
apis
:
print
(
f"
{
api
.
name
}
:
{
api
.
display_name
}
-
{
api
.
path
}
"
)
Create Product
from
azure
.
mgmt
.
apimanagement
.
models
import
ProductContract
product
=
client
.
product
.
create_or_update
(
resource_group_name
=
"my-resource-group"
,
service_name
=
"my-apim"
,
product_id
=
"premium"
,
parameters
=
ProductContract
(
display_name
=
"Premium"
,
description
=
"Premium tier with unlimited access"
,
subscription_required
=
True
,
approval_required
=
False
,
state
=
"published"
)
)
print
(
f"Created product:
{
product
.
display_name
}
"
)
Add API to Product
client
.
product_api
.
create_or_update
(
resource_group_name
=
"my-resource-group"
,
service_name
=
"my-apim"
,
product_id
=
"premium"
,
api_id
=
"my-api"
)
Create Subscription
from
azure
.
mgmt
.
apimanagement
.
models
import
SubscriptionCreateParameters
subscription
=
client
.
subscription
.
create_or_update
(
resource_group_name
=
"my-resource-group"
,
service_name
=
"my-apim"
,
sid
=
"my-subscription"
,
parameters
=
SubscriptionCreateParameters
(
display_name
=
"My Subscription"
,
scope
=
f"/products/premium"
,
state
=
"active"
)
)
print
(
f"Subscription key:
{
subscription
.
primary_key
}
"
)
Set API Policy
from
azure
.
mgmt
.
apimanagement
.
models
import
PolicyContract
policy_xml
=
"""
azure-mgmt-apimanagement-py
安装
npx skills add https://github.com/sickn33/antigravity-awesome-skills --skill azure-mgmt-apimanagement-py