API Design
API Design
Phần tiêu đề “API Design”Best practices trong việc thiết kế APIs.
RESTful Principles
Phần tiêu đề “RESTful Principles”GET /api/users # List usersGET /api/users/123 # Get user 123POST /api/users # Create userPUT /api/users/123 # Update user 123DELETE /api/users/123 # Delete user 123HTTP Status Codes
Phần tiêu đề “HTTP Status Codes”200 OK # Success201 Created # Resource created400 Bad Request # Invalid input401 Unauthorized # Not authenticated403 Forbidden # Not authorized404 Not Found # Resource not found500 Internal Error # Server errorResponse Format
Phần tiêu đề “Response Format”// Success response{ "data": { "id": 1, "name": "Phi" }}
// Error response{ "error": { "message": "User not found", "code": "USER_NOT_FOUND" }}
// Pagination{ "data": [...], "meta": { "page": 1, "perPage": 20, "total": 100 }}Versioning
Phần tiêu đề “Versioning”/api/v1/users/api/v2/usersAuthentication
Phần tiêu đề “Authentication”Authorization: Bearer eyJhbGciOiJIUzI1NiIs...Best Practices
Phần tiêu đề “Best Practices”- Nouns, not verbs trong URLs
- Plural names -
/usersnot/user - Nested resources -
/users/123/posts - Filtering & Sorting -
/users?status=active&sort=name - Pagination -
/users?page=2&limit=20 - Versioning -
/api/v1/