jaxkodex
jaxkodex

Reputation: 504

How to disable subscription key in Azure API Managment with Powershell

We are using Azure API Management and Powershell to import the configuration on the portal with a script, however whenever the API is imported the "Subscription required" check from the portal is enabled (The API required Subscription Key). We are not using the subscription feature by now on the API so we need to disable this when imported. We are using Import-AzApiManagementApi and Set-AzApiManagementApi with the following code:

Set-AzApiManagementApi -ApiId $apiId -Context $context -Protocols @('https') -ServiceUrl $serviceBase$path -Name $api.Name
Set-AzApiManagementPolicy -Context $context -ApiId $apiId -PolicyFilePath "$pwd/src/private/security_policy.xml"

We haven't found in the documentation the way to import the API without this check. Is there any script to disable this feature via powershell?

Upvotes: 2

Views: 3244

Answers (3)

Andrew Tregonning
Andrew Tregonning

Reputation: 71

I was unable to change SubscriptionRequired to false on an imported API using Set-AzApiManagementApi as suggested by https://stackoverflow.com/a/56095090/240586. As SubscriptionRequired is a switch parameter I couldn't specify it as false.

A variation of https://stackoverflow.com/a/66675277/240586 worked, however. @RomericRobo's answer is missing the final Set-AzApiManagementApi command to save the change (sorry, I'm unable to edit or comment on his answer). My complete working solution is:

# get context
$apimContext = New-AzApiManagementContext -ResourceGroupName "targetResourceGroup" -ServiceName "targetApimInstance"
$api = Get-AzApiManagementApi -Context $apimContext -ApiId "api-id"

# set subscriptionRequired to false
$api.SubscriptionRequired=$false
Set-AzApiManagementApi -InputObject $api

Upvotes: 2

RomericRobo
RomericRobo

Reputation: 11

Quick addition to this. The following Powershell command will set an API's Subscription Required property to false. A word of warning though: this currently does not work with Azure DevOps pipelines. The pipeline will claim the task ran successfully, and if you run an echo of $api in the pipeline it will show that the api has SubscriptionRequired : False. However, if you go to check the api in Azure, it will still be set to True (the default behavior).

# get context
$apimContext = New-AzApiManagementContext -ResourceGroupName "targetResourceGroup" -ServiceName "targetApimInstance"
$api = Get-AzApiManagementApi -Context $apimContext -Name "Name of Api"

# set subscriptionRequired to false
$api.SubscriptionRequired=$false

Upvotes: 1

mohaidar
mohaidar

Reputation: 4251

You can either use the Arm module command Set-AzureRmApiManagementProduct or the Az module command Set-AzApiManagementProduct to disable the required subscription option

Check the below documents

https://github.com/Azure/azure-powershell/blob/master/src/ApiManagement/ApiManagement/help/Set-AzApiManagementProduct.md

https://learn.microsoft.com/en-us/powershell/module/azurerm.apimanagement/Set-AzureRmApiManagementProduct?view=azurermps-6.13.0

Update

I can see that the set-azapimanagementapi supports the SubscriptionRequired parameter now

https://learn.microsoft.com/en-us/powershell/module/az.apimanagement/set-azapimanagementapi?view=azps-2.4.0#description

Set-AzApiManagementApi
   -Context <PsApiManagementContext>
   -ApiId <String>
   [-Name <String>]
   [-Description <String>]
   [-ServiceUrl <String>]
   [-Path <String>]
   [-Protocols <PsApiManagementSchema[]>]
   [-AuthorizationServerId <String>]
   [-AuthorizationScope <String>]
   [-OpenIdProviderId <String>]
   [-BearerTokenSendingMethod <String[]>]
   [-SubscriptionKeyHeaderName <String>]
   [-SubscriptionKeyQueryParamName <String>]
   [-SubscriptionRequired]
   [-PassThru]
   [-DefaultProfile <IAzureContextContainer>]
   [<CommonParameters>]

Upvotes: 0

Related Questions