AI prompt for writing API documentation
This is a ready-made AI prompt for writing API documentation, written for Claude and free to copy. API documentation fails on the error cases. The happy path is easy to describe and rarely what someone is reading the docs for — they are there because something returned a 422 and they do not know why. The prompt below handles that: it is compiled as a coding task, so it carries the structure and the constraints that kind of work needs.
The prompt
Written for Claude. Compiled as a coding task — writing, reviewing, debugging, or refactoring software.
<role>
You are a senior software engineer who writes production-ready, tested code.
</role>
<context>
Endpoint: {{endpoint}}. Parameters: {{parameters}}. Responses: {{responses}}. Auth: {{auth}}.
</context>
<task>
Write documentation for this API endpoint.
</task>
<constraints>
- Give a complete working request example, not a fragment.
- Document every error response, not just the success case.
- State which parameters are required and what the defaults are.
- Say what happens on the edge cases: empty results, rate limits, partial failures.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.
</constraints>
<output_format>
Respond in Markdown. Use descriptive headings, and keep paragraphs to three sentences or fewer.
</output_format>
<approach>
Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.
</approach>
<success_criteria>
Before you finish, check the output against every item below and fix anything that fails.
- A developer could make a successful call without reading anything else.
</success_criteria>
Now, write documentation for this API endpoint.Open this in the prompt builder to add your own context and see what would improve it most.
What makes writing API documentation hard to prompt for
API documentation fails on the error cases. The happy path is easy to describe and rarely what someone is reading the docs for — they are there because something returned a 422 and they do not know why.
Why this prompt works
- Background comes before the instruction
- The model reads the situation before it learns what to do with it, which stops the instruction being diluted by everything that follows. The task is then restated as the final line, where models weight it most heavily.
- Rules a coding task assumes but nobody writes down
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections. Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle. These are added automatically because leaving them implicit is the most common reason this kind of output disappoints.
- Success criteria the model checks itself against
- Stating how the output will be judged gives the model something concrete to verify before it finishes. Prompts without criteria produce work that is plausible but incomplete, because nothing defined when it was done.
What to replace
| Placeholder | What to put there |
|---|---|
| {{endpoint}} | Your endpoint. |
| {{parameters}} | Your parameters. |
| {{responses}} | Your responses. |
| {{auth}} | Your auth. |
Check the output before you use it
- Copy the example request and run it. If it does not work verbatim, it is not an example.
- Confirm every error code the endpoint can return is documented.
- Check required versus optional is stated for every parameter.
The same prompt for other models
Identical content, packaged the way each model reads most reliably.
ChatGPT
## Role
You are a senior software engineer who writes production-ready, tested code.
## Context
Endpoint: {{endpoint}}. Parameters: {{parameters}}. Responses: {{responses}}. Auth: {{auth}}.
## Task
Write documentation for this API endpoint.
## Constraints
- Give a complete working request example, not a fragment.
- Document every error response, not just the success case.
- State which parameters are required and what the defaults are.
- Say what happens on the edge cases: empty results, rate limits, partial failures.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.
## Output format
Respond in Markdown. Use descriptive headings, and keep paragraphs to three sentences or fewer.
## Approach
Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.
## Success criteria
Before you finish, check the output against every item below and fix anything that fails.
- A developer could make a successful call without reading anything else.
Now, write documentation for this API endpoint.Gemini
**Task**
Write documentation for this API endpoint.
**Role**
You are a senior software engineer who writes production-ready, tested code.
**Context**
Endpoint: {{endpoint}}. Parameters: {{parameters}}. Responses: {{responses}}. Auth: {{auth}}.
**Constraints**
- Give a complete working request example, not a fragment.
- Document every error response, not just the success case.
- State which parameters are required and what the defaults are.
- Say what happens on the edge cases: empty results, rate limits, partial failures.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.
**Output format**
Respond in Markdown. Use descriptive headings, and keep paragraphs to three sentences or fewer.
**Approach**
Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.
**Success criteria**
Before you finish, check the output against every item below and fix anything that fails.
- A developer could make a successful call without reading anything else.
Now, write documentation for this API endpoint.Common questions
- What makes API documentation actually useful?
- A runnable example and complete error documentation. Developers arrive at docs either to make their first call or to understand a failure, and the second case is the one most documentation neglects.
- Can AI generate API docs from my code?
- It can produce the structure and the happy path accurately. What it cannot infer is why an endpoint behaves as it does, which errors are common in practice, and what integrators typically get wrong — supply those.
- Which AI model is best for writing API documentation?
- All of them handle this; what changes is the packaging. This page shows the same prompt written for Claude, ChatGPT, Gemini. Claude follows XML-delimited structure most reliably, ChatGPT works best with markdown headings, and Gemini prefers the task stated before the material. The content of the prompt is identical in each.
- Can I change this prompt for my own situation?
- Yes, and you should. Replace the placeholders with your own details, then open it in the builder to add context specific to you. The builder scores what you supply and tells you exactly which missing piece would improve it most.
- Why does this prompt include rules I did not ask for?
- Because coding tasks carry requirements that experienced practitioners apply automatically and rarely write down. The compiler adds them so the output does not fail on something obvious. Every added rule is listed on the how it works page.
Related prompts
- AI prompt for reviewing code
- AI prompt for debugging an error
- AI prompt for writing unit tests
- AI prompt for refactoring code
- AI prompt for writing a SQL query
- AI prompt for writing a regular expression
Browse all ready-made prompts, the full prompt library, or read the guides.