Adding policy-based capabilities to an API proxy is a two-step process:
The diagram below shows the relationship between policies and flows. As you can see, a policy is attached to a flow as a processing step. To configure the desired behavior for your API, you need to understand a little bit about flows. (This topic was covered earlier in Configuring flows.)
One type of policy that is commonly used is the SpikeArrest policy. SpikeArrest prevents sudden increases in message traffic that might swamp your backend services.
To attach a policy to a flow:
In the Create policy dialog, click in the Select policy type field and scroll down to Traffic Management and select SpikeArrest.
Optionally, you can change the Display name and Name of the policy.
When you are done, click Create to create the policy.
Click Add to attach the policy.
The Spike Arrest policy is now displayed in the Response pane:
The following XML code for the proxy is displayed in the text editor:
<ProxyEndpointname="default"> <PreFlowname="PreFlow"> <Request/> <Response> <Step>< <Name>SA-</Name> </Step> </Response> </PreFlow> <Flows/> <PostFlowname="PostFlow"> <Request/> <Response/> </PostFlow> <HTTPProxyConnection> <BasePath>/myproxy</BasePath> </HTTPProxyConnection> <RouteRulename="default"> <TargetEndpoint>default</TargetEndpoint> </RouteRule> </ProxyEndpoint>
Todetachapolicyfromaflow,clickthethree-dotmenunexttotheflowandselectDeletepolicystep
Toattachapolicytoaflow:
DisplayName:Uniquedisplaynameforthepolicy.TheUIwillgenerateadefaultname,butitisadvisabletocreateadescriptivenameforthepolicy.Thiswillensurethatotherdevelopersinyourorganizationhaveaneasytimeunderstandingwhatthepolicyisintendedtodo.
Name:Uniquenameforthepolicy.Thepolicynamematchesthedisplaynamewiththefollowingexceptions:
Spacesarereplacedbydashes.
Consecutivedashesarereplacedbyasingledash.
Exceptfordashes,underscores,andspaces(whicharereplaced,asnotedabove),charactersthatarenotalphanumeric,suchaspoundsigns,percentsigns,ampersands,andsoon,areremoved.
Thepolicyisattachedtotheselectedflow.
Afterthepolicyisattached,you’llseethepolicydisplayedintheDesignerviewfortheflow,inthisexamplethePreFlowofthedefaultproxyendpoint,andinthePolicieslistintheNavigationpane.TheCodeview,whichdisplaystheXMLforthenewlyattachedpolicy,displaysbelowtheDesignerview.Apigeegeneratesaninstanceofthepolicythatcontainsasetofreasonabledefaultvalues.
Toaddapolicywithoutattachingittoaflow,whichisusefulforHandlingfaults,click+inthePoliciesbarintheNavigatorviewtoopentheAddPolicydialogandaddthepolicy(asdescribedinsteps4through6intheprevioussection).
ApolicythatisnotattachedtoanyflowisflaggedwiththedetachediconinthePolicieslist,asshownadjacenttotheAPIkeymessagepolicyshowninthepreviousfigure.
Afteryoucreatethepolicy,youcanattachittoaflowbydragginganddroppingitfromthePolicieslisttothedesiredflowintheDesignerview.
TodetachapolicyfromaFlow,selecttheFlow.MouseovertheiconforthepolicyintheDesignerviewoftheFlow.ClicktheXinthecirclethatappearsintheicon.
Todeleteapolicyinstance,mouseovertheentryforthepolicyintheNavigatorview.ClicktheXinthecirclethatappearstotherightoftheentry.
WhentheUIgeneratesapolicyinstance,itappliesreasonabledefaultvaluesforcommonsettings.Youmayneedtomodifythesesettingstomeetyourrequirements.
Forexample:<?xmlversion="1.0"encoding="UTF-8"standalone="yes"?> <SpikeArrestasync="false"continueOnError="false"enabled="true"name="spikearrest-1"> <DisplayName>SpikeArrest-1</DisplayName> <FaultRules/> <Properties/> <Identifierref="request.header.some-header-name"/> <MessageWeightref="request.header.weight"/> <Rate>30ps</Rate> </SpikeArrest>
You can configure a policy by directly editing its XML configuration in the Code view. For example, the peak message rate for the Spike Arrest policy is initially set to 30 messages per second. You can change the peak rate by changing the <Rate
> element value in the XML for the policy. For further details about configuring policies, see the Policy reference.
You can also cut-and-paste policies into the Code view. This is a great way to re-use policies from the samples available on GitHub.
When you make changes to a policy definition in the Code view, the changes are reflected in the Property Inspector. The reverse is also true — make changes in the Property Inspector and they appear in the XML in the Code view.
For documentation specific to the Quota policy used in this topic, see Quota policy.
If you have Gemini Code Assist, you can use it to understand policies and policy elements directly in the Apigee UI in Cloud console:
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-04-24 UTC.