Configuring Quotas

This page describes how to configure quotas for your API in Cloud Endpoints Frameworks. For an overview of the functionality provided by quotas, see About Quotas.

Java

The following procedure assumes that you have already:

To configure quotas on your API:

  1. In the file that contains the API-Scoped Annotations, add the following to your @Api annotation:

      limitDefinitions = {
            @ApiLimitMetric(
              name = "[YOUR-METRIC-NAME]",
              displayName = "[YOUR-METRIC-DISPLAY-NAME]",
              limit = [YOUR-QUOTA-LIMIT])
      }
    
    1. Replace [YOUR-METRIC-NAME] with a name that describes the API requests counter.
    2. Replace [YOUR-METRIC-DISPLAY-NAME] with the text that will be displayed in the Endpoints dashboard on the Quotas page to identify the quota.
    3. Replace [YOUR-QUOTA-LIMIT] with an integer value. This is the number of requests that an application associated with a consumer's Cloud project can make in a minute. For Example:

      @Api(
          name = "echo",
          version = "v1",
          namespace =
            @ApiNamespace(
              ownerDomain = "echo.example.com",
              ownerName = "echo.example.com",
              packagePath = ""
            ),
          limitDefinitions = {
            @ApiLimitMetric(
              name = "read-requests",
              displayName = "Read requests",
              limit = 1000),
            @ApiLimitMetric(
              name = "list-requests",
              displayName = "List requests",
              limit = 100),
            @ApiLimitMetric(
              name = "write-requests",
              displayName = "Write requests",
              limit = 50),
          }
      )
      
  2. For each method that you want to apply a quota to, add the following to the @ApiMethod annotation:

        metricCosts = {
                     @ApiMetricCost(
                       name ="[YOUR-METRIC-NAME]",
                       cost = [YOUR-COST])
        }
    
    1. Replace [YOUR-METRIC-NAME] with a name that you specified in the limitDefinitions parameter in the @Api annotation.
    2. Replace [YOUR-COST] with an integer value that specifies the cost for each request.

    For example:

        @ApiMethod(name = "echo",
                   metricCosts = {
                     @ApiMetricCost(
                       name = "read-requests",
                       cost = 1)
                   })
        public Message echo(Message message, @Named("n") @Nullable Integer n) {
          // ...function code here...
        }
    
  3. Deploy the API.

See the following for more information on the annotations used in quotas:

Python

The following procedure assumes that you have already:

To configure quotas on your API:

  1. In the file that contains the API decorator, create a list of LimitDefinition instances, like the following:

        quota_limits = [
          endpoints.LimitDefinition(
            "[YOUR-METRIC-NAME]",
            "[YOUR-METRIC-DISPLAY-NAME]",
            limit)
        ]
    
    1. Replace [YOUR-METRIC-NAME] with a name that describes the API requests counter.
    2. Replace [YOUR-METRIC-DISPLAY-NAME] with the text that will be displayed in the Endpoints dashboard on the Quotas page to identify the quota.
    3. Replace limit with an integer value. This is the number of requests that an application associated with a consumer's Cloud project can make in a minute. For example:

      quota_limits = [
        endpoints.LimitDefinition('read-requests', 'Read Requests', 1000),
        endpoints.LimitDefinition('list-requests', 'List Requests', 100),
        endpoints.LimitDefinition('write-requests', 'Write Requests', 50),
      ]
      
  2. Add your quota to the API decorator by assigning it to the limit_definitions argument. For example:

        @endpoints.api(name='bookstore',
                       version='v1',
                       limit_definitions=quota_limits)
    
  3. For each method that you want to apply a quota to, assign a dictionary to the [metric_costs] argument of the method decorator. The key must be a name that you specified in the limit_definitions argument to the API decorator, and the value is an integer that specifies the cost for each request. For example:

        @endpoints.method(path='shelves/{shelf}',
                          http_method='GET',
                          metric_costs={'read-requests': 1})
        def get_shelf(self, request):
        # ...function code here...
    
  4. Deploy the API.

See the following for more information on the decorators used in quotas:

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Endpoints Frameworks for App Engine