Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Generate OpenAPI 3.x specifications from existing code, routes, or descriptions with proper schemas and documentation.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
SKILL.md
1---2name: openapi-spec-generation3description: Generate and maintain OpenAPI 3.1 specifications from code, design-first specs, and validation patterns. Use when creating API documentation, generating SDKs, or ensuring API contract compliance.4---56# OpenAPI Spec Generation78Comprehensive patterns for creating, maintaining, and validating OpenAPI 3.1 specifications for RESTful APIs.910## When to Use This Skill1112- Creating API documentation from scratch13- Generating OpenAPI specs from existing code14- Designing API contracts (design-first approach)15- Validating API implementations against specs16- Generating client SDKs from specs17- Setting up API documentation portals1819## Core Concepts2021### 1. OpenAPI 3.1 Structure2223```yaml24openapi: 3.1.025info:26title: API Title27version: 1.0.028servers:29- url: https://api.example.com/v130paths:31/resources:32get: ...33components:34schemas: ...35securitySchemes: ...36```3738### 2. Design Approaches3940| Approach | Description | Best For |41| ---------------- | ---------------------------- | ------------------- |42| **Design-First** | Write spec before code | New APIs, contracts |43| **Code-First** | Generate spec from code | Existing APIs |44| **Hybrid** | Annotate code, generate spec | Evolving APIs |4546## Templates4748### Template 1: Complete API Specification4950```yaml51openapi: 3.1.052info:53title: User Management API54description: |55API for managing users and their profiles.5657## Authentication58All endpoints require Bearer token authentication.5960## Rate Limiting61- 1000 requests per minute for standard tier62- 10000 requests per minute for enterprise tier63version: 2.0.064contact:65name: API Support66email: [email protected]67url: https://docs.example.com68license:69name: MIT70url: https://opensource.org/licenses/MIT7172servers:73- url: https://api.example.com/v274description: Production75- url: https://staging-api.example.com/v276description: Staging77- url: http://localhost:3000/v278description: Local development7980tags:81- name: Users82description: User management operations83- name: Profiles84description: User profile operations85- name: Admin86description: Administrative operations8788paths:89/users:90get:91operationId: listUsers92summary: List all users93description: Returns a paginated list of users with optional filtering.94tags:95- Users96parameters:97- $ref: "#/components/parameters/PageParam"98- $ref: "#/components/parameters/LimitParam"99- name: status100in: query101description: Filter by user status102schema:103$ref: "#/components/schemas/UserStatus"104- name: search105in: query106description: Search by name or email107schema:108type: string109minLength: 2110maxLength: 100111responses:112"200":113description: Successful response114content:115application/json:116schema:117$ref: "#/components/schemas/UserListResponse"118examples:119default:120$ref: "#/components/examples/UserListExample"121"400":122$ref: "#/components/responses/BadRequest"123"401":124$ref: "#/components/responses/Unauthorized"125"429":126$ref: "#/components/responses/RateLimited"127security:128- bearerAuth: []129130post:131operationId: createUser132summary: Create a new user133description: Creates a new user account and sends welcome email.134tags:135- Users136requestBody:137required: true138content:139application/json:140schema:141$ref: "#/components/schemas/CreateUserRequest"142examples:143standard:144summary: Standard user145value:146email: [email protected]147name: John Doe148role: user149admin:150summary: Admin user151value:152email: [email protected]153name: Admin User154role: admin155responses:156"201":157description: User created successfully158content:159application/json:160schema:161$ref: "#/components/schemas/User"162headers:163Location:164description: URL of created user165schema:166type: string167format: uri168"400":169$ref: "#/components/responses/BadRequest"170"409":171description: Email already exists172content:173application/json:174schema:175$ref: "#/components/schemas/Error"176security:177- bearerAuth: []178179/users/{userId}:180parameters:181- $ref: "#/components/parameters/UserIdParam"182183get:184operationId: getUser185summary: Get user by ID186tags:187- Users188responses:189"200":190description: Successful response191content:192application/json:193schema:194$ref: "#/components/schemas/User"195"404":196$ref: "#/components/responses/NotFound"197security:198- bearerAuth: []199200patch:201operationId: updateUser202summary: Update user203tags:204- Users205requestBody:206required: true207content:208application/json:209schema:210$ref: "#/components/schemas/UpdateUserRequest"211responses:212"200":213description: User updated214content:215application/json:216schema:217$ref: "#/components/schemas/User"218"400":219$ref: "#/components/responses/BadRequest"220"404":221$ref: "#/components/responses/NotFound"222security:223- bearerAuth: []224225delete:226operationId: deleteUser227summary: Delete user228tags:229- Users230- Admin231responses:232"204":233description: User deleted234"404":235$ref: "#/components/responses/NotFound"236security:237- bearerAuth: []238- apiKey: []239240components:241schemas:242User:243type: object244required:245- id246247- name248- status249- createdAt250properties:251id:252type: string253format: uuid254readOnly: true255description: Unique user identifier256email:257type: string258format: email259description: User email address260name:261type: string262minLength: 1263maxLength: 100264description: User display name265status:266$ref: "#/components/schemas/UserStatus"267role:268type: string269enum: [user, moderator, admin]270default: user271avatar:272type: string273format: uri274nullable: true275metadata:276type: object277additionalProperties: true278description: Custom metadata279createdAt:280type: string281format: date-time282readOnly: true283updatedAt:284type: string285format: date-time286readOnly: true287288UserStatus:289type: string290enum: [active, inactive, suspended, pending]291description: User account status292293CreateUserRequest:294type: object295required:296297- name298properties:299email:300type: string301format: email302name:303type: string304minLength: 1305maxLength: 100306role:307type: string308enum: [user, moderator, admin]309default: user310metadata:311type: object312additionalProperties: true313314UpdateUserRequest:315type: object316minProperties: 1317properties:318name:319type: string320minLength: 1321maxLength: 100322status:323$ref: "#/components/schemas/UserStatus"324role:325type: string326enum: [user, moderator, admin]327metadata:328type: object329additionalProperties: true330331UserListResponse:332type: object333required:334- data335- pagination336properties:337data:338type: array339items:340$ref: "#/components/schemas/User"341pagination:342$ref: "#/components/schemas/Pagination"343344Pagination:345type: object346required:347- page348- limit349- total350- totalPages351properties:352page:353type: integer354minimum: 1355limit:356type: integer357minimum: 1358maximum: 100359total:360type: integer361minimum: 0362totalPages:363type: integer364minimum: 0365hasNext:366type: boolean367hasPrev:368type: boolean369370Error:371type: object372required:373- code374- message375properties:376code:377type: string378description: Error code for programmatic handling379message:380type: string381description: Human-readable error message382details:383type: array384items:385type: object386properties:387field:388type: string389message:390type: string391requestId:392type: string393description: Request ID for support394395parameters:396UserIdParam:397name: userId398in: path399required: true400description: User ID401schema:402type: string403format: uuid404405PageParam:406name: page407in: query408description: Page number (1-based)409schema:410type: integer411minimum: 1412default: 1413414LimitParam:415name: limit416in: query417description: Items per page418schema:419type: integer420minimum: 1421maximum: 100422default: 20423424responses:425BadRequest:426description: Invalid request427content:428application/json:429schema:430$ref: "#/components/schemas/Error"431example:432code: VALIDATION_ERROR433message: Invalid request parameters434details:435- field: email436message: Must be a valid email address437438Unauthorized:439description: Authentication required440content:441application/json:442schema:443$ref: "#/components/schemas/Error"444example:445code: UNAUTHORIZED446message: Authentication required447448NotFound:449description: Resource not found450content:451application/json:452schema:453$ref: "#/components/schemas/Error"454example:455code: NOT_FOUND456message: User not found457458RateLimited:459description: Too many requests460content:461application/json:462schema:463$ref: "#/components/schemas/Error"464headers:465Retry-After:466description: Seconds until rate limit resets467schema:468type: integer469X-RateLimit-Limit:470description: Request limit per window471schema:472type: integer473X-RateLimit-Remaining:474description: Remaining requests in window475schema:476type: integer477478examples:479UserListExample:480value:481data:482- id: "550e8400-e29b-41d4-a716-446655440000"483email: "[email protected]"484name: "John Doe"485status: "active"486role: "user"487createdAt: "2024-01-15T10:30:00Z"488pagination:489page: 1490limit: 20491total: 1492totalPages: 1493hasNext: false494hasPrev: false495496securitySchemes:497bearerAuth:498type: http499scheme: bearer500bearerFormat: JWT501description: JWT token from /auth/login502503apiKey:504type: apiKey505in: header506name: X-API-Key507description: API key for service-to-service calls508509security:510- bearerAuth: []511```512513For advanced code-first generation patterns and tooling, see [references/code-first-and-tooling.md](references/code-first-and-tooling.md):514515- **Template 2: Python/FastAPI** — Pydantic models with `Field` validation, enum types, full CRUD endpoints with `response_model` and `status_code`, exporting the spec as JSON516- **Template 3: TypeScript/tsoa** — Decorator-based controllers (`@Route`, `@Get`, `@Security`, `@Example`, `@Response`) that generate OpenAPI from TypeScript types517- **Template 4: Validation & Linting** — Spectral ruleset (`.spectral.yaml`) with custom rules for operationId, security, naming conventions; Redocly config with MIME type enforcement and code sample generation518- **SDK Generation** — `openapi-generator-cli` for TypeScript (fetch), Python, and Go clients519520## Best Practices521522### Do's523524- **Use $ref** - Reuse schemas, parameters, responses525- **Add examples** - Real-world values help consumers526- **Document errors** - All possible error codes527- **Version your API** - In URL or header528- **Use semantic versioning** - For spec changes529530### Don'ts531532- **Don't use generic descriptions** - Be specific533- **Don't skip security** - Define all schemes534- **Don't forget nullable** - Be explicit about null535- **Don't mix styles** - Consistent naming throughout536- **Don't hardcode URLs** - Use server variables537