Api gateway openapi spec authoring

From google/skills · View interactive version

Api gateway openapi spec authoring is an AI agent skill from the GitHub repository google/skills. Writes, migrates, and fixes OpenAPI specs for Google Cloud API Gateway. Use when converting an OpenAPI 2.0 (Swagger) spec to OpenAPI 3.x, writing a new OpenAPI 3.x spec, or fixing a failed "gcloud api-gateway api-configs create", such as "extension is only supported in OpenAPI 2.x". Also use when qu

How to use this skill

Skill file

API Gateway OpenAPI Spec Authoring

API Gateway accepts API configs in OpenAPI 2.0, 3.0.x, and 3.1.x. The Google extensions in OpenAPI 3.x have a different structure from the OpenAPI 2.0 extensions:

Use this skill to migrate an OpenAPI 2.0 spec, write a new OpenAPI 3.x spec, or fix an API config that fails to create.

Attribution

MUST READ: Before executing any gcloud, REST API, or MCP command, you

MUST read Usage Attribution. For commands executed

directly in the terminal (never in generated scripts, Terraform, or user code),

prefix gcloud with

CLOUDSDK_METRICS_REQUEST_ATTRIBUTION="agent-skills/1.0 (skill:api-gateway-openapi-spec-authoring)"

and append agent-skills/1.0 (skill:api-gateway-openapi-spec-authoring) to User-Agent headers.

Prerequisites

  1. Install the tools:

```bash

pip install "PyYAML>=6.0,<7.0"

```

  1. Authenticate, and select the project, billing, and location:
  1. Enable the required Google Cloud APIs:

```bash

gcloud services enable \

apigateway.googleapis.com \

servicemanagement.googleapis.com \

servicecontrol.googleapis.com \

--project={project_id}

```

  1. Verify the IAM roles (use the most restrictive role for each step):

Decision Matrix

| Task | Required Mechanism | Critical Guardrail | Detailed Reference |

| --- | --- | --- | --- |

| Migrate an OpenAPI 2.0 (swagger: "2.0") config | Convert the standard fields, then move each Google extension to its OpenAPI 3.x form. | A converter such as Swagger Editor leaves every x-google-* extension in its OpenAPI 2.0 form. It also writes servers without x-google-endpoint, so the API loses its base path. Rationale: API Gateway rejects most OpenAPI 2.0 extensions in a 3.x spec with The '{extension}' extension is only supported in OpenAPI 2.x. | OpenAPI 2.0 to 3.x Migration |

| Route requests to backends | Root x-google-api-management.backends.{backend_name} + x-google-backend: {backend_name} at the root or on an operation | Set pathTranslation on every backend. Rationale: the default is APPEND_PATH_TO_ADDRESS for a root x-google-backend, but CONSTANT_ADDRESS for an operation-level one. CONSTANT_ADDRESS sends every request to the address unchanged, and sends path parameters as query parameters. APPEND_PATH_TO_ADDRESS appends the full request path, with the base path. NEVER set x-google-backend on a path item: API Gateway rejects it. | Extensions and Limits |

| Authenticate callers | JWT: type: oauth2 scheme with x-google-auth (issuer, jwksUri, audiences). API key: type: apiKey scheme with x-api-key in a header, or key or api_key in the query. | API Gateway does not check OAuth scopes. In a security list, put an API key in every alternative (OR) or in none. NEVER put two JWT schemes in one requirement object (AND). Rationale: API Gateway rejects these combinations. If the user asks for one of them, such as a JWT or an API key as alternatives, do not write it in the spec. Explain the rule, and write a supported option, such as an API key in every alternative or a separate path for each kind of caller. | Extensions and Limits |

| Add quotas | x-google-api-management.metrics and quota.limits + x-google-quota: {metric_name: cost} on each operation | NEVER rely on x-google-quota at the root or path level. Rationale: in OpenAPI 3.x, API Gateway ignores it there without an error. Require an API key on each operation that has a quota, and keep each metric name the same in every API config. | Extensions and Limits |

| Set the base path and CORS | One servers entry with x-google-endpoint, such as url: /v1 | API Gateway ignores servers entries without x-google-endpoint. allowCors: true only passes CORS requests to the backend. Rationale: the gateway does not add CORS headers, so the backend must answer them. | Extensions and Limits |

| Fix a failed api-configs create | Match the error text to the error table, and fix the spec. | Fix the spec before you retry, unless the error is about IAM or another api-configs create that runs. Rationale: API Gateway returns the same error for the same spec. | API Config Errors |

| Expose operations as MCP tools | The api-gateway-mcp-server skill | MCP adds rules for tool names, descriptions, and tools/list security. If that skill is not installed, use this skill for the OpenAPI 3.x spec, and follow the Configure MCP guide for the MCP extensions. | - |

Execution Workflow and Validation Loop

  1. Get the current spec:

```bash

gcloud api-gateway gateways describe {gateway_id} --location={region} --project={project_id} --format="value(apiConfig)"

```

```bash

gcloud api-gateway api-configs describe {config_id} --api={api_id} --project={project_id} --view=FULL \

--format="value(openapiDocuments[0].document.contents)" | tr '_-' '/+' | base64 --decode

```

  1. Write the OpenAPI 3.x spec:
  1. Run the deterministic validation loop on every spec that you write or change, before you answer or create the API config:

```bash

python3 scripts/validate_apigw_openapi.py --spec={spec_file}

```

  1. Summarize the spec for the user, and get approval before you deploy. State each of these points:
  1. Create a new API config and update the gateway (request human approval before you update a production gateway):

```bash

gcloud api-gateway api-configs create {new_config_id} \

--api={api_id} \

--openapi-spec={spec_file} \

--backend-auth-service-account={gateway_sa_email} \

--project={project_id}

gcloud api-gateway gateways update {gateway_id} \

--api={api_id} \

--api-config={new_config_id} \

--location={region} \

--project={project_id}

```

  1. Verify the gateway:

Pre-Deployment Checklist

Resource Cleanup

To avoid ongoing charges when you tear down a test environment, request human approval, and delete the resources in dependency order:

gcloud 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}
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.