Microsoft's Azure API Management allows developers to "publish APIs to external, partner, and employee developers securely and at scale." To manage these APIs, you could watch the tutorials on navigating the console and learning where things are, or you could use the command line. As with nearly every Azure service, Microsoft provides an API for API Management, and the Azure Resource Manager (AzureRM) PowerShell commands rely on this API.

Getting started

Before we can do anything with Azure in PowerShell, we need to get the Azure PowerShell module. Be sure to download the latest one from the PowerShell Gallery before getting started using Install-Module -Name AzureRm; I will be using v3.6.0.

Once we've got the module downloaded and installed, we'll then have to authenticate to our subscription. We'll do this by running Add-AzureRmAccount and providing the subscription credentials. However, if you're using a certificate or a service principal name (SPN) to authenticate, those methods work just fine as well.

I'm going to assume that you already have an API Management instance setup to hold your APIs. If not, Microsoft provides a tutorial on how to set one up. You will also need to have an Azure app service created as a destination to send the API requests to.

Discovering the API Cmdlets

Now that we're authenticated to Azure and are ready to go, let's see what kind of cmdlets we have available for working with Azure API Management. To figure out what cmdlets we have, I'll use the Get-Command cmdlet to see the modules installed along with the AzureRm module.

I know from experience there are lots of dependent modules that come along. Chances are, the commands for managing API Management APIs are in one of those modules. By running Get-Module -Name *API* -ListAvailable, I can see that I now have a module called AzureRM.APIManagement. I'll now use Get-Command again and see all the cmdlets in there.

AzureRm cmdlets

AzureRm cmdlets

I can see right away there are lots of commands that aren't necessarily tied to managing the APIs like we're interested in. Let's scope that down a bit by running Get-Command -Module AzureRM.ApiManagement -Name *AzureRmApiManagementApi* to get only the cmdlets that work with the APIs.

Creating an Azure API Management API

Let's first see what it takes to build one of these APIs. One of the first things you might find odd is the use of the Azure context. You need a context to work with any of the Azure API management commands. Create a context with New-AzureRmApiManagementContext. This object represents the API Management Service you've already created.

$azrContext = New-AzureRmApiManagementContext -ResourceGroupName APIMANAGEMENTSERVICERGHERE -ServiceName APIMANAGEMENTSERVICENAMEHERE

After saving the context, you'll use this in every one of your calls to Azure. Now we need to run New-AzureRmApiManagementApi to create the API. Since this command requires a few parameters, I'll save some space by using parameter splatting.

$newApiParams = @{
Context = $azrContext
Name = 'Testing123'
ServiceUrl = ''
Path = '/api/partner/foo'
Protocols = 'https'

New-AzureApiManagementApi @newApiParams

If all goes well, the command will return an object representing your newly created API.

Created API

Created API

We can now see the API by running Get-AzureRmApiManagementApi -Context $azrContext. This should return the same object returned during creation.

Modifying existing APIs

Now that I have created an API, I see I forgot to add a description. I can change existing APIs with the Set-AzureRmApiManagementApi cmdlet. Using Set-AzureRmApiManagementApi requires knowing a few attributes of the API I wish to modify. To change only the attribute we're looking for (Description), I'll capture the existing API and use those attributes with Set-AzureRmApiManagementApi.

The requirement to provide the other properties is a noticeably convoluted way to make changes. But unless Microsoft changes this cmdlet behavior, we can't do much about it.

PowerShell $api = Get-AzureRmApiManagementApi -Context $azrContext -Name Testing123
setParams = @{
Description = 'newdescription'     
Context = $azrContext
Name = $api.Name
ApiId = $api.ApiId    
ServiceUrl = $api.ServiceUrl
Protocols = $api.Protocols} 
Set-AzureRmApiManagementApi @setParams

Removing existing APIs

Now that we've created an API and have modified it, we'll now remove it. Since we've already got our API saved to the $api variable, we can reference its ApiId property to pass it then to Remove-AzureRmManagementApi. This removes the API just as quickly as we created it.

Subscribe to 4sysops newsletter!

Remove-AzureRmApiManagementApi -Context $azrContext -ApiId $api.ApiId
  1. Avatar
    shinoy 6 years ago

    Thank you very much for this post. Do you have any idea how we can do the same thing using C#?

  2. Avatar
    Ericgol 6 years ago

    @shinoy : Use Powershell’s amazing -Debug flag. It will show you the underlying RESTful calls you make to the ARM APIs to create an API.  (Ironic, isn’t it?)  In C# you will need to obtain an oAuth token, set your subscription headers, etc. and do the restful call using a typical web client. Should be fairly easy to do if you have the right perms.

Leave a reply

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2023


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account