prakashrajansakthivel
prakashrajansakthivel

Reputation: 2032

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.

Upvotes: 7

Views: 3420

Answers (2)

Kai Walter
Kai Walter

Reputation: 4001

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.

Upvotes: 6

ChaitanyaN-MSFT
ChaitanyaN-MSFT

Reputation: 541

Currently it is not possible to fetch subscription key using AZ CLI commands. The PowerShell command used is the correct way to go. Just in case if it helps another way to get the subscription key is by using Management API call

Upvotes: 1

Related Questions