how to incorporate individual policies for different operations inside a single API in an APIM

84017655 71 Reputation points
2025-03-07T06:39:25.87+00:00

I have a task to set individual policies for different operations in an API in API Management Instance. Currently, we are manually adding policies for different operations from the Azure Portal. But my task is to set individual policies for different operations using Bicep.

I searched for a solution and found a document on Stack overflow, but It is not answering my question:

https://stackoverflow.com/questions/75104897/how-do-i-apply-a-policy-from-azure-api-management-service-to-azure-app-service-r

I could not find any solution in the official mslearn documentation.

Kindly propose a solution and confirm whether I have to keep multiple allOperationsPolicy.xml files in my repo to achieve this.

Also, initiate a private conversation where I can share existing bicep for more clarity.

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

Accepted answer
  1. VenkateshDodda-MSFT 25,111 Reputation points Microsoft Employee Moderator
    2025-04-01T18:18:01.4866667+00:00

    @84017655 Thanks for your follow up questions on this.

    As suggested by Sakshi Kokardekar you need to define separate policy files for each operation and reference them in your Bicep template. You will indeed need multiple .xml files in your repo, one for each operation policy. I think, In your Bicep template, you'll specify the apiOperation resource and reference the corresponding policy file

    I want to apply an individual policy to GET /api/v5/auth_code - GET operation. What changes do I need to make in this bicep snippet? Do I need to mention GET /api/v5/auth_code - GET operation name in the name property of Microsoft.ApiManagement/service/apis/operations/policies resource?

    You have to pass the value name property under Microsoft.ApiManagement/service/apis/operations/policies as policy only and here the sample policy resource block which we have used to create to policy for a specific operation under an API.

    resource getresourceechoapipolicy 'Microsoft.ApiManagement/service/apis/operations/policies@2024-06-01-preview' = {
      name: 'policy'
      parent: apiretrieveresource 
    #update the resource block name of API Operation Name
      dependsOn: [
        apiretrieveresource  #update the resource block name of API Operation Name
        apiManagementService #update the resource block name of APIM instance
      ]
      properties: {
        value: loadTextContent('./policydefinition.xml')
        format: 'xml'
      }
    }
    

    Also confirm, do I need to define Microsoft.ApiManagement/service/apis/operations/policies resource again If I want to apply individual policies to POST [/api/v5/appversion - POST] operation of same API in same API Gateway Instance?

    Yes, you have to define multiple resource blocks since you want to add policy at operation level inside an API.

    Hope this helps, let me know if you have any further questions on this.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

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.