is there a way to add an API operations to ALL apis ?

Martin Kallukalam 360 Reputation points
2024-11-22T16:01:01.48+00:00

PS: Kindly read the question . A lot of times MS tech who answer the question is too quick to reply without paying attention to the details of the question. Hence the customer have to go back and forth multiple times before getting an appropriate answer

Scenario:
I want to add an operation to ALL APIS.
eg: I want a healthcheck operation to ALL apis in the form of a GET /healthcheck and a mock policy which will return OK.
I do not want to go into each API and add this operation. Instead I want to add this under the "All APIs" section. However when I click "App APIs" , click frontend , openapi editor, it brings up a blank page.

Q: What is the correct way to add an operation which will be attached to ALL Apis?

User's image

User's image

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,216 questions
{count} votes

1 answer

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 16,701 Reputation points
    2024-11-26T18:31:33.8166667+00:00

    Hi @Martin Kallukalam Apologies for the delay in getting back to you. While I could not find a direct way to achieve this use case through the portal, you can certainly use other approaches such as using PowerShell, Azure CLI or a Rest API to add the new healthcheck operation.

    Below is a sample approach I tested using the PowerShell and added the operation on all the available API endpoints.

    $resourceGroupName = "<ResourceGroupName>"
    $serviceName = "<APIMInstanceName>"
    $ApiMgmtContext = New-AzApiManagementContext -ResourceGroupName $resourceGroupName -ServiceName $serviceName
    
    foreach ($api in $apis) {
        $apiId = $api.ApiId
    
        # Define the operation details
        $operationId = "healthcheck"
        $operationName = "Health Check"
        $method = "GET"
        $urlTemplate = "/healthcheck"
        $description = "Health check operation to return a 200 OK response."
    
        # Add the operation to the API
        New-AzApiManagementOperation -Context $apimContext -ApiId $apiId -OperationId $operationId -Name $operationName -Method $method -UrlTemplate $urlTemplate -Description $description
    
    }
    
    
    

    Running the above set of statements added a healthcheck end point to all the APIs in the instance and can be accessed as you need through https://fqdn/apibasepath/heathcheck

    If you prefer to use Azure CLI, you can use the commands az apim api list to get all the APIs in the instance and run the command az apim api operation create against each App Id to create the operation.

    Similarly, if you prefer to use a Rest API end point, you can leverage the end points Api - List By Tags and Api Operation - Create Or Update to create the operation.

    Hope this helps! Please let us know if you have any additional questions or need further assistance.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.