how to create new subscription key for APIM using AZ cli commands

Sobharani Vadireddy 5 Reputation points
2024-08-05T05:13:13.6+00:00

I need to create a Bash script that creates a new subscription key for given product using az cli commands

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,447 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Abiola Akinbade 29,405 Reputation points Volunteer Moderator
    2024-08-05T08:12:27.3233333+00:00

    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


  2. Sobharani Vadireddy 5 Reputation points
    2024-08-08T04:34:51.6233333+00:00

    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"
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.