Why API Design Matters
A well-designed API is a product in itself. It affects developer experience, integration speed, and long-term maintainability. Here are the principles we follow at DevFlow.
An API is also a contract that is hard to change: once other teams, partners or mobile apps depend on it, every breaking change costs them time. The decisions below are cheap to get right at the start and expensive to fix later.
URL Design Principles
Resource Naming
Use nouns, not verbs. The HTTP method specifies the action.
# Good
GET /api/v1/users
POST /api/v1/users
GET /api/v1/users/:id
# Bad
GET /api/v1/getUsers
POST /api/v1/createUser
- Use plural nouns for collections (
/users) and an identifier for a single item (/users/123). - Express relationships with nesting, but only one level deep:
/users/123/ordersis clear,/users/123/orders/456/items/789is not. - Pick one naming style for paths and fields (for example, lowercase paths and camelCase JSON) and use it everywhere.
Use HTTP Methods Correctly
| Method | Purpose | Safe to retry? |
|---|---|---|
| GET | Read a resource or collection | Yes |
| POST | Create a resource or start an action | Only with an idempotency key |
| PUT | Replace a resource completely | Yes |
| PATCH | Update part of a resource | Depends on the change |
| DELETE | Remove a resource | Yes |
Networks fail, and clients retry. For operations that must not happen twice, such as payments or order creation, accept an Idempotency-Key header: the server stores the result for that key and returns the same response if the request is repeated.
Consistent Response Format
Every API endpoint should return a consistent format so consumers know what to expect.
{
"status": "success",
"data": { "id": "user-123", "name": "John Doe" },
"pagination": { "page": 2, "limit": 20, "total": 150 }
}
Use ISO 8601 for dates and times (with a time zone), return identifiers as strings, and never change the type of an existing field.
HTTP Status Codes
| Code | Usage |
|---|---|
| 200 | Successful GET, PUT, PATCH |
| 201 | Successful POST (resource created) |
| 204 | Successful DELETE (no content) |
| 400 | Validation failed |
| 401 | Authentication required |
| 403 | Insufficient permissions |
| 404 | Resource not found |
| 429 | Rate limit exceeded |
Error Handling
An error response should tell the client what went wrong and what to do about it. The Problem Details standard (RFC 9457) defines a common JSON format that many tools already understand:
{
"type": "https://api.example.com/errors/validation",
"title": "Validation failed",
"status": 400,
"detail": "The request contains invalid fields.",
"errors": [{ "field": "email", "message": "Must be a valid email address" }]
}
Return every validation error at once rather than one per request, use stable error codes that clients can rely on, and never expose stack traces or internal details in production.
Pagination Patterns
# Cursor-based (recommended for large datasets)
GET /api/v1/posts?cursor=eyJpZCI6MTAwfQ&limit=20
# Offset-based (simpler, good for small datasets)
GET /api/v1/posts?page=3&limit=20
Cursor-based pagination stays fast on large tables and does not skip or repeat items when records are added while a client is paging. Always set a maximum page size, and return the cursor for the next page in the response.
Support filtering and sorting with simple, documented query parameters, for example ?status=active&sort=-createdAt, and only on fields that are indexed.
Versioning and Deprecation
Version in the URL (/api/v1) or in a header, and make only additive changes within a version: new endpoints and new optional fields are fine; removing or renaming fields is not. When a version must be retired, announce it early, signal it in responses with the Sunset header (RFC 8594), and track which clients still call it before switching it off.
Security and Rate Limiting
- Use HTTPS only, and authenticate with OAuth 2.0 or scoped API keys rather than shared credentials.
- Check authorization on every request, at the level of the individual object: a valid token for one customer must never read another customer's data.
- Validate all input against a schema and reject unknown fields.
- Rate-limit per client and return
429with aRetry-Afterheader, so well-behaved clients know when to try again.
Documentation
Describe the API in an OpenAPI specification and generate the reference documentation from it, so documentation and implementation cannot drift apart. Include real request and response examples, explain authentication step by step, and keep a changelog.
API Design Checklist
- Nouns for resources, HTTP methods for actions, one naming style
- Idempotency keys for operations that must not repeat
- Consistent responses and Problem Details for errors
- Cursor pagination with a maximum page size
- Additive changes within a version; deprecation announced in advance
- Object-level authorization, input validation and rate limiting
- OpenAPI specification and generated documentation
Conclusion
Good API design is an investment that pays dividends throughout the product lifecycle. Follow conventions, be consistent, and document everything.
Need an API designed or reviewed? See our web development services or talk to our team.