How to Write API Documentation With Code Examples
A practical guide to writing API documentation that developers actually use, with advice on code examples, structure, and keeping docs up to date.
Start With the Endpoint, Not the Explanation
Most developers open API docs with one question: what do I call and what do I get back? Answer that first. Lead each endpoint with the HTTP method, the full path, and a one-sentence summary. Save the paragraphs of context for after the example — not before it.
A developer who can paste a curl command and see a real response in two minutes will trust your docs. One who has to read three paragraphs before seeing any code will open a different tab. Structure is the first form of respect you can show your users.
Code Examples Do More Work Than Prose
Every endpoint needs at least one complete, runnable example. Not a template with YOUR_API_KEY_HERE sprinkled throughout — a real request and a real response, even if the data is mocked. Developers copy and paste first; they read second.
Show multiple languages where adoption warrants it. curl is the baseline because every developer can read it regardless of their stack. Add Python, JavaScript, and one other language that fits your audience. Keep the examples in sync with the actual API — stale examples are worse than no examples, because they waste the reader's time and erode trust.
For response examples, include both a success case and a common error. A 400 with a clear error object teaches developers how to handle failures before they hit one in production at midnight.
Structure Parameters and Authentication Consistently
Pick a format for documenting parameters and never deviate from it. For each parameter: name, type, whether it is required, a default value if optional, and a one-line description. A table works well. Whatever you choose, every single endpoint should follow the same layout so developers can scan without re-orienting.
Authentication deserves its own top-level section, not a footnote on the first endpoint. Explain the method — API key, OAuth 2.0, JWT — show exactly where credentials go in the request, and list the error codes a developer will see if they get it wrong. This section will be the most-read page in your docs for any developer starting fresh.
Treat Docs Like Code — Version and Review Them
Docs that live in a wiki no one owns go stale fast. Store documentation alongside the code in the same repository. When a pull request changes an endpoint's behavior, the review should include a docs update. No merge without updated docs is a reasonable team policy.
Use a linter or a spec file like OpenAPI to generate or validate reference docs automatically. This does not replace human-written guides, but it keeps the mechanical details — parameter names, response schemas, status codes — accurate without relying on a writer to catch every change.
Version your docs alongside your API. If you maintain v1 and v2, readers need to be on the right version without hunting. A version switcher at the top of every page is a small investment that prevents a lot of confused support tickets.
Frequently asked questions
- How long should an API documentation code example be?
- Long enough to be runnable, short enough to scan in under thirty seconds. Show the request, the key headers, and the response. Strip anything that is not essential to the concept being demonstrated. If a full working example needs more context, link to a longer guide rather than expanding the reference page.
- Should I use OpenAPI to write my API docs?
- OpenAPI is excellent for generating accurate reference documentation and keeping it in sync with the API. It is less good for tutorials, conceptual guides, and code examples that explain intent. Use OpenAPI for the reference layer and write human-authored guides alongside it.
- What is the most common mistake in API documentation?
- Outdated code examples. An example that no longer works destroys trust immediately. Treat code examples as production artifacts — review them when the API changes, test them in CI if you can, and remove any you cannot keep current.
- How do I document error responses properly?
- List every status code an endpoint can return, not just 200. For each error, show the exact JSON response shape, explain what triggered it, and tell the developer how to fix it. Vague error docs produce support tickets; specific ones produce happy developers.