Claude Skills Guide

Claude Code OpenAPI Spec Generation Guide

Generating OpenAPI specifications manually can be tedious and error-prone. Claude Code offers several approaches to streamline this workflow, whether you’re documenting existing APIs or designing new ones from scratch. This guide covers practical methods for OpenAPI spec generation using Claude Code and related skills.

Why Use Claude Code for OpenAPI Generation

OpenAPI specifications serve as the contract between frontend and backend teams. Keeping these contracts accurate and up-to-date is critical for API stability. Claude Code can help by understanding your codebase, extracting endpoint information, and generating properly structured OpenAPI documents.

The main approaches include: prompting Claude directly to analyze code and generate specs, using specialized skills designed for API documentation, and combining Claude with external tools for more complex scenarios.

Method 1: Direct Code Analysis

The simplest approach involves asking Claude to analyze your API code and generate an OpenAPI specification. This works well for projects with well-structured route handlers.

Claude Code works best when route handlers include meaningful parameter names, typed responses, and explicit HTTP status codes. Here’s a well-prepared example:

// routes/users.js
const express = require('express');
const router = express.Router();

/**
 * GET /api/users/:id
 * Retrieve a user by their unique identifier
 */
router.get('/users/:id', async (req, res) => {
  const { id } = req.params;

  try {
    const user = await userService.findById(id);

    if (!user) {
      return res.status(404).json({
        error: 'UserNotFound',
        message: `No user found with ID: ${id}`
      });
    }

    return res.status(200).json({
      id: user.id,
      email: user.email,
      name: user.name,
      createdAt: user.created_at
    });
  } catch (error) {
    return res.status(500).json({
      error: 'InternalServerError',
      message: 'Failed to retrieve user'
    });
  }
});

module.exports = router;

This endpoint includes JSDoc comments, proper error handling, and clear response structures—all signals that help Claude Code produce accurate OpenAPI output.

When you share this code with Claude Code and request an OpenAPI spec, it can generate the corresponding OpenAPI document with path, method, parameters, and response definitions. For more complex APIs, provide the full route file and specify the OpenAPI version you need (typically OpenAPI 3.0 or 3.1).

This method works best when your route handlers include clear parameter names, typed responses, and explicit status codes. If your codebase uses decorators or routing libraries, mention those in your prompt for more accurate results.

Method 2: Using the API Documentation Skill

The automated-code-documentation-workflow-with-claude-skills approach complements OpenAPI generation by maintaining comprehensive API docs. Combine this with the tdd skill to ensure your OpenAPI specs align with test-driven development practices.

When working with multiple endpoints, organizing your documentation workflow helps maintain consistency. The supermemory skill can store and retrieve API design decisions, making it easier to maintain coherence across large API projects.

Method 3: Converting Existing Documentation

If you have API documentation in other formats, Claude can convert them to OpenAPI specs. Share your existing docs—whether in Markdown, HTML, or plain text—and request transformation to OpenAPI YAML or JSON.

# Example generated OpenAPI fragment
paths:
  /api/products:
    get:
      summary: List all products
      parameters:
        - name: category
          in: query
          schema:
            type: string
        - name: limit
          in: query
          schema:
            type: integer
            default: 20
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Product'

Claude handles the conversion by understanding the documentation structure and mapping descriptions, parameters, and response formats to appropriate OpenAPI fields.

Method 4: PDF Integration for Documentation

For teams that need to generate client libraries or SDK documentation alongside OpenAPI specs, the pdf skill provides additional capabilities. Generate your OpenAPI spec first, then use Claude to create comprehensive PDF documentation that includes usage examples, authentication guides, and code samples.

The frontend-design skill can help if you’re building interactive API explorers or developer portals that consume OpenAPI specs. It ensures your documentation UI follows modern design patterns.

Practical Workflow

Here’s a practical workflow for generating OpenAPI specs with Claude Code:

Step 1: Prepare your API code — Ensure your route handlers or API definitions are accessible. Group related endpoints together for clearer context.

Step 2: Generate initial spec — Ask Claude Code to analyze your code and produce an OpenAPI document. Be specific about the version and organizational preferences:

Analyze the attached Express.js route handlers and generate an OpenAPI 3.0 specification.
Include:
- All paths and HTTP methods
- Parameter definitions with types and locations (path, query)
- Response schemas for each status code
- Request body schemas where applicable
- Use components/schemas for reusable definitions

Claude Code will analyze your code and produce a YAML or JSON OpenAPI document. For example, for a user retrieval endpoint it might generate:

paths:
  /api/users/{id}:
    get:
      summary: Retrieve a user by their unique identifier
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
          description: The user's unique identifier
      responses:
        '200':
          description: User found and returned
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
        '404':
          description: User not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: string
        email:
          type: string
          format: email
        name:
          type: string
        createdAt:
          type: string
          format: date-time
    ErrorResponse:
      type: object
      properties:
        error:
          type: string
        message:
          type: string

Step 3: Review and refine — Check the generated spec for accuracy. Claude can make corrections if you identify missing fields or incorrect types.

Step 4: Validate — Use tools like swagger-cli or the Redocly CLI to ensure the spec is valid:

npx @redocly/cli lint openapi.yaml

If validation fails or the spec doesn’t match your expectations, refine your code or provide additional context to Claude Code. Common adjustments include:

Step 5: Generate client SDKs — Once validated, use the spec to generate client libraries in various languages using tools like openapi-generator.

Tips for Better Results

Providing more context improves OpenAPI generation quality. Include your database models, authentication middleware, and error handling patterns. This helps Claude understand the full API surface.

For large APIs, generate specs incrementally—one resource or module at a time. Then combine them into a complete specification. This approach reduces errors and makes review easier.

If you’re designing new APIs, describe your requirements in plain language first. Claude can then generate an initial OpenAPI design before you implement the code, serving as a contract-first development approach.

Common Challenges

Type inference can be challenging when generating OpenAPI from dynamic languages. Be explicit about types in your prompts when possible. For enum values, list all possible options in your request.

Nested objects and arrays require careful specification. Ask Claude to include array item schemas and nested object properties explicitly.

Authentication schemes need special attention. Specify whether you’re using API keys, JWT tokens, OAuth2, or other methods so Claude includes the correct security definitions.

Integration with CI/CD

Automate OpenAPI generation in your CI pipeline. Run Claude against your updated API code on each commit, generate the spec, and validate it before merging. This ensures your documentation always matches your implementation.

For continuous integration, create a script that combines code analysis with specification generation:

#!/bin/bash
# generate-openapi.sh

echo "Analyzing API routes..."
CLAUDE_OUTPUT=$(claude -p "Analyze ./src/routes and generate OpenAPI 3.0 spec" 2>/dev/null)

echo "$CLAUDE_OUTPUT" > openapi.yaml

echo "Validating specification..."
npx @redocly/cli lint openapi.yaml

echo "OpenAPI specification generated successfully"

Run this script during your build process to keep specifications synchronized with your code.

Combine with the tdd skill to generate tests from your OpenAPI specs, creating a full API development lifecycle that keeps contracts and implementations synchronized.


Built by theluckystrike — More at zovo.one