API Design Principles - Complete Interview Guide with Spring Boot Examples
Master REST API design principles with production-ready examples, REST resource design, HTTP methods, versioning, pagination, filtering, error handling, security, observability, Spring Boot best practices, and 15 interview questions with detailed answers.
Introduction
Designing a REST API is much more than exposing endpoints.
A well-designed API should be:
- Easy to understand
- Consistent
- Secure
- Scalable
- Backward compatible
- Easy to maintain
- Well documented
- High performing
- Observable
- Developer friendly
Poor API design leads to:
- Breaking client applications
- Difficult integrations
- Increased maintenance cost
- Performance issues
- Security vulnerabilities
- Poor developer experience
Good API design allows applications to evolve for years without affecting existing consumers.
This guide covers the most important API design principles used by companies such as Google, Microsoft, Amazon, Stripe, Netflix, PayPal, and many enterprise organizations.
What Are API Design Principles?
API design principles are a collection of guidelines that help developers build APIs that are:
- Predictable
- Reusable
- Extensible
- Consistent
- Easy to test
- Easy to document
- Secure
- Production ready
The goal is to create APIs that remain stable even as business requirements evolve.
Principle 1: Resource-Oriented Design
REST APIs should expose resources (nouns) rather than actions (verbs).
Good
GET /customers
GET /customers/101
POST /customers
PUT /customers/101
DELETE /customers/101
Bad
GET /getCustomer
POST /createCustomer
POST /deleteCustomer
POST /updateCustomer
Resources represent business entities such as:
- Customers
- Orders
- Payments
- Products
- Accounts
Principle 2: Use HTTP Methods Correctly
| Method | Purpose |
|---|---|
| GET | Read data |
| POST | Create resources |
| PUT | Replace a resource |
| PATCH | Partially update |
| DELETE | Remove a resource |
Using proper HTTP semantics improves caching, security, interoperability, and readability.
Principle 3: Consistent URL Design
Use consistent naming conventions.
Example:
/api/v1/customers
/api/v1/orders
/api/v1/products
/api/v1/payments
Recommendations:
- Use lowercase URLs
- Use plural resource names
- Avoid verbs
- Use hyphens instead of underscores when needed
- Keep URLs short and meaningful
Principle 4: Version Your APIs
APIs evolve over time.
Instead of breaking existing clients, publish a new version.
Example:
/api/v1/customers
/api/v2/customers
Common versioning strategies:
- URI versioning
- Header versioning
- Media type versioning
URI versioning is the most common in enterprise systems.
Principle 5: Design Consistent Request and Response Models
Use a consistent JSON structure.
Example response:
{
"customerId": 101,
"name": "Venu",
"email": "[email protected]"
}
Maintain consistent:
- Field naming
- Data types
- Date formats
- Boolean values
- Null handling
Principle 6: Use Proper HTTP Status Codes
Examples:
| Status | Meaning |
|---|---|
| 200 | Success |
| 201 | Resource created |
| 202 | Accepted |
| 204 | No content |
| 400 | Bad request |
| 401 | Unauthorized |
| 403 | Forbidden |
| 404 | Not found |
| 409 | Conflict |
| 422 | Validation error |
| 500 | Internal server error |
Avoid always returning 200 OK for every response.
Principle 7: Standardize Error Responses
A good error response should contain:
- Timestamp
- Status
- Error code
- Message
- Path
- Trace ID (optional)
Example:
{
"timestamp": "2026-07-20T10:30:00Z",
"status": 404,
"code": "CUSTOMER_NOT_FOUND",
"message": "Customer 101 not found.",
"path": "/api/v1/customers/101"
}
Never expose stack traces or internal implementation details.
Principle 8: Support Pagination, Filtering, and Sorting
Pagination:
GET /customers?page=0&size=20
Filtering:
GET /customers?status=ACTIVE
Sorting:
GET /customers?sort=name,asc
Searching:
GET /customers?keyword=venu
These features improve scalability and user experience.
Principle 9: Secure Your APIs
Production APIs should implement:
- HTTPS
- OAuth 2.0
- JWT authentication
- Authorization
- Input validation
- Rate limiting
- Encryption
- Audit logging
Security should be built into the design from the beginning.
Principle 10: Design for Observability
Modern APIs should expose operational insights.
Include:
- Trace IDs
- Correlation IDs
- Structured logging
- Metrics
- Distributed tracing
- Health endpoints
Example:
Trace-Id: 9a7d5b1c
Correlation-Id: 41fd8b90
These identifiers help troubleshoot requests across multiple microservices.
Principle 11: Maintain Backward Compatibility
Avoid breaking existing clients.
Instead of removing fields:
- Add optional fields
- Introduce new endpoints
- Deprecate old APIs
- Release a new version
Backward compatibility reduces client migration effort.
Principle 12: Document Your APIs
Every production API should provide clear documentation.
Popular standards include:
- OpenAPI Specification
- Swagger UI
Documentation should describe:
- Endpoints
- Request models
- Response models
- Authentication
- Error codes
- Examples
Spring Boot Production Example
Project structure:
src
├── controller
├── service
├── repository
├── dto
├── entity
├── exception
├── config
└── security
Sample endpoint:
@RestController
@RequestMapping("/api/v1/customers")
public class CustomerController {
@GetMapping("/{id}")
public CustomerResponse getCustomer(
@PathVariable Long id) {
return customerService.getCustomer(id);
}
}
This follows:
- Resource-oriented URLs
- Versioning
- Proper HTTP methods
- Layered architecture
Production Best Practices
- Keep APIs stateless.
- Use resource-oriented URLs.
- Validate all input.
- Return meaningful status codes.
- Standardize error responses.
- Use API versioning.
- Support pagination.
- Design idempotent POST APIs where appropriate.
- Secure APIs using OAuth2/JWT.
- Log Trace IDs and Correlation IDs.
- Document APIs using OpenAPI.
- Monitor latency, throughput, and error rates.
- Maintain backward compatibility.
- Avoid exposing internal database models.
- Write comprehensive API tests.
Common API Design Mistakes
- Using verbs in URLs
- Ignoring HTTP semantics
- No versioning strategy
- Inconsistent naming conventions
- Returning excessive data
- Missing pagination
- Exposing internal entities
- Poor validation
- Inconsistent error responses
- Breaking backward compatibility
- Missing authentication and authorization
- Poor documentation
15 Interview Questions and Answers
The following interview questions cover the most frequently asked API design topics in senior Java, Spring Boot, and Microservices interviews:
- What are API design principles?
- Why is consistency important in REST APIs?
- What are REST resource naming best practices?
- How should HTTP methods be used correctly?
- How should API versioning be implemented?
- Why should APIs avoid breaking changes?
- How should pagination be implemented?
- How should filtering, sorting, and searching be designed?
- What makes a good API error response?
- How should production APIs be secured?
- What is HATEOAS and when should it be used?
- How should APIs be optimized for performance?
- How should APIs be observable in production?
- What are common API design mistakes?
- How would you design a production-ready REST API?
(Use the detailed interview answers from the companion Interview Questions section.)
Quick Revision
| Principle | Summary |
|---|---|
| Resource Design | Use nouns, not verbs |
| HTTP Methods | Follow REST semantics |
| URL Design | Consistent and predictable |
| Versioning | Protect existing clients |
| Status Codes | Return meaningful HTTP responses |
| Error Handling | Standardized error model |
| Pagination | Improve scalability |
| Security | OAuth2, JWT, HTTPS |
| Observability | Trace IDs, metrics, logs |
| Documentation | OpenAPI/Swagger |
| Compatibility | Avoid breaking changes |
| Performance | Optimize payloads and queries |
Key Takeaways
- Design APIs around business resources.
- Keep URLs simple and consistent.
- Follow HTTP standards.
- Version APIs from the beginning.
- Standardize responses and error handling.
- Implement security as a core design principle.
- Support pagination, filtering, and sorting.
- Design APIs for observability and monitoring.
- Maintain backward compatibility.
- Document every API using OpenAPI/Swagger.
- Prioritize developer experience and long-term maintainability.