How to get a list of subscription IDs of APIM and their corresponding primary keys by az CLI?

Jacky Lam 210 Reputation points
2023-06-19T02:11:17.5566667+00:00

Hi,

I need a way to retrieve Azure APIM's subscription id lists and their corresponding primary keys using az CLI. Have do some research and seems az apim currently not suppoting this. But I am not using Windows so powershell is not an option. Any other alternative?

Jacky

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

Accepted answer
  1. navba-MSFT 27,540 Reputation points Microsoft Employee Moderator
    2023-06-19T03:04:16.01+00:00

    @Jacky Lam Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    I understand that you are looking for the Azure CLI command to fetch the APIM subscription and list the secret keys. Currently there is no AzCLI command to achieve this ask.

    However, You can leverage the Azure CLI rest command to achieve your requirement.
    https://learn.microsoft.com/en-us/cli/azure/reference-index?view=azure-cli-latest#az-rest

    I am sharing the below CLI commands which I executed at my end and its corresponding output, for your reference.

    AzCLI command to list the APIM subscription:

    az rest --uri /subscriptions/{SubscriptionID}/resourceGroups/{RGName}/providers/Microsoft.ApiManagement/service/{APIMName}/subscriptions?api-version=2022-08-01

    Output:

    {
      "count": 4,
      "value": [
        {
          "id": "/subscriptions/XXXXXXXX/resourceGroups/RGName/providers/Microsoft.ApiManagement/service/APIMName/subscriptions/6487fd2f0aeeed005f070001",
          "name": "6487fd2f0aeeed005f070001",
          "properties": {
            "allowTracing": false,
            "createdDate": "2023-06-13T05:22:55.6Z",
            "displayName": null,
            "endDate": null,
            "expirationDate": null,
            "notificationDate": null,
            "ownerId": "/subscriptions/XXXXXXX/resourceGroups/RGName/providers/Microsoft.ApiManagement/service/APIMName/users/1",
            "scope": "/subscriptions/XXXXXXXX/resourceGroups/RGName/providers/Microsoft.ApiManagement/service/APIMName/products/starter",
            "startDate": null,
            "state": "active",
            "stateComment": null
          },
          "type": "Microsoft.ApiManagement/service/subscriptions"
        },
    

    AzCLI command to fetch the subscription secrets:

    az rest --method post --uri /subscriptions/{SubscriptionID}/resourceGroups/{RGName}/providers/Microsoft.ApiManagement/service/{APIMName}/subscriptions/{APIMSubscriptionName}/listSecrets?api-version=2022-08-01

    Output:

    {
      "primaryKey": "8a489cdd18af438586c5cffc2cfacca3",
      "secondaryKey": "9eeb969c8a9847f3bad284da4a290938"
    }
    

    Note: You need to update the {placeholders} in the above command, like Subscription ID, APIM Name, Resource Group Name and the APIM Subscription Name.

    Please let me know if you face any challenges while running the above command or have any follow-up queries. I would be happy to assist.

    Sharing some related links for your reference.

    https://learn.microsoft.com/en-us/rest/api/apimanagement/current-ga/subscription/list?tabs=HTTP
    https://learn.microsoft.com/en-us/rest/api/apimanagement/current-ga/subscription/list-secrets?tabs=HTTP https://stackoverflow.com/questions/69445513/fetch-azure-api-management-subscription-key-using-azure-cli

    Hope this helps

    **

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.