---
name: write-openapi-spec
description: Writes an OpenAPI 3.1 document for an API that already has routes - paths, reusable schemas, examples, and a security scheme - and checks the file parses. Use when the user wants an OpenAPI file, a Swagger document, or to document existing endpoints as a contract.
license: MIT
metadata:
  category: backend
  tags: "openapi,docs"
  author: johnny-bravo
  version: "1.0"
  updated: "2026-11-07"
  status: published
  aliases: "generate-openapi-spec, swagger, openapi 3.1, api contract file"
  source_url: "https://github.com/wshobson/agents/tree/7e2fb559cf4b824b27f1fec49fe88f7511066d26/plugins/documentation-generation/skills/openapi-spec-generation"
  source_license: MIT
  copyright: "Copyright (c) 2024 Seth Hobson"
  original_author: "Seth Hobson (wshobson/agents, openapi-spec-generation)"
  modified: "Adapted by TheSkillVault: rewritten as a short code-first checklist (paths, components, examples, security, lint) with one original fragment; dropped the design-first catalogue; added secret handling, gotchas and done-when."
  imported_on: "2026-11-07"
---

# Write an OpenAPI spec

OpenAPI is the contract file other people and tools read. Write it from the routes you already have. Share shapes with `$ref` so a field is described once.

## When to use
- Handlers exist and there is no `openapi.yaml`.
- A Swagger page or a generated client is out of date.
- The same error shape is copied under every path.

## When not to use
- The routes, status codes, and error shape are not chosen yet. Settle that contract first.
- The task is only to speed up a SQL query.

## Steps
1. Set `openapi: 3.1.0`, plus `info.title` and `info.version`. Put the public base URL under `servers`. Do not hard-code a laptop address.
2. Add one path item per route you ship. Name the method, the summary, and the success response. Match the path the server actually mounts, including a version prefix if you use one.
3. Move repeated bodies into `components.schemas`. Point at them with `$ref`. Use the same name for a field in every schema.
4. Mark fields that must be sent with the schema's field list, and give each of those fields an example that is obviously fake.
5. Document the error body once, then `$ref` it from the error responses you actually emit.
6. Add `components.securitySchemes` for the auth you use (a bearer token, a cookie, or an API key header). Attach it to the paths that check auth. Leave public paths unmarked.
7. Lint the file. Redocly, Spectral, or `@apidevtools/swagger-parser` will do. The file must parse, and every `$ref` must resolve.
8. If the handlers drift, update the spec in the same change. A spec that lies is worse than no spec.

```yaml
openapi: 3.1.0
info:
  title: Orders
  version: 1.4.0
paths:
  /orders:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/NewOrder"
      responses:
        "201":
          description: Created
components:
  schemas:
    NewOrder:
      type: object
      properties:
        sku:
          type: string
          examples: ["mug-01"]
```

## Gotchas
- Do not paste live tokens, passwords, or customer payloads into examples. Use fake values such as `sku-demo`.
- OpenAPI 3.1 uses JSON Schema. A 3.0 `nullable: true` is not the 3.1 form. Use `type: ["string", "null"]` when a value may be null.
- A path that does not match the router will generate a client that calls the wrong URL.
- Security on the whole file also locks public routes. Override those with `security: []`.

## Portability
The file is tool-neutral. If you use FastAPI, Nest, or tsoa, generate this document from the code and then read the diff. If you use Go or Rails, a hand-written file plus a lint step is enough. The same `$ref` habit works in either style.

## Done when
- `openapi.yaml` parses as 3.1 and every shipped route is listed.
- Shared bodies and the error shape are `$ref`s, with fake examples.
- Auth is declared, and no real secret is in the file.

## Example prompt
"Write an OpenAPI 3.1 file for the order routes in this server. Reuse the error schema, and do not include real customer data."

---
Adapted from "openapi-spec-generation" in wshobson/agents (MIT, Copyright (c) 2024 Seth Hobson). See LICENSE.txt.
