Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Quick Start: Add Quota

WWitman edited this page Sep 10, 2014 · 24 revisions

Adding a Quota using Apigee-127

An API quota specifies the number of request messages that an app is allowed to submit to an API over the course of a time unit of hours, minutes, or days.

Volos.js provides the middleware required to add a quota to your Apigee-127 API.

Adding a Quota using Volos.js is as simple as adding a few lines of YAML in your Swagger document!

  • First, update your package.json file in your project to include the volos-quota-memory": module under
        "dependencies": {
             "express": "",
             "a127-magic": "",
             "volos-quota-memory": ""
        }

Note: You can also use a quota implementation that uses Redis or Apigee as its provider. More information here.

  • Run, $ npm install.

  • Execute $ a127 project edit to open up the Swagger editor.

  • In the Swagger configuration file, under x-volos-resources, add the following to enable an in-memory quota provider named 'quota' that you refer to later:

        quota:     
            provider: volos-quota-memory
            options:
                timeUnit: minute
                interval: 1
                allow: 2 
  • Finally, use x-volos-apply to apply this policy to the paths you want to enforce quotas on, referencing the resource you defined in the previous step:
        paths:     
            /hello:
                x-swagger-router-controller: hello_world
                x-volos-authorizations: {}
                x-volos-apply:
                    quota: {}

Now, try running the app as in the quickstart and try using the example curl command thrice:

        $ a127 project start
        $ curl http://localhost:10010/hello\?name\=Me 
        $ curl http://localhost:10010/hello\?name\=Me 
        $ curl http://localhost:10010/hello\?name\=Me 
          Error: exceeded quota<br> &nbsp; &nbsp;at...

The quota policy will only allow the first two requests per minute. From the third request onwards, you will get an error saying that the quota was exceeded.

Using a helper function for customized quotas

The example above shows how you can enforce a global quota for your API - but what if you want per-Developer, per-App or per-Token Quotas? We've got you covered! You can also use a Helper function to customize how a Quota is caluculated.

Here is an example:

paths:
    /weather_quota:
        x-swagger-router-controller: weather
        x-volos-apply:
          quota: 
            key:
              helper: volos
              function: quotaHelper
        get:
          ...

Using this annotation instructs Volos.js to call the quotaHelper function of the helper module volos which it will look for in api/helpers by default. Here is an example implementation that uses the client_id query parameter to evaluate and enforce the Quota:

module.exports = {
  quotaHelper: quotaHelper
};

function quotaHelper(req) {
  var city = req.swagger.params.client_id.value;
  console.log('Quota Key: '+key)
  return key;
}

This can be customized to do whatever logic you wish to maintain your quota, but don't add too much complexity or your latency may go way up!

You can also add caching, OAuth, analytics, and deploy to Apigee.

Clone this wiki locally