Rate limit Google Cloud Functions with API Gateway

Photo by Larry James Baylas on Unsplash — Protect the gates of your API

Add an API Key to the Gateway

security:
- api_key: []
securityDefinitions:
api_key:
type: "apiKey"
name: "key"
in: "query"
Api Config Name : Medium Config with API Key
Name : SmoothAPI Gateway API Key
smoothapi-0h4j1i8nhd89ej.apigateway.my-project.cloud.goog
https://smoothapi-gateway-api-key-3dh32s89.nw.gateway.dev/listSmoothies?key=AIzaSyADWwrzjr32fj_apz2AX-GRKzHkDGNDer0

Add Quotas and Rate Limit the API

hum…
x-google-management:
metrics:
- name: "list-smoothies-request"
displayName: "List smoothie quota"
valueType: INT64
metricKind: DELTA
- name: "order-smoothie-request"
displayName: "Order smoothie quota"
valueType: INT64
metricKind: DELTA
quota:
limits:
- name: "list-smoothie-limit"
metric: "list-smoothies-request"
unit: "1/min/{project}"
values:
STANDARD: 2
- name: "order-smoothie-limit"
metric: "order-smoothie-request"
unit: "1/min/{project}"
values:
STANDARD: 2
x-google-quota:
metricCosts:
"list-smoothies-request": 1
Api Config Name : Medium Config with API Key and Quotas
Name : SmoothAPI Gateway API Key and Quotas
{"message": "RESOURCE_EXHAUSTED:Quota exceeded for quota metric 'List smoothie quota' and limit 'List smoothie quota per minute' of service 'smoothapi-011vx544djuq3.apigateway.steam-outlet-209412.cloud.goog' for consumer 'project_number:XX'.","code": 429}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store