Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch Azure API Management subscription key using azure cli

I am trying to get the subscription keys for my products as well as default subscription key using cli. I have gone through the documentation https://learn.microsoft.com/en-us/cli/azure/apim/api?view=azure-cli-latest , but right now I don't see any commands to get me the subscription key.

While I can see there are powershell way of getting it, we run the tasks in ubuntu pipeline, and the commands listed below is not working in the linux agent. It says Set-AzContext is not a known command

$subscriptionId = "id"
$RG = "rg"
$service = "apim-name"

Set-AzContext -Subscription $subscriptionId 

$apimContext = New-AzApiManagementContext -ResourceGroupName $RG -ServiceName $service

Get-AzApiManagementSubscriptionKey -Context $apimContext -SubscriptionId "master"

Update I am able to fetch the details through powershell task for Azure in the DevOps pipeline. If there is no option in azure cli I will use this as a workaround.

like image 948
threeleggedrabbit Avatar asked Aug 01 '26 00:08

threeleggedrabbit


1 Answers

Using az rest it is possible:

APIMID=`az apim show -n apimname -g resourcegroup --query id -o tsv`
az rest --method post  --uri ${APIMID}/subscriptions/test-subscription/listSecrets?api-version=2022-08-01 --query primaryKey -o tsv

where test-subscription is the name of the subscription.

like image 85
Kai Walter Avatar answered Aug 02 '26 19:08

Kai Walter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!