Fetch Azure API Management subscription key using azure cli
Asked Answered
H

2

7

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.

Hialeah answered 5/10, 2021 at 5:59 Comment(0)
I
1

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

Infantilism answered 5/10, 2021 at 20:22 Comment(1)
the link is invalidQuicktempered
L
5

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.

Lapboard answered 23/2, 2022 at 18:14 Comment(1)
In case that someone wants to read more about it you can find the official documentation from Microsoft: Subscription - List Secrets.Around
I
1

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

Infantilism answered 5/10, 2021 at 20:22 Comment(1)
the link is invalidQuicktempered

© 2022 - 2024 — McMap. All rights reserved.