@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.