Ensiklopedia VibeKoding: Principles of API Design: Frontend-Backend Communication Protocols.Ensiklopedia VibeKoding: Principles of API Design: Frontend-Backend Communication Protocols.
How do frontend and backend communicate efficiently? It's like asking: how should a restaurant design its menu so guests can understand it at a glance? How should waiters take orders without making mistakes? How should dishes be served to keep customers satisfied? API design solves the problem of "conversation rules."How do frontend and backend communicate efficiently? It's like asking: how should a restaurant design its menu so guests can understand it at a glance? How should waiters take orders without making mistakes? How should dishes be served to keep customers satisfied? API design solves the problem of "conversation rules."
------
Scenario 1: Inconsistent API NamingScenario 1: Inconsistent API Naming
CODE GET /getUserData GET /fetchUserInfo GET /queryUserById GET /users/query
Four endpoints, same functionality, completely different naming styles. New hires are confused: which one should I use?Four endpoints, same functionality, completely different naming styles. New hires are confused: which one should I use?
Scenario 2: Inconsistent Error HandlingScenario 2: Inconsistent Error Handling
json // Some return HTTP status codes HTTP/1.1 404 Not Found // Some return 200 + code HTTP/1.1 200 OK { "code": 404, "message": "User not found" } // Some just throw exceptions HTTP/1.1 200 OK { "error": "Something went wrong" }
The frontend doesn't know how to determine if a request was successful.The frontend doesn't know how to determine if a request was successful.
Scenario 3: Inconsistent Response StructuresScenario 3: Inconsistent Response Structures
json // Endpoint A { "data": { ... } } // Endpoint B { "result": { ... } } // Endpoint C { "content": { ... } }
Every endpoint returns a different format, requiring the frontend to handle each one individually.Every endpoint returns a different format, requiring the frontend to handle each one individually.
------
Good API design is like a restaurant's ordering system โ clear menu, standardized procedures, and informative error messages.Good API design is like a restaurant's ordering system โ clear menu, standardized procedures, and informative error messages.
------
API (Application Programming Interface) is simply the "agreement for communication between programs."API (Application Programming Interface) is simply the "agreement for communication between programs."
| Restaurant Role | Corresponding Concept | Description |
|---|---|---|
| Menu | API Documentation | Tells you what "dishes" are available |
| Waiter | HTTP Protocol | A standardized "way of communicating" |
| Kitchen | Server | Processes requests based on "orders" |
| Serving Food | Response | Returns results to the "guest" |
๐ Try it out: Click the button below to observe a complete API request-response flow:๐ Try it out: Click the button below to observe a complete API request-response flow:
------
Before diving into specific RESTful design, let's understand four major API design styles:Before diving into specific RESTful design, let's understand four major API design styles:
Many people confuse these two concepts:Many people confuse these two concepts:
| Concept | Meaning | Description |
|---|---|---|
| REST | An architectural style | A design philosophy proposed by Roy Fielding, consisting of a set of constraints |
| RESTful | Conforming to REST style | An adjective indicating that the API design follows REST principles |
Analogy:Analogy:
Six REST Constraints:Six REST Constraints:
| Constraint | Description |
|---|---|
| Client-Server Separation | Frontend and backend develop independently, interfaces are decoupled |
| Stateless | Each request contains all necessary information; the server doesn't save session state |
| Cacheable | Responses should indicate whether they are cacheable, improving performance |
| Uniform Interface | Use standard HTTP methods and status codes |
| Layered System | Clients don't need to know which layer of server they're connecting to |
| Code on Demand (optional) | The server can extend client functionality |
1. Low learning curve: The HTTP protocol itself embodies REST principles 2. Mature ecosystem: Rich tools, frameworks, and documentation 3. High versatility: Any language, any platform can call it 4. Easy to cache: GET requests are naturally cacheable, CDN-friendly1. Low learning curve: The HTTP protocol itself embodies REST principles 2. Mature ecosystem: Rich tools, frameworks, and documentation 3. High versatility: Any language, any platform can call it 4. Easy to cache: GET requests are naturally cacheable, CDN-friendly
------
REST (Representational State Transfer) is an architectural style with core principles:REST (Representational State Transfer) is an architectural style with core principles:
| Warehouse Concept | REST Equivalent | Example |
|---|---|---|
| Shelf address | URL | /users, /orders |
| Operation method | HTTP Method | GET (view), POST (add) |
| Goods | Resource | User data, order data |
Key Principle: URLs are nouns, not verbs.Key Principle: URLs are nouns, not verbs.
| Rule | Wrong Example | Correct Example | Description |
|---|---|---|---|
| Use nouns, not verbs | /getUsers | /users | URL represents resources, HTTP methods represent operations |
| Use plural form | /user | /users | Consistent plural style |
| Lowercase + hyphens | /UserProfiles | /user-profiles | URLs are case-sensitive |
| Avoid deep nesting | /a/b/c/d/e | /a/b/c | Maximum 3 levels |
| Use query params for filtering | /products/phone/5000 | /products?cat=phone | Use ? parameters for filters |
Using lowercase + hyphens (-) is the safest approach, avoiding case confusion and inconsistent underscore styles.Using lowercase + hyphens (-) is the safest approach, avoiding case confusion and inconsistent underscore styles.
| Method | Purpose | Idempotent | Safe | Typical Scenario |
|---|---|---|---|---|
| GET | Retrieve resource | Yes | Yes | Query lists, view details |
| POST | Create resource | No | No | Add user, submit order |
| PUT | Full update | Yes | No | Replace entire user profile |
| PATCH | Partial update | No | No | Only modify nickname |
| DELETE | Delete resource | Yes | No | Delete user, cancel order |
Idempotency: Multiple executions produce the same result. - Idempotent operations (GET/PUT/DELETE): Clicking 10 times produces the same result as clicking once - Non-idempotent operations (POST): Clicking 10 times might create 10 orders Solution: Use unique IDs for POST operations to prevent duplicate processing.Idempotency: Multiple executions produce the same result. - Idempotent operations (GET/PUT/DELETE): Clicking 10 times produces the same result as clicking once - Non-idempotent operations (POST): Clicking 10 times might create 10 orders Solution: Use unique IDs for POST operations to prevent duplicate processing.
------
HTTP status codes are the standard way for servers to tell clients "what happened."HTTP status codes are the standard way for servers to tell clients "what happened."
| Category | Meaning | Typical Status Codes |
|---|---|---|
| 2xx | Success | 200 OK, 201 Created, 204 No Content |
| 3xx | Redirection | 301 Permanent Move, 304 Not Modified |
| 4xx | Client Error | 400 Bad Request, 401 Unauthorized, 404 Not Found |
| 5xx | Server Error | 500 Internal Error, 503 Service Unavailable |
๐ Try it out: Click the button below to learn about common status codes:๐ Try it out: Click the button below to learn about common status codes:
------
Good error handling lets clients "understand what happened from the status code" instead of guessing.Good error handling lets clients "understand what happened from the status code" instead of guessing.
Pitfall 1: Returning 200 for All ErrorsPitfall 1: Returning 200 for All Errors
json // โ Bad practice HTTP/1.1 200 OK { "error": "Something went wrong" }
Problem: Caching layers will cache this "successful" response, and monitoring systems won't detect the issue.Problem: Caching layers will cache this "successful" response, and monitoring systems won't detect the issue.
Pitfall 2: Error Messages Too VaguePitfall 2: Error Messages Too Vague
json // โ Bad practice HTTP/1.1 400 Bad Request { "message": "Invalid parameters" }
Problem: The client doesn't know which parameter is wrong or why.Problem: The client doesn't know which parameter is wrong or why.
Pitfall 3: Exposing Sensitive InformationPitfall 3: Exposing Sensitive Information
json // โ Dangerous practice HTTP/1.1 500 Internal Server Error { "stack": "at UserService.login...", "sql": "SELECT * FROM..." }
Danger: Exposes code structure and database queries that attackers can exploit.Danger: Exposes code structure and database queries that attackers can exploit.
๐ Try it out: Compare "good" and "bad" error response designs:๐ Try it out: Compare "good" and "bad" error response designs:
------
Scenario: Your app has 1 million users, and you need to modify the order endpoint.Scenario: Your app has 1 million users, and you need to modify the order endpoint.
Without versioning:Without versioning:
Correct approach:Correct approach:
/v1/orders - Old endpoint, continues serving old apps/v1/orders - Old endpoint, continues serving old apps/v2/orders - New endpoint, new features go here/v2/orders - New endpoint, new features go here| Strategy | Example | Pros | Cons |
|---|---|---|---|
| URL Path | /v1/users | Intuitive, cacheable | Longer URLs |
| Request Header | Accept: vnd.api.v2+json | Clean URLs | Harder to debug |
| Query Parameter | /users?version=2 | Simple | Less standard |
Using the user endpoint as an example, showing v1 to v2 evolution:Using the user endpoint as an example, showing v1 to v2 evolution:
| Endpoint | v1 (Old) | v2 (New) | Change Description |
|---|---|---|---|
| Get User | GET /v1/usersReturns: name, email | GET /v2/usersReturns: name, email, avatar, phone | Added avatar and phone fields |
| Create Order | POST /v1/ordersAccepts: items[] | POST /v2/ordersAccepts: items[], coupons[] | Added coupon support |
| Batch Operations | None | POST /v2/orders/batch | Added batch creation endpoint |
- Maintain backward compatibility: Keep v1 endpoints for at least 6-12 months to give clients time to upgrade - Update documentation in sync: Each version should have its own API documentation - Deprecation notices: Announce in advance when v1 will be retired and guide migration - Monitor usage: Track v1 call volume and confirm it's safe to retire before stopping service- Maintain backward compatibility: Keep v1 endpoints for at least 6-12 months to give clients time to upgrade - Update documentation in sync: Each version should have its own API documentation - Deprecation notices: Announce in advance when v1 will be retired and guide migration - Monitor usage: Track v1 call volume and confirm it's safe to retire before stopping service
------
Response structure is the "data contract" for frontend-backend collaboration. A unified format dramatically reduces communication costs.Response structure is the "data contract" for frontend-backend collaboration. A unified format dramatically reduces communication costs.
Refer to [Google API Design Guide](https://cloud.google.com/apis/design/errors). Google requires all API error responses to include the google.rpc.Status message structure: ``json { "error": { "code": 429, "message": "Resource exhausted, please try again later", "status": "RESOURCE_EXHAUSTED", "details": [ { "@type": "type.googleapis.com/google.rpc.ErrorInfo", "reason": "RESOURCE_AVAILABILITY", "domain": "compute.googleapis.com", "metadata": { "zone": "us-east1-a", "service": "compute" } } ] } } ` Core Requirements: - Must include ErrorInfo providing machine-readable error identifiers - message is developer-facing, describing the problem and solution in concise language - details array can include LocalizedMessage, Help` (help links), etc.Refer to [Google API Design Guide](https://cloud.google.com/apis/design/errors). Google requires all API error responses to include the google.rpc.Status message structure: ``json { "error": { "code": 429, "message": "Resource exhausted, please try again later", "status": "RESOURCE_EXHAUSTED", "details": [ { "@type": "type.googleapis.com/google.rpc.ErrorInfo", "reason": "RESOURCE_AVAILABILITY", "domain": "compute.googleapis.com", "metadata": { "zone": "us-east1-a", "service": "compute" } } ] } } ` Core Requirements: - Must include ErrorInfo providing machine-readable error identifiers - message is developer-facing, describing the problem and solution in concise language - details array can include LocalizedMessage, Help` (help links), etc.
Refer to [Microsoft REST API Guidelines](https://github.com/microsoft/api-guidelines/blob/vNext/Guidelines.md). Microsoft emphasizes response consistency: Error vs. Fault Classification: - Error: Client sent invalid data, returns 4xx, doesn't affect API availability - Fault: Server cannot properly respond to a valid request, returns 5xx, affects API availability Response Header Standards: - Date: Must be returned, using RFC 5322 format (GMT timezone) - Content-Type: Must be returned - ETag: Must be returned for resources supporting optimistic concurrency controlRefer to [Microsoft REST API Guidelines](https://github.com/microsoft/api-guidelines/blob/vNext/Guidelines.md). Microsoft emphasizes response consistency: Error vs. Fault Classification: - Error: Client sent invalid data, returns 4xx, doesn't affect API availability - Fault: Server cannot properly respond to a valid request, returns 5xx, affects API availability Response Header Standards: - Date: Must be returned, using RFC 5322 format (GMT timezone) - Content-Type: Must be returned - ETag: Must be returned for resources supporting optimistic concurrency control
Refer to [Alibaba Java Development Manual](https://developer.aliyun.com/special/tech-java). Alibaba has the following API response standards: Unified Return Object: ``java public class Result`` Error Code Segmented Design: | Range | Type | Example | | :--- | :--- | :--- | | 0 | Success | 0 | | 1xxxx | Parameter Error | 10001 Missing required parameter | | 2xxxx | Business Error | 20001 Insufficient balance | | 3xxxx | Authentication Error | 30001 Not logged in | | 5xxxx | System Error | 50001 Database exception |Refer to [Alibaba Java Development Manual](https://developer.aliyun.com/special/tech-java). Alibaba has the following API response standards: Unified Return Object: ``java public class Result`` Error Code Segmented Design: | Range | Type | Example | | :--- | :--- | :--- | | 0 | Success | 0 | | 1xxxx | Parameter Error | 10001 Missing required parameter | | 2xxxx | Business Error | 20001 Insufficient balance | | 3xxxx | Authentication Error | 30001 Not logged in | | 5xxxx | System Error | 50001 Database exception |
Refer to [Stripe API Documentation](https://docs.stripe.com/api/errors). Stripe's error response design is highly refined: ``json { "error": { "type": "card_error", "code": "card_declined", "message": "Your card was declined.", "param": "number", "decline_code": "insufficient_funds", "doc_url": "https://stripe.com/docs/error-codes/card-declined" } } ` Design Highlights: - type distinguishes error types: api_error, card_error, invalid_request_error - param identifies which specific parameter has the error, frontend can directly locate form fields - doc_url provides documentation links for developers to learn more - decline_code` provides more granular error reasonsRefer to [Stripe API Documentation](https://docs.stripe.com/api/errors). Stripe's error response design is highly refined: ``json { "error": { "type": "card_error", "code": "card_declined", "message": "Your card was declined.", "param": "number", "decline_code": "insufficient_funds", "doc_url": "https://stripe.com/docs/error-codes/card-declined" } } ` Design Highlights: - type distinguishes error types: api_error, card_error, invalid_request_error - param identifies which specific parameter has the error, frontend can directly locate form fields - doc_url provides documentation links for developers to learn more - decline_code` provides more granular error reasons
Refer to [JSON:API Specification](https://jsonapi.org/format/), a widely adopted JSON API response specification in the industry: ``json { "data": { "type": "articles", "id": "1", "attributes": { "title": "JSON:API Specification Explained" }, "relationships": { "author": { "data": { "type": "users", "id": "9" } } } }, "included": [ { "type": "users", "id": "9", "attributes": { "name": "John Doe" } } ] } ` Core Design: - data contains the primary resource, must have type and id - attributes stores resource attributes - relationships describes resource associations - included` avoids repeated requests by returning related data at onceRefer to [JSON:API Specification](https://jsonapi.org/format/), a widely adopted JSON API response specification in the industry: ``json { "data": { "type": "articles", "id": "1", "attributes": { "title": "JSON:API Specification Explained" }, "relationships": { "author": { "data": { "type": "users", "id": "9" } } } }, "included": [ { "type": "users", "id": "9", "attributes": { "name": "John Doe" } } ] } ` Core Design: - data contains the primary resource, must have type and id - attributes stores resource attributes - relationships describes resource associations - included` avoids repeated requests by returning related data at once
Refer to [GitHub REST API Documentation](https://docs.github.com/en/rest). GitHub's response design emphasizes developer experience: Success Response: ``json { "id": 1296269, "node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5", "name": "Hello-World", "full_name": "octocat/Hello-World", "owner": { "login": "octocat", "id": 1, "avatar_url": "https://github.com/images/error/octocat_happy.gif" }, "private": false, "html_url": "https://github.com/octocat/Hello-World" } ` Error Response: `json { "message": "Bad credentials", "documentation_url": "https://docs.github.com/rest" } ` Design Highlights: - Response includes multiple URL formats (html_url, url) for different scenarios - Error response includes documentation_url pointing to docs - Uses Link` response header for pagination navigationRefer to [GitHub REST API Documentation](https://docs.github.com/en/rest). GitHub's response design emphasizes developer experience: Success Response: ``json { "id": 1296269, "node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5", "name": "Hello-World", "full_name": "octocat/Hello-World", "owner": { "login": "octocat", "id": 1, "avatar_url": "https://github.com/images/error/octocat_happy.gif" }, "private": false, "html_url": "https://github.com/octocat/Hello-World" } ` Error Response: `json { "message": "Bad credentials", "documentation_url": "https://docs.github.com/rest" } ` Design Highlights: - Response includes multiple URL formats (html_url, url) for different scenarios - Error response includes documentation_url pointing to docs - Uses Link` response header for pagination navigation
Refer to [Twitter API v2 Documentation](https://developer.twitter.com/en/docs/twitter-api). Twitter API v2 uses a concise response format: ``json { "data": { "id": "1460323737035677698", "text": "Hello, Twitter!" }, "includes": { "users": [ { "id": "2244994945", "name": "Twitter Dev", "username": "TwitterDev" } ] } } ` Design Highlights: - data contains primary data, includes contains related data (similar to JSON:API) - Supports field selection: ?tweet.fields=created_at,public_metrics - Pagination uses next_token and previous_token`Refer to [Twitter API v2 Documentation](https://developer.twitter.com/en/docs/twitter-api). Twitter API v2 uses a concise response format: ``json { "data": { "id": "1460323737035677698", "text": "Hello, Twitter!" }, "includes": { "users": [ { "id": "2244994945", "name": "Twitter Dev", "username": "TwitterDev" } ] } } ` Design Highlights: - data contains primary data, includes contains related data (similar to JSON:API) - Supports field selection: ?tweet.fields=created_at,public_metrics - Pagination uses next_token and previous_token`
Combining the above specifications, response structure design should follow these principles:Combining the above specifications, response structure design should follow these principles:
data is the core of the response, and its design directly impacts frontend development efficiency.data is the core of the response, and its design directly impacts frontend development efficiency.
- [Google API Design Guide - Errors](https://cloud.google.com/apis/design/errors) - [Microsoft REST API Guidelines](https://github.com/microsoft/api-guidelines) - [Alibaba Java Development Manual](https://developer.aliyun.com/special/tech-java) - [Heroku HTTP API Design Guide](https://github.com/interagent/http-api-design) - [Stripe API - Errors](https://docs.stripe.com/api/errors) - [JSON:API Specification](https://jsonapi.org/format/)- [Google API Design Guide - Errors](https://cloud.google.com/apis/design/errors) - [Microsoft REST API Guidelines](https://github.com/microsoft/api-guidelines) - [Alibaba Java Development Manual](https://developer.aliyun.com/special/tech-java) - [Heroku HTTP API Design Guide](https://github.com/interagent/http-api-design) - [Stripe API - Errors](https://docs.stripe.com/api/errors) - [JSON:API Specification](https://jsonapi.org/format/)
------
CODE # User Module GET /v1/users # Get user list POST /v1/users # Create new user GET /v1/users/{id} # Get user details PUT /v1/users/{id} # Full update user PATCH /v1/users/{id} # Partial update user DELETE /v1/users/{id} # Delete user # Order Module GET /v1/users/{id}/orders # Get orders for a user POST /v1/orders # Create order GET /v1/orders/{id} # Get order details PATCH /v1/orders/{id}/status # Update order status # Product Module (use query params for complex filtering) GET /v1/products?category=phone&price_max=5000&sort=price_desc&page=1
------
AI can help you quickly generate specification-compliant API designs. The key is providing clear context and constraints.AI can help you quickly generate specification-compliant API designs. The key is providing clear context and constraints.
CODE You are a senior backend architect, proficient in RESTful API design. Please help me design a set of API endpoints. ## Business Background [Describe your business scenario, e.g., e-commerce system, blog platform, task management, etc.] ## Functional Requirements [List the required functional modules, e.g.: - User management: registration, login, personal information - Order management: create order, query orders, cancel order - Product management: product list, product details, search] ## Design Requirements 1. Follow RESTful conventions 2. URLs use plural nouns, lowercase + hyphens 3. Use HTTP methods correctly (GET/POST/PUT/PATCH/DELETE) 4. Unified response format: { code, message, data, request_id } 5. Appropriate status code usage 6. Versioning: URL path approach (/v1/) ## Output Format Please output in the following format: ### Endpoint List | Method | URL | Description | Request Body | Response Body | |--------|-----|-------------|--------------|---------------| ### Request/Response Examples [Detailed examples for key endpoints] ### Status Code Descriptions [Status codes used and their meanings]
Input Prompt:Input Prompt:
CODE You are a senior backend architect, proficient in RESTful API design. Please help me design a set of API endpoints for an e-commerce order system. ## Business Background A B2C e-commerce platform where users can browse products, place orders, and view order status. ## Functional Requirements - Order module: create order, query order list, query order details, cancel order, pay order - Cart module: add product, modify quantity, remove product, view cart ## Design Requirements 1. Follow RESTful conventions 2. URLs use plural nouns, lowercase + hyphens 3. Use HTTP methods correctly 4. Unified response format 5. Versioning: /v1/
AI Output Example:AI Output Example:
| Method | URL | Description |
|---|---|---|
POST | /v1/orders | Create order |
GET | /v1/orders | Query order list |
GET | /v1/orders/{id} | Query order details |
PATCH | /v1/orders/{id}/status | Update order status (cancel/pay) |
GET | /v1/users/{id}/cart | Get cart |
POST | /v1/users/{id}/cart/items | Add product to cart |
PATCH | /v1/users/{id}/cart/items/{itemId} | Modify cart item quantity |
DELETE | /v1/users/{id}/cart/items/{itemId} | Remove cart item |
| Note | Description |
|---|---|
| Provide complete context | Business background, user roles, and data relationships should all be clearly stated |
| Define constraints clearly | Naming conventions, versioning strategy, and response format should be defined upfront |
| Iterate and refine | The first output may not be perfect; ask follow-up questions and request modifications |
| Manual review | AI-generated content needs human verification against business requirements |
| Cover edge cases | Ask AI to consider error handling, permission control, pagination, and other edge cases |
- "Please add error response examples for each endpoint" - "Please consider pagination, sorting, and filtering parameters" - "Please add permission control descriptions for the endpoints" - "Please check if it follows RESTful best practices"- "Please add error response examples for each endpoint" - "Please consider pagination, sorting, and filtering parameters" - "Please add permission control descriptions for the endpoints" - "Please check if it follows RESTful best practices"
------
| Term | English | Explanation |
|---|---|---|
| API | Application Programming Interface | Agreement for communication between programs |
| REST | Representational State Transfer | An architectural style that uses URLs to identify resources |
| Resource | Resource | Core concept in REST architecture, has unique identifiers (URLs) |
| Idempotency | Idempotency | Multiple executions produce the same result |
| Status Code | Status Code | Response status defined by the HTTP protocol |
| Versioning | Versioning | Allows old and new APIs to coexist for smooth upgrades |
| Request Body | Request Body | Data carried by POST/PUT/PATCH requests |
| Response Body | Response Body | Data returned by the server |
| Header | Header | Metadata for requests/responses (e.g., Content-Type) |
| Authentication | Authentication | Verifying "who you are" (login, Token) |
| Authorization | Authorization | Verifying "what you can do" (permissions) |