In my azure devops pipeline i have a task AzureCLI@2 that deploys an api to apim. How can i use the same task using the az apim api to update the api? I know that there is an operation to do this but the documentantion don't show how to and i didn't find any example. I'm using an open api file generated in the pipeline to create and i would like to use it to update the api either.
-
What about this issue, does the answer will help you? If yes, you could [Accept it as an Answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), so it could help other community members who get the same issues and we could archive this thread, thanks. – Felix Apr 27 '21 at 02:18
4 Answers
Just use az apim api import
again. It will replace existing API with new one, preserving policies for operations with the same template. --path
and --api-id
parameters must be the same as existing API. Provide url/path to the swagger/openapi and correct specification format. Example:
az apim api import --path "/myapi" --api-id "myapiid" --resource-group "test-rg" --service-name "test-apim" --specification-url "https://petstore.swagger.io/v2/swagger.json" --specification-format Swagger
If you want to add revision instead of updating existing one, add additional --api-revision
parameter
az apim api update
is suited for updating single properties or fields like description, api type etc.

- 1,222
- 1
- 9
- 25
-
Are you sure that az apim api import update existing one ? I am trying but it is not reflecting. – dotnetstep Oct 01 '21 at 17:33
-
-
-
I'm already creating the API with the method IMPORT that is the right to create an api with the open api specification. I can't DELETE the API to recreate it after. I want to use [this](https://learn.microsoft.com/en-us/cli/azure/apim/api?view=azure-cli-latest#az_apim_api_update) method to update the API.with the new open api specification generated by my pipeline but i dont't know how. – leafar29 Apr 23 '21 at 16:53
Please use the below script to create/update API in APIM using az cli. I am assuming
A. Your api definition is in Open API definition file. B. You are identifying apim endpoints using path ($apiPrefix).
$apiPrefix="my-path"
$rgName="RG_NAME"
$apimS="APIM_SVC_NAME"
$apiDefinitionLocalPath="PATH_TO_APIM_DEFINITION_JSON"
$allAPIs= az apim api list --resource-group $rgName --service-name $apimS
$allAPIsJson= $allAPIs | ConvertFrom-Json
$apiWithMatchingPath=$allAPIsJson | Where-Object {$_.path -eq $apiPrefix }
if($apiWithMatchingPath){
#it only requires the ID. using $apiWithMatchingPath.id will return the
#fully qualified ID... which is not required.
az apim api import --api-id $apiWithMatchingPath.name --resource-group `
$rgName --service-name $apimS --path $apiPrefix --api-type http --protocols`
https --specification-format OpenApi --specification-path `
$apiDefinitionLocalPath
}else{
#IMPORT definition w/o name
az apim api import --resource-group $rgName --service-name $apimS --path `
$apiPrefix --api-type http --protocols https --specification-format `
OpenApi --specification-path $apiDefinitionLocalPath
}
In the azure devops pipeline, we can set the AzureCLI task like this:
We need to go to the azure portal and find our api ID, and then update the api info. Here is my test result:

- 1,104
- 3
- 6