AI Prompt to Generate an OpenAPI Spec From Existing API Code
This OpenAPI spec generator prompt tells ChatGPT, Claude, or Gemini how to read your existing API route handlers, controllers, or endpoint functions and produce a clean, versioned OpenAPI (Swagger) specification instead of guessing at a schema from scratch. It's built for backend developers, API maintainers, and teams who inherited undocumented endpoints and need a spec that actually matches the code, not a hand-written approximation that drifts out of date.
The prompt forces the model to work endpoint by endpoint: extract the HTTP method and path, infer request parameters and body shape from the function signature and any validation logic, infer response shapes from what the handler actually returns (including error responses), and flag anything it can't determine with confidence rather than inventing a plausible-looking field. That last part matters — a generated spec with confidently wrong types is worse than no spec, because downstream consumers (client SDKs, mock servers, contract tests) will trust it.
Because the output is YAML or JSON that other tools parse, keeping the prompt itself tight and unambiguous is worth the extra pass. If you're pasting in a large controller file or a verbose instruction set, running it through Prompt Optimizer in Coding mode first can tighten the constraints before you hand it to the model.
Prompt template
Want it sharper? Optimize this prompt with Prompt Optimizer, check it with the Prompt Debugger or shorten it with the Token Optimizer.
Example input
When to use it
- You inherited a REST API with no OpenAPI/Swagger file and need one before onboarding a client team
- A frontend or mobile team needs an accurate contract to generate typed client SDKs against
- You're setting up contract testing or a mock server and need a spec that reflects real behavior, not intent
- You've added or changed several endpoints and the existing spec file has drifted out of sync
Best practices
- Paste the actual handler/controller code, not a description of it — the model should infer types from real validation and serialization logic
- Process one resource or route group per prompt run rather than an entire codebase at once, so context limits don't force the model to summarize instead of read carefully
- Explicitly ask for every error response path (4xx/5xx) the handler can produce, since models tend to document only the happy path unless told otherwise
- Validate the generated YAML against an OpenAPI linter (such as Spectral) before committing it — treat the model's output as a strong first draft, not a final artifact
Common mistakes
- Accepting field types the model guessed without checking them against the actual validation or ORM schema in the code
- Skipping authentication and header requirements because the prompt didn't mention them, leaving the spec incomplete for client generation
- Feeding in minified or transpiled code instead of the original source, which strips the variable names the model needs to infer meaning
- Generating the whole spec in one shot for a large API, which causes the model to drop or merge endpoints it ran out of room to document
FAQs
Can ChatGPT generate an OpenAPI spec directly from my code?
Yes, if you paste the actual route or controller code rather than describing it. The model can infer parameter types, request bodies, and status codes from the logic, though it should flag anything it can't confirm instead of guessing.
What's the difference between generating OpenAPI from code versus from an API description?
Generating from code produces a spec that matches real behavior, including edge cases and error responses the handler actually returns. Generating from a description only captures what you remember to mention, which is why drift happens in the first place.
How do I keep an AI-generated OpenAPI spec from drifting out of date again?
Re-run the prompt whenever a route changes and diff the output against the committed spec file, or wire the prompt into a pre-merge check so undocumented endpoint changes get caught before they ship.
Which Cuelara tool can help me tighten this prompt before running it on a large codebase?
Prompt Optimizer — its Coding mode is built for exactly this kind of structured, constraint-heavy developer prompt, and tightens vague instructions before you spend tokens on a long code paste.