Loading...
Loading...
Secures and rate limits backend services behind Google Cloud API Gateway across OpenAPI 2.0 and OpenAPI 3.0.x API configurations, including MCP-enabled gateways. Use when configuring API key or JWT authentication, locking down Cloud Run or Cloud Run functions backends with a gateway service account, reading authenticated claims from X-Apigateway-Api-Userinfo, setting per-project request quotas, or diagnosing HTTP 429 and HTTP 500 Service Control Quota errors. Do not use for Apigee, GKE Gateway API, Cloud Load Balancing custom domain setup, or LLM model routing.
npx skill4agent add google/skills api-gateway-backend-security-and-rate-limitingswagger: "2.0"openapi: 3.0.4PyYAMLpip install "PyYAML>=6.0,<7.0"gcloud auth logingcloud auth application-default loginGOOGLE_APPLICATION_CREDENTIALS={key_file_path}gcloud config set project {project_id}gcloud projects create {project_id}{project_id}us-central1--locationgcloud services enable \
apigateway.googleapis.com \
servicemanagement.googleapis.com \
servicecontrol.googleapis.com \
iam.googleapis.com \
apikeys.googleapis.com \
--project={project_id}roles/apigateway.adminroles/serviceusage.serviceUsageAdminroles/iam.serviceAccountAdminroles/iam.serviceAccountUserroles/serviceusage.apiKeysAdminroles/run.invokerroles/cloudfunctions.invoker| Goal | Mechanism to Configure | Key Constraint | Detailed Reference |
|---|---|---|---|
| Identify the calling consumer project for quotas and billing | API key ( | NEVER rely on an API key alone to authenticate end-user identity. Rationale: API keys identify a Google Cloud project rather than a user, and clients can extract them. | Client Authentication |
| Authenticate end users or calling services | JWT ( | Operation-level | Client Authentication |
| Block direct calls to the backend service | Dedicated gateway service account ( | Roll out the gateway config with | Backend Security |
Rate limit requests per consumer project ( | | NEVER rename or remove a quota metric when creating a new API config while any gateway of the API runs an older config referencing that metric. Rationale: Creating the config immediately overwrites API-wide quotas and causes | Quotas and Rate Limiting |
Secure MCP | | | MCP Traffic Security |
{gateway_sa_name}roles/run.invoker{backend_service}X-Apigateway-Api-Userinfobase64urlAuthorizationx-google-api-management.mcptools-listsecurityapi-configs create{candidate_openapi_spec}x-google-management.metricsx-google-api-management.metricsapi-configs createscripts/check_quota_metrics.py{api_id}{project_id}python3 scripts/check_quota_metrics.py \
--spec={candidate_openapi_spec} \
--project={project_id} \
--api={api_id}gcloud--baseline-specpython3 scripts/check_quota_metrics.py \
--spec={candidate_openapi_spec} \
--baseline-spec={previous_openapi_spec} \
--dry-runscripts/check_quota_metrics.py1status: "FAIL"{candidate_openapi_spec}x-google-quotaignored_quota_extensions0status: "PASS"skipped_configs2gcloudgcloud api-gateway api-configs creategcloud api-gateway gateways updatemanagedService{backend_service}--no-allow-unauthenticated*.run.app403defaultHostname401200{gateway_sa_name}roles/run.invokerroles/cloudfunctions.invoker--no-allow-unauthenticatedX-Apigateway-Api-UserinfodefaultHostname*.gateway.devapi_key: []managedServicepython3 scripts/check_quota_metrics.pystatus: "PASS"0api-configs createx-google-api-management.mcp.tools-list.securitysecurityx-google-mcp-tool: falsegcloud api-gateway gateways delete {gateway_id} --location={region} --project={project_id}
gcloud api-gateway api-configs delete {config_id} --api={api_id} --project={project_id}
gcloud api-gateway apis delete {api_id} --project={project_id}
gcloud iam service-accounts delete {gateway_sa_name}@{project_id}.iam.gserviceaccount.com --project={project_id}