Rate limit Google Cloud Functions with API Gateway

A free built-in solution after hours reading fuzzy documentations

This tutorial follows the implementation made in this tutorial where we created an API with two routes for a Smoothie shop:

With Google API Gateway, deployment, maintenance and monitoring is made easy. BUT, contrary to Cloud Endpoints, we can’t rate limit our API… For real?

After hours reading tutorials, documentation, watching videos and trying different costly solutions, I found out a hack combining Cloud Endpoints and API Gateway documentations.

To rate limit the API, we must add an API Key. Quotas will concern every API Key distinctly.

Add an API Key to the Gateway

To add an API Key we must edit the previously uploaded Open API specification file and add a few keys.

This one for every route :

And this one at the very end :

The resulting file will look like :

We now have to create a new API Config :

Go back to https://console.cloud.google.com/api-gateway/api

Again click “Create Gateway”

But this time, in the API part, select the API we created in the previous Tutorial.

API Config Part

  1. Keep “Create new API config”

2. Choose a Display Name for this config

3. Upload the YAML file we just created

API Gateway Part

Give a name to the Gateway and a region.

The deployment should again take ages.

Once deployed, you can access the API URL after clicking the API and clicking the “Gateway” tab.

If you try to trigger a route defined in the OpenAPI Specification, you will get the following answer :

We need to create an API Key.

To assign the API Key, we first need to enable our API to be used by Google Cloud Platform :

Authorize the API

The fresh API has to be authorized to be accessed GCP services like Credentials.

To authorize the API, we need its Managed Service name.

To find it : Click the API and go to the “Details” tab, copy the “Managed service” value and keep it safe.

Mine looks like this :

Now that we have the Managed service, we can go to https://console.cloud.google.com/apis/library?pli=1 where we can find all existing APIs, including ours.

Search your API by API name :

Click it and click “Enable”.

The API is now enabled, we can attribute credentials.

Attribute credentials

Go to https://console.cloud.google.com/apis/credentials

Click “Create Credentials” and select “API Key”.

The API Key is displayed, directly click “Restrict Key”.

Keep “Application restrictions” at “None” and click “Restrict key” in the “API restrictions” section.

Search your API.

Prepare your cup of tea while the API Key is being created.

After 5 minutes, you can call the gateway using the previous route and the fresh api key at the end :

You will access the smoothies list and be able to order a seasonal smoothie.

Add Quotas and Rate Limit the API

You want to rate limit the API in a few clicks for free with no additional information?

Well… according to Google API Gateway documentation, that’s not possible.

They kind of mention it, but there aren’t any tutorial explaining how to do.

You are the lucky person about to know how to do!


To configure Quotas we need to edit the OpenAPI Specifications file and give the following information :

  • Name our quotas
  • Set the default rate (query per minute and per project)
  • Define the cost of each paths call
  1. Name our quotas

A quota need a name and a displayName. The displayName will be shown and should be readable in the IAM & Admin -> Quotas page to monitor the use of the route.

We will add the x-google-management key at the root level of the YAML file :

For now, valueType and metricKind have no other options.

2. Limit our quotas

Once we have metrics, we need to set a maximum for it.

Inside the x-google-management , add this key :

What’s after “metric: ” should be the name of the corresponding metric set in the previous part.

In the previous example, I set the limit at 2 calls per minute. You might prefer using hours instead of minute? That’s not possible for now, only minute is allowed.

3. Set the cost of an API call

Inside each route (at the same level as the security key), we have to set a cost :

The final yaml file will look as follow:


Heeeeere we go again :

Go back to https://console.cloud.google.com/api-gateway/api

Again click “Create Gateway”

Select the same API.

API Config Part

  1. Keep “Create new API config”

2. Choose a Display Name for this config

3. Upload the YAML file we just created

API Gateway Part

Give a name to the Gateway and a region.

The deployment should again take ages.


We now have a new Gateway URL (accessible in the Gateway tab)

We can call the listSmoothies endpoint a multiple time, we will get this answer confirming that everything went well :

Quotas are displayed in a dashboard in IAM & Admin -> Quotas :

That’s all!

If you enjoyed reading how to do what’s not possible, 50 claps minimum ;)

Adios hippos!

Future Unicorn Founder — Using tech for good