KristoferA
KristoferA

Reputation: 12397

Custom API key for Azure API management subscription

I'm looking into changing API gateway from another solution to Azure API management. This is for an existing app with millions of existing users / installations on end-user devices that we have no control over.

To keep existing app versions working for users, that means I need to use a custom header for the API key as well as a custom API key value.

I can change the name of the API key header in the API management portal, but I have not found a way to set up a subscription with an existing API key value instead of using a new Azure-generated API key.

Is there a way to do this? We can't force existing users to upgrade the app, and disabling API keys altogether doesn't seem like a good idea either.

Upvotes: 2

Views: 3543

Answers (2)

search-learn
search-learn

Reputation: 1314

For those not using AzureRm (outdated) and instead Az powershell here are the commands

$apimContext = New-AzApiManagementContext -ResourceGroupName "{resource group name}" -ServiceName "{your APIM name}"

Set-AzApiManagementSubscription -Context $apimContext -SubscriptionId -"SubscriptionId" -PrimaryKey "{key1}" -SecondaryKey "{key2}" -State "Active"

To get a list of subscriptions:

Get-AzApiManagementSubscription -Context $apimContext

Upvotes: 2

Allen Wu
Allen Wu

Reputation: 16438

You can set the primary and secondary keys for a subscription by using Set-AzureRmApiManagementSubscription.

$apimContext = New-AzureRmApiManagementContext -ResourceGroupName "{resource group name}" -ServiceName "{your APIM name}"
Set-AzureRmApiManagementSubscription -Context $apimContext -SubscriptionId "{subscription id}" -PrimaryKey "{an existing API key}" -SecondaryKey "{an existing API key}" -State "Active"

You can get the subscription id by using Get-AzureRmApiManagementSubscription at first.

Please note that before you set an existing key value as the primary or secondary key, you should delete it from the original subscription/delete the subscription that has the existing key. In other words, the key is unique, you can't keep the same keys in different subscriptions.

Upvotes: 3

Related Questions