APIs are contracts.
If the contract is unclear, things break — slowly or suddenly.
This API design covers basics that make your APIs easier to use, safer to change, and harder to break.
1️⃣ Use HTTP Status Codes Properly
Status codes are not optional metadata — they’re signals.
❌ Bad
200 OK
{
"error": "User not found"
}
✅ Good
404 Not Found
Common codes to remember:
200 → success
201 → created
400 → bad request
401 → unauthorized
403 → forbidden
404 → not found
500 → server error
2️⃣ Keep URLs Simple and Clear
URLs should describe resources, not actions.
❌ Bad
/getUser
/createOrder
✅ Good
GET /users/{id}
POST /orders
Let HTTP methods do the work.
3️⃣ Be Consistent With Responses
Inconsistent responses confuse clients.
❌ Bad
{ "msg": "ok" }
{ "success": true }
✅ Good
{
"data": {...},
"meta": {...}
}
Consistency makes APIs predictable.
4️⃣ Design Errors Like a Feature
Errors should be helpful, not mysterious.
❌ Bad
{ "error": "Something went wrong" }
✅ Good
{
"error": {
"code": "INVALID_INPUT",
"message": "Email format is invalid"
}
}
Clear errors reduce support and debugging time.
5️⃣ Always Paginate Lists
Large responses hurt performance.
❌ Bad
GET /users
✅ Good
GET /users?page=1&limit=20
Response:
{
"data": [...],
"page": 1,
"limit": 20,
"total": 250
}
Pagination protects your API and database.
6️⃣ Version Your API Early
Breaking changes happen — plan for them.
✅ Good
/api/v1/users
/api/v2/users
Versioning lets clients upgrade safely.
7️⃣ Don’t Overload One Endpoint
One endpoint should do one thing well.
❌ Bad
POST /users?action=createAndSendEmailAndLog
✅ Good
POST /users
POST /emails
Simple endpoints are easier to test and maintain.
8️⃣ Secure by Default
Never assume clients behave correctly.
Basics to follow:
➡️ Validate inputs
➡️ Authenticate every request
➡️ Authorize every action
➡️ Rate limit public endpoints
Security is part of API design, not an add-on.
9️⃣ Document Everything
APIs without docs slow everyone down.
Use tools like:
➡️ OpenAPI / Swagger
Docs are part of your API.
🎯 Final Thoughts
Good API design is not about being clever.
It’s about being clear, predictable, and boring.
And that’s a good thing.
Top comments (0)