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:
- Written and annotated the code for your API.
- Added API management.
- Deployed your API.
- Configured your API to use an API key. This is needed so that Endpoints Frameworks can identify the Google Cloud project that the calling application is associated with. See Sharing APIs protected by API key for more information.
To configure quotas on your API:
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) }
- Replace
YOUR_METRIC_NAME
with a name that describes the API requests counter. - Replace
YOUR_METRIC_DISPLAY_NAME
with the text that is displayed on Endpoints > Services > Quotas page to identify the quota. Replace
YOUR_QUOTA_LIMIT
with an integer value. This is the number of requests that an application associated with a consumer's Google 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), } )
- Replace
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) }
- Replace
YOUR_METRIC_NAME
with a name that you specified in thelimitDefinitions
parameter in the@Api
annotation. 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... }
- Replace
See the following for more information on the annotations used in quotas:
Python
The following procedure assumes that you have already:
- Installed version 2.4.5 or later of the Endpoints Frameworks library.
- Created your API.
- Created a web server.
- Added API management.
- Deployed your API.
- Configured your API to use an API key. This is needed so that Endpoints Frameworks can identify the Google Cloud project that the calling application is associated with. See Sharing APIs protected by API key for more information.
To configure quotas on your API:
In the file that contains the API decorator, create a list of
LimitDefinition
instances, similar to the following:quota_limits = [ endpoints.LimitDefinition( "YOUR_METRIC_NAME", "YOUR_METRIC_DISPLAY_NAME", limit) ]
- Replace
YOUR_METRIC_NAME
with a name that describes the API requests counter. - Replace
YOUR_METRIC_DISPLAY_NAME
with the text that is displayed on Endpoints > Services > Quotas page to identify the quota. Replace
limit
with an integer value. This is the number of requests that an application associated with a consumer's Google 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), ]
- Replace
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)
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 thelimit_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...
See the following for more information on the decorators used in quotas: