Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,447 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I need to create a Bash script that creates a new subscription key for given product using az cli commands
Try:
#!/bin/bash
# Set your Azure subscription ID
subscription_id="your-subscription-id"
# Set APIM details
resource_group="your-resource-group-name"
service_name="your-apim-service-name"
product_id="your-product-id"
subscription_name="New Subscription"
az login
# Set the subscription
az account set --subscription $subscription_id
# Create the new subscription
new_subscription=$(az apim subscription create \
--resource-group $resource_group \
--service-name $service_name \
--product-id $product_id \
--display-name "$subscription_name" \
--state active)
# Extract and display the subscription key
subscription_key=$(echo $new_subscription | jq -r '.primaryKey')
echo "New subscription created with key: $subscription_key"
You can mark it 'Accept Answer' and 'Upvote' if this helped you
Regards,
Abiola
I have found below script which works for creating and also updating new subscription key.
resourceGroupName="yourResourceGroupName"
serviceName="yourAPIMServiceName"
productId="yourProductId"
subscriptionId="yourSubscriptionId" # The ID you want to assign to the new subscription
subscriptionName="yourSubscriptionName" # The name for the new subscription
# Create the body of the request
body=$(cat <<EOF
{
"properties": {
"scope": "/products/$productId",
"displayName": "$subscriptionName",
"state": "active",
"ownerId": "/subscriptions/$subscriptionId"
}
}
EOF
)
# Make the REST API call to create the subscription and capture the response
response=$(az rest --method put \
--uri "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/$resourceGroupName/providers/Microsoft.ApiManagement/service/$serviceName/subscriptions/$subscriptionId?api-version=2021-04-01-preview" \
--body "$body" \
--headers "Content-Type=application/json")
# Extract the primary key from the response using jq
primaryKey=$(echo $response | jq -r '.properties.primaryKey')
# Output the primary key
echo "Primary Key: $primaryKey"