- Client-Server: Separation of concerns for modularity.
- Statelessness: Each request is self-contained, with no server-side session data.
- Cacheability: Responses can be cached to improve performance.
- Layered System: Components are independent and replaceable.
- Code on Demand (optional): Servers can extend client functionality by sending executable code.
- Uniform Interface: Standardized interaction with resources.
- GET: Retrieve resources.
- POST: Create new resources or submit data.
- PUT: Update or replace existing resources.
- PATCH: Partially modify existing resources.
- DELETE: Remove resources.
- HEAD: Retrieve headers only.
- OPTIONS: Get information about available communication options for a resource.
- 2xx (Success):
- 200 OK: The request was successful.
- 201 Created: The resource was successfully created.
- 3xx (Redirection):
- 301 Moved Permanently: The resource was moved to a new URI.
- 4xx (Client Error):
- 401 Unauthorized: Authentication is required or has failed.
- 403 Forbidden: The server understands the request but refuses to authorize it.
- 404 Not Found: The requested resource could not be found.
- 5xx (Server Error):
- 500 Internal Server Error: A generic error occurred on the server side.
- Authentication: Use OAuth 2.0, JWT (JSON Web Tokens).
- Authorization: Implement RBAC (Role-Based Access Control) or ABAC (Attribute-Based Access Control).
- HTTPS: Use TLS/SSL for encrypting communications.
- Input Validation: Always validate and sanitize input data to prevent injection attacks.
- Rate Limiting and Throttling: Implement limits to avoid abuse.
- CORS (Cross-Origin Resource Sharing): Control access from different origins.
- Security Headers: Use headers like Content-Security-Policy and X-Frame-Options for mitigating web vulnerabilities.
- Nouns: Use nouns for resource names (e.g.,
/users
,/products
). - Pluralization: Use plural nouns for collections (e.g.,
/users
). - Hyphens: Use hyphens for readability (e.g.,
/product-categories
). - Lowercase: Use lowercase letters consistently.
- Versioning: Use version numbers in the URI (e.g.,
/v1/users
). - Filtering and Sorting: Apply query parameters for filtering and sorting (e.g.,
/users?status=active&sort=name,asc
). - Pagination: Use limit and offset parameters for large datasets.
- Error Handling: Return clear and consistent error codes and messages.
- Documentation: Use tools like OpenAPI (Swagger) for comprehensive API documentation.
- Caching: Implement server-side and client-side caching for better performance.