Policies in Azure API Management
In Azure API Management (APIM), policies are a powerful capability of the system that allow the publisher to change the behavior of the API through configuration. Policies are a collection of Statements that are executed sequentially on the request or response of an API. Popular Statements include format conversion from XML to JSON and call rate limiting to restrict the amount of incoming calls from a developer. Many more policies are available out of the box.
Policies are applied inside the gateway which sits between the API consumer and the managed API. The gateway receives all requests and usually forwards them unaltered to the underlying API. However a policy can apply changes to both the inbound request and outbound response.
Policy expressions can be used as attribute values or text values in any of the API Management policies, unless the policy specifies otherwise. Some policies such as the Control flow and Set variable policies are based on policy expressions. For more information, see Advanced policies and Policy expressions.
The policy definition is a simple XML document that describes a sequence of inbound and outbound statements. The XML can be edited directly in the definition window. A list of statements is provided to the right and statements applicable to the current scope are enabled and highlighted.
Clicking an enabled statement will add the appropriate XML at the location of the cursor in the definition view.
If the policy that you want to add is not enabled, ensure that you are in the correct scope for that policy. Each policy statement is designed for use in certain scopes and policy sections. To review the policy sections and scopes for a policy, check the Usage section for that policy in the Policy Reference.
The configuration is divided into
on-error. The series of specified policy statements is executes in order for a request and a response.
<policies> <inbound> <!-- statements to be applied to the request go here --> </inbound> <backend> <!-- statements to be applied before the request is forwarded to the backend service go here --> </backend> <outbound> <!-- statements to be applied to the response go here --> </outbound> <on-error> <!-- statements to be applied if there is an error condition go here --> </on-error> </policies>
If there is an error during the processing of a request, any remaining steps in the
outbound sections are skipped and execution jumps to the statements in the
on-error section. By placing policy statements in the
on-error section you can review the error by using the
context.LastError property, inspect and customize the error response using the
set-body policy, and configure what happens if an error occurs. There are error codes for built-in steps and for errors that may occur during the processing of policy statements. For more information, see Error handling in API Management policies.
For information on how to configure policies, see Set or edit policies.
See the Policy reference for a full list of policy statements and their settings.
See Policy samples for more code examples.
Apply policies specified at different scopes
If you have a policy at the global level and a policy configured for an API, then whenever that particular API is used both policies will be applied. API Management allows for deterministic ordering of combined policy statements via the base element.
<policies> <inbound> <cross-domain /> <base /> <find-and-replace from="xyz" to="abc" /> </inbound> </policies>
In the example policy definition above, the
cross-domain statement would execute before any higher policies which would in turn, be followed by the
Restrict incoming requests
To add a new statement to restrict incoming requests to specified IP addresses, place the cursor just inside the content of the
inbound XML element and click the Restrict caller IPs statement.
This will add an XML snippet to the
inbound element that provides guidance on how to configure the statement.
<ip-filter action="allow | forbid"> <address>address</address> <address-range from="address" to="address"/> </ip-filter>
To limit inbound requests and accept only those from an IP address of 22.214.171.124 modify the XML as follows:
<ip-filter action="allow"> <address>126.96.36.199</address> </ip-filter>
For more information working with policies, see: