API yang well-designed membuat frontend dan mobile developer senang. Berikut best practices yang harus kamu terapkan.
RESTful Naming Convention
GET /api/articles → List articles
GET /api/articles/:id → Get single article
POST /api/articles → Create article
PUT /api/articles/:id → Update article
DELETE /api/articles/:id → Delete article
HTTP Status Codes
- 200: OK
- 201: Created
- 400: Bad Request (validasi error)
- 401: Unauthorized (belum login)
- 403: Forbidden (tidak punya akses)
- 404: Not Found
- 500: Internal Server Error
Response Format
{
"data": { ... },
"meta": {
"page": 1,
"limit": 20,
"total": 100
}
}
Error:
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Email tidak valid"
}
}
Pagination
Gunakan query params:
GET /api/articles?page=2&limit=20&sort=createdAt&order=desc
Input Validation
Selalu validasi di server, jangan percaya client:
if (!title?.trim()) {
return Response.json({ error: "Title required" }, { status: 400 });
}
API yang konsisten dan well-documented adalah tanda developer yang profesional.