Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How can we apply fault string for Quota policy in apigee.

I configured quota policy by including custom attributes from application(Interval,TimeUnit,count) and fetched those values from the application.Now I am trying to configure fault string to specify the customized error information to the consumer for the quota policy.So,that I am want to notify the consumer with the present "counter (request count) status in the response at the time of violating the quota policy want to see the request count as a response to the consumer.

Solved Solved
1 12 844
1 ACCEPTED SOLUTION

Yes, this is a good pattern.

It is not possible to do it solely with a Quota policy. As you have probably seen there is no configuration option in the Quota policy that allows you to set the message that gets returned to the caller.

BUT, there is a general purpose way to do this within Apiege Edge: Fault Rules. Handling faults with Fault Rules is described in detail in the Apigee docs. But the short story is this: when a fault occurs for any reason, the "logic flow" within Apigee Edge transfers to Fault Rules. When a Quota policy rejects a call, it throws a fault, which means you can specify a Fault Rule that sets the appropriate response including payload and headers sent back to the caller.

For example you might want to respond with an HTTP 429 status, and a payload indicating "quota exceeded", and a set of headers that indicate the available quota, the call count available, and when the quota will reset.

The fault rules section in your proxyendpoint might look like this:

  <FaultRules>
    <FaultRule name="quota">
      <Condition>(fault.name = "QuotaViolation")</Condition>
      <Step><Name>AM-QuotaViolationMessage</Name></Step>
    </FaultRule>
  </FaultRules>

And the AssignMessage policy would look like this:

<AssignMessage name='AM-QuotaViolationMessage'>
  <Description>message for quota exceeded</Description>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <Set>
    <Headers>
      <Header name='X-Quota-Reset'>{ratelimit.Quota-1.expiry.time}</Header>
      <Header name='X-Quota-Allowed'>{ratelimit.Quota-1.allowed.count}</Header>
      <Header name='X-Quota-Available'>{ratelimit.Quota-1.available.count}</Header>
    </Headers>
    <Payload contentType='application/json'>{
  "error" : {
    "message" : "you have exceeded your quota",
    "clientId" : "{request.queryparam.apikey}"
  }
}
</Payload>
    <StatusCode>429</StatusCode>
    <ReasonPhrase>Quota Exceeded</ReasonPhrase>
  </Set>
</AssignMessage>

The above assumes your Quota policy has a name of "Quota-1".

You may decide to inject those Quota information into every response, in headers. Even responses that do not exceed the quota. That way a client would be able to always track how many calls it has made, when the quota will reset, how many calls are remaining, and so on.

If you did that, then ... the way to do it would be to drop a different AssignMessage policy, in the postflow. Something like this:

<AssignMessage name='AM-QuotaHeaders'>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <Set>
    <Headers>
      <Header name='X-Quota-Reset'>{ratelimit.Quota-1.expiry.time}</Header>
      <Header name='X-Quota-Allowed'>{ratelimit.Quota-1.allowed.count}</Header>
      <Header name='X-Quota-Available'>{ratelimit.Quota-1.available.count}</Header>
    </Headers>
  </Set>
</AssignMessage>

This policy would have the affect of adding headers, but it would not change the response payload or status code.

View solution in original post

12 REPLIES 12
Top Solution Authors