Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
APPLIES TO: All API Management tiers
This article shows you how to create and use policy fragments in your API Management policy definitions. Policy fragments are centrally managed, reusable XML snippets containing one or more API Management policy configurations.
Policy fragments help you configure policies consistently and maintain policy definitions without needing to repeat or retype XML code.
A policy fragment:
Limitations:
<inbound>
, <outbound>
, etc.) or the <base/>
element.If you don't already have an API Management instance and a backend API, see:
While not required, you may want to configure one or more policy definitions. You can copy policy elements from these definitions when creating policy fragments.
In the Azure portal, search for and select API Management services.
On the API Management services page, select your API Management instance.
In the left navigation of your API Management instance, under APIs, select Policy fragments > + Create.
In the Create a new policy fragment window, enter a Name and an optional Description of the policy fragment. The name must be unique within your API Management instance.
Example name: ForwardContext
In the XML policy fragment editor, type or paste one or more policy XML elements between the <fragment>
and </fragment>
tags.
For example, the following fragment contains a set-header
policy configuration to forward context information to a backend service. This fragment would be included in an inbound policy section. The policy expressions in this example access the built-in context
variable.
<fragment>
<set-header name="x-request-context-data" exists-action="override">
<value>@(context.User.Id)</value>
<value>@(context.Deployment.Region)</value>
</set-header>
</fragment>
Select Create. The fragment is added to the list of policy fragments.
Configure the include-fragment
policy to insert a policy fragment in a policy definition. For more information about policy definitions, see Set or edit policies.
For example, insert the policy fragment named ForwardContext in the inbound policy section:
<policies>
<inbound>
<include-fragment fragment-id="ForwardContext" />
<base />
</inbound>
[...]
Tip
To see the content of an included fragment displayed in the policy definition, select Calculate effective policy in the policy editor.
After creating a policy fragment, you can view and update the properties of a policy fragment, or delete the policy fragment at any time.
To view properties of a policy fragment:
To edit a policy fragment:
Note
Update affects all policy definitions where the fragment is included.
To delete a policy fragment:
For more information about working with policies, see:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Improve the performance of an API by adding a caching policy in Azure API Management - Training
Learn how to use Azure API Management to change an API without rewriting code to enable you to apply a caching policy to a GET operation for quicker response.