1
votes

I am trying to figure out how to create an API Management resource and an API Management API from a script. I believe there are two options:

.

  1. Use the powershell Management api from AzureRm.ApiManagement

    To do this I believe the two commands to use are:

    New-AzureRmApiManagement

    New-AzureRmApiManagementApi

.

  1. Use the Management REST API

    For this I believe to create the resource you would need to use the first option and then can use the following method with the Invoke-RestMethod powershell command:

    PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}?api-version=2019-01-01

.

I just want to pass the name of the new management api resource and the name of the devlab to create it under and have it created and then create an api underneath it that is not associated to a product (or associated to the unlimited default product...whichever is easier).

Can anyone help with the powershell script to do this?

Thanks.

1

1 Answers

2
votes

When you run command "New-AzureRmApiManagementApi", you just create a api and the api will not be added to a product. You need to run command "Add-AzureRmApiManagementApiToProduct" to add the api to a product. My script is as below

$ResourceGroupName=""
$sku=""
$name="" 
$Location=""                   
$Organization=""
$AdminEmail=""
$apiName=" #the name of the web API."
$url="" #the URL of the web service that exposes the API.
$path="" #the web API path, which is the last part of the API's public URL and corresponds to the Web API URL suffix field in the admin portal.

#login azure
Connect-AzureRmAccount

#create api management instance with required parameters
New-AzureRmApiManagement -ResourceGroupName $ResourceGroupName -Name $name -Sku $sku -Location $Location -Organization $Organization -AdminEmail $AdminEmail

$ApiMgmtContext =New-AzureRmApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $name

#create api
$api = New-AzureRmApiManagementApi -Context $ApiMgmtContext -Name $apiName -ServiceUrl $url -Protocols @("http", "https") -Path $path

#run the command if you do not know product id
Get-AzureRmApiManagementProduct -Context $ApiMgmtContext

#add api to product
Add-AzApiManagementApiToProduct -Context $ApiMgmtContext -ProductId "" -ApiId $api.ApiId

For more details about Azure API management powershell command, please refer to the document