API Reference DocumentationLesson 3.5
How to write API code examples developers actually copy
multi-language code examples, curl examples, SDK code examples, realistic request data, runnable examples, language selection, copy-paste readiness
API Code Examples That Get Copied
Developers copy code examples. That's not a shortcut — it's the workflow. Your examples must be complete enough to copy, modify, and run without any implicit knowledge.
The Minimum Viable Example
Every example must include: the full request (URL, headers, body), authentication, and the expected response. No gaps.
### Create a User
**curl**
```bash
curl -X POST https://api.example.com/v1/users \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"email": "alice@example.com",
"name": "Alice Chen",
"role": "editor"
}'
```
**JavaScript (fetch)**
```javascript
const response = await fetch('https://api.example.com/v1/users', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: 'alice@example.com',
name: 'Alice Chen',
role: 'editor',
}),
});
const user = await response.json();
console.log(user.id); // "usr_01HQ..."
```Use Realistic Data
Replace placeholder data like string or value with realistic examples: real-looking emails, proper UUIDs, valid ISO dates. Fake but realistic data reduces the mental work of adapting examples to real use cases.
