API Design Patterns: Building APIs That Don’t Suck
An API is often the front door to your application or service. If it’s confusing, inconsistent, or poorly designed, developers will avoid using it—or worse, misuse it.
This guide distills years of backend experience and real-world API lessons into design patterns that make your API a joy to use.
1. Resource-Oriented Design (ROA)
Design your APIs around nouns (resources), not verbs:
✅ GET /users/42
✅ POST /users
❌ POST /createUser
Benefits:
- Predictable URLs
- Consistent behavior
- Easier to cache and document
2. Use HTTP Semantics Properly
Method | Purpose |
---|---|
GET | Read resource |
POST | Create resource |
PUT | Replace resource |
PATCH | Update resource |
DELETE | Delete resource |
Examples:
GET /posts/123
PATCH /posts/123
DELETE /posts/123
Avoid tunneling everything through POST.
3. Consistent Naming Conventions
- Use snake_case or camelCase, but never both
- Avoid abbreviations:
created_at
, notcrtd_dt
- Plural nouns for collections:
/users
, not/user
4. Embrace Versioning
Version your APIs from day one:
GET /v1/users
Avoid breaking changes. If needed, bump the version.
5. Standardized Error Responses
Your errors should be structured and informative:
{
"error": {
"code": "INVALID_INPUT",
"message": "Email format is invalid.",
"details": {
"field": "email"
}
}
}
6. Pagination, Filtering, and Sorting
For large collections, always support:
GET /posts?page=2&limit=10&sort=-created_at&filter[author]=hasan
Best Practices:
- Use cursor-based pagination if performance matters
- Allow filtering on common fields
- Allow multi-sort fields if possible
7. Idempotency
Ensure clients can safely retry operations:
- GET, PUT, DELETE are naturally idempotent
- For POST, allow
Idempotency-Key
headers:
POST /payments
Idempotency-Key: abc123
8. Hypermedia as the Engine of Application State (HATEOAS)
Provide discoverable APIs with links:
{
"id": 123,
"title": "REST Patterns",
"_links": {
"self": { "href": "/posts/123" },
"author": { "href": "/users/42" }
}
}
While not mandatory, this improves UX and integration.
9. OpenAPI & Documentation First
Design your API using OpenAPI specs before implementation.
- Enables early feedback
- Auto-generate docs, SDKs, clients
- Standardizes across teams
Tools: Swagger UI, Stoplight, Redocly
10. Authentication & Authorization
Use industry standards:
- OAuth 2.0 or JWT for auth
- API keys for internal use only
- Role-based access control (RBAC)
Example:
Authorization: Bearer eyJhbGciOi...
Bonus: Make it Developer-Friendly
- Provide
curl
and Postman examples - Return helpful validation errors
- Always include status codes, e.g.,
200 OK
,400 Bad Request
- Use consistent response shapes:
{
"data": {...},
"meta": {...},
"errors": [...]
}
Conclusion
Good API design doesn’t require genius—just empathy and discipline. These patterns aim to reduce friction for your users while keeping your system maintainable and evolvable.
Which pattern do you wish more APIs followed? Let’s talk on LinkedIn.