MDZelo
首页/Indie Hacker Guide/How to Write API Documentation

How to Write API Documentation

Many products expose an API, but the documentation is only a parameter table. Endpoint, method, field types, and response examples are all there, but developers still struggle to use it. They open the docs and still do not know where to start, how to get a token, what the first code sample should be, or how to debug failure.

Good API documentation is not about listing endpoints. It helps developers complete the first successful call as quickly as possible. The faster the first success, the more likely developers continue integration. The longer they get stuck, the faster they leave.

Early API docs do not need to be huge, but they must be clear, copyable, verifiable, and debuggable.

Start With Quickstart

The most important part of API docs is not the full endpoint list. It is the Quickstart. When developers open the docs for the first time, they should complete a minimal call within a few minutes.

Quickstart should include: create API key, prepare request tool, make first request, see successful response, and understand the next step. Do not begin with long concepts, permission models, and every parameter.

A good Quickstart looks like this:

text
1. Get API Key
2. Copy curl example
3. Replace token
4. Send request
5. Receive a real response
6. Use returned ID to query result

If your API uses async tasks, do not only document task creation. Explain how to query status, get results, and handle failure. Developers care about the complete loop, not a single endpoint.

Explain the Use Case for Every Endpoint

Many API docs describe the endpoint but not when to use it. Developers see POST /tasks and know it creates a task, but they do not know the scenario, relation to other endpoints, or next step.

Every endpoint should start with one sentence explaining the use case. For example: “Use this endpoint to create an analysis task when you need to generate a competitor report for a URL.” This is more useful than “Create task.”

Each endpoint should include purpose, method, path, authentication, request parameters, response fields, success example, failure example, common errors, and next endpoint.

The “next endpoint” is especially important. After creating a resource, should developers query details? After uploading a file, should they wait for processing? After a payment webhook, should they update an order? The docs should guide the next move.

Code Examples Must Run

Code examples in API docs should not be pseudo-code. They should run after copying and replacing the token.

The most basic example is curl because it does not depend on language setup and lets developers quickly verify the API. Depending on your audience, you can also provide JavaScript, Python, PHP, Go, or other examples.

Examples should include complete headers, authentication, request body, and response. Do not omit important fields, and do not use names that differ from the actual API.

Bad example:

text
Call the endpoint and pass parameters.

Good example:

bash
curl -X POST https://api.example.com/v1/reports \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url":"https://example.com","language":"en"}'

Developers love runnable code more than long explanations.

Explain How to Handle Errors

API calls will fail. Invalid auth, bad parameters, insufficient quota, missing resources, rate limits, service timeout, and task failure will all happen.

Docs should not only list error codes. They should explain what developers should do. 401 means invalid API key, so check the token. 429 means too many requests, so use backoff retry. 402 means insufficient quota, so upgrade or recharge. 500 means server issue, so retry later or contact support.

The error format should be consistent. Do not return message in one endpoint, error in another, and plain strings elsewhere. A consistent format makes error handling easier.

A recommended format:

json
{
  "error": {
    "code": "rate_limit_exceeded",
    "message": "Too many requests. Please retry later.",
    "request_id": "req_123"
  }
}

request_id is important. When developers report a problem, you can use it to find logs.

Document Authentication and Permissions Separately

API docs must clearly explain authentication. Is it Bearer Token, Basic Auth, signing, OAuth, or webhook secret? Where is an API key created? Can it be rotated? How is it revoked after leakage? Which resources can different keys access?

Many developers get stuck on auth first. The docs should have a separate Authentication page with header format, key management, scopes, security advice, and common errors.

If your API has sandbox and production environments, explain the base URL difference clearly. Do not let developers use test keys against production or production keys during local experiments.

An API key is a security boundary, not just a string. Warn developers not to expose keys in frontend code, mobile apps, or public repositories.

Webhook Docs Need More Detail

If your product supports webhooks, those docs need more detail than normal endpoints. Webhooks are your system calling the developer's system, so debugging is harder.

Webhook docs should include event types, request method, payload, signature verification, retry policy, timeout, idempotency, and testing method.

Signature verification and idempotency are especially important. Developers need to know the request truly comes from you, and they need to handle duplicate events without double charging, double delivery, or duplicate records.

Webhook examples should include complete payloads, not only field tables. Developers need to see what real events look like.

API Documentation Structure Template

Use this structure for a first version:

text
Overview
- What the API can do
- Base URL
- Versioning

Quickstart
- Get API Key
- First call
- Query result

Authentication
- Header format
- Key management
- Scopes

Core APIs
- Create resource
- Query status
- Get result
- Update / delete

Errors
- Error format
- Common error codes
- Handling suggestions

Webhooks
- Event list
- Signature verification
- Retry and idempotency

SDK / Examples
- curl
- JavaScript
- Python

The first version does not need an SDK, but it should have curl and one mainstream language example. If developers can run the first call quickly, your docs are already better than many parameter tables.

Summary

The goal of API documentation is helping developers complete the first successful call quickly and debug failure themselves. Quickstart, use cases, runnable examples, consistent errors, authentication, and webhook details are the most important parts of early API docs.

Do not write API docs as an endpoint list. Write them as an integration path that takes developers from API key to first real result.

Homework

  • Write a 5-minute Quickstart for your API.
  • Add one use-case sentence to every endpoint.
  • Provide a curl example and success response for the core endpoint.
  • Design a consistent error format with request_id.
  • If you have webhooks, document signature verification and retry policy.

Next Lesson

How to Plan Product Versions: versions are not feature timelines. They are a way to expand product capability around validation goals.

评论

0

分享你对这一章的想法

还没有评论成为第一个分享想法的人。