How to Write API Documentation That Developers Love
A complete guide to writing API docs — from endpoint descriptions to code examples to error handling.
API documentation is the user interface for developers. It is the thing they stare at while integrating your service, the thing they search when something breaks at 2 AM, and the thing they judge before deciding whether to use your product at all. Bad API docs cost you users. Good API docs are a competitive advantage.
This guide covers everything you need to write API documentation that developers actually enjoy using.
Start With the Reference, Not the Guide
Most API documentation projects stall because the team starts writing conceptual guides before documenting the endpoints. This is backwards. The endpoint reference is the foundation. Build it first.
Every endpoint page should follow a consistent structure:
- HTTP method and path —
POST /v1/users - One-line description — What this endpoint does
- Authentication — How to authenticate the request
- Parameters — Every field, with type, required/optional, and description
- Request example — A complete, working request
- Response example — The exact JSON response body
- Error responses — Status codes and what they mean for this endpoint
This structure is not negotiable. Every endpoint, every time. Consistency is what makes API docs usable. When a developer learns the pattern on one page, they can navigate every other page without thinking.
Write Descriptions That Answer "Why"
Most API docs describe what a parameter is. Few explain when or why you would use it.
Bad description:
timeout— The timeout value.
Better:
timeout(integer, optional) — Maximum time in milliseconds to wait for processing to complete. Defaults to30000. Increase this for large batch operations that may take longer than 30 seconds.
The second version tells the developer three things: what the parameter does, what a sensible default is, and when they might need to change it. That context saves support tickets.
Apply this principle everywhere. Don't just say a field is a string — say what format the string should be in. Don't just say a parameter is optional — explain what happens when it's omitted.
Make Every Example Copy-Paste Ready
Code examples are the most-used part of API documentation. Developers do not read your docs linearly; they scan for a code block, copy it, modify it, and run it. If the example doesn't work, they blame your docs, not themselves.
Rules for good API examples:
Include all required headers. Don't write curl https://api.example.com/endpoint and assume the developer will figure out authentication. Show the complete request:
curl -X POST https://api.example.com/v1/messages \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"to": "user@example.com",
"subject": "Hello from the API",
"body": "This is a test message."
}'
Use realistic data. Don't use foo, bar, test123, or Lorem ipsum. Use data that looks like what the developer will actually send. This helps them map the example to their own use case.
Show the response. Every request example should be followed by the exact response the developer will receive:
{
"id": "msg_8f3k2j1",
"status": "sent",
"to": "user@example.com",
"created_at": "2026-03-18T14:30:00Z"
}
Provide examples in multiple languages. At minimum, provide curl and one popular language (Python, JavaScript, or Go, depending on your audience). Tabbed code blocks are ideal for this. The curl example is for quick testing; the language example is for production integration.
Document Errors Like a Developer Would Read Them
Error documentation is the most neglected part of API docs, and it is the part developers need most urgently. When something breaks, the developer is already frustrated. Unclear error docs make it worse.
Structure your error responses
Every error response from your API should have a consistent structure:
{
"error": {
"type": "invalid_request",
"message": "The 'email' field must be a valid email address.",
"param": "email",
"code": "invalid_email_format"
}
}
Document errors per endpoint
A global error reference page is useful, but developers need to know which errors a specific endpoint can return. On each endpoint page, list the relevant error codes:
| Status | Code | Description |
|---|---|---|
| 400 | missing_required_field |
A required field was not included in the request body |
| 401 | invalid_api_key |
The API key in the Authorization header is invalid or expired |
| 409 | duplicate_email |
A user with this email address already exists |
| 429 | rate_limit_exceeded |
Too many requests. Retry after the time specified in the Retry-After header |
Explain how to fix each error
Don't just tell the developer what went wrong — tell them how to fix it. "The email field is required" is good. "The email field is required. Include it in the request body as a string in standard email format (user@example.com)" is better.
Authentication Deserves Its Own Page
Authentication is the first hurdle every developer hits, and it is the most common source of integration failures. Give it a dedicated page with:
- How to get an API key (step by step, with screenshots if the dashboard is complex)
- How to include the key in requests (header name, format)
- What happens when the key is invalid (exact error response)
- Rate limits and how they are enforced
- Scoping and permissions, if applicable
Even if authentication is simple, document it thoroughly. The developer reading your auth docs might be a junior developer setting up their first API integration. Don't assume knowledge.
Pagination, Filtering, and Sorting
If your API returns lists of resources, document the pagination system clearly. This is an area where vague docs cause real problems because the developer needs to implement pagination correctly or they'll miss data.
Cover:
- What pagination style you use (cursor-based, offset-based, page-based)
- The parameters for controlling pagination (
limit,cursor,offset,page) - Maximum and default page sizes
- How to detect the last page
- A complete example of paginating through a full result set
# First page
curl "https://api.example.com/v1/users?limit=50"
# Next page (using cursor from previous response)
curl "https://api.example.com/v1/users?limit=50&cursor=eyJpZCI6NTB9"
{
"data": [...],
"has_more": true,
"next_cursor": "eyJpZCI6MTAwfQ"
}
Versioning and Changelog
Tell developers how your API is versioned and what happens when you release a new version. Key questions to answer:
- Where is the version specified? (URL path, header, query parameter)
- What is the current version?
- What is your deprecation policy?
- Where is the changelog?
Maintain a changelog that lists every breaking change, new endpoint, and deprecated field. Date each entry. Developers need to know whether an API change happened before or after they shipped their integration.
Rate Limits
Document your rate limits on a dedicated page and on each endpoint that has custom limits. Include:
- The default rate limit (e.g., 100 requests per minute)
- Any endpoint-specific limits
- Which headers contain rate limit information (
X-RateLimit-Limit,X-RateLimit-Remaining,X-RateLimit-Reset) - What happens when the limit is exceeded (HTTP 429,
Retry-Afterheader) - Best practices for staying under the limit (exponential backoff, request batching)
SDKs and Client Libraries
If you provide official client libraries, document them separately from the REST API. Each SDK should have its own getting started guide with installation, initialization, and a basic example. Link from each REST endpoint page to the equivalent SDK method.
Don't assume that SDK users will read the REST docs, and don't assume that REST users will read the SDK docs. These are different audiences with different needs.
Testing and Sandbox Environments
If you offer a sandbox or test mode, document it prominently. Developers want to test their integration without affecting real data. Explain:
- How to switch between test and production modes
- Any differences in behavior between modes
- Test API keys or credentials
- How to reset test data
Putting It All Together
Great API documentation is not about beautiful design or clever copy. It is about completeness, consistency, and accuracy. Cover every endpoint. Use the same structure on every page. Keep examples up to date. Document errors thoroughly. And above all, write for the developer who is trying to get something working at 2 AM — because that is when your docs matter most.
The effort you invest in API documentation pays compounding returns. Good docs reduce support tickets, accelerate onboarding, improve developer satisfaction, and ultimately drive adoption. It is one of the highest-leverage investments a developer tools company can make.