AgentSkillsCN

backend-api-restful-design

REST 原则、资源命名规范、HTTP 方法。

SKILL.md
--- frontmatter
name: backend-api-restful-design
description: REST principles, resource naming, HTTP methods.

Backend API RESTful Design

When to use this skill

  • Designing APIs
  • Creating endpoints
  • Following REST standards
  • API consistency

Workflow

  • Use nouns for resources
  • Use HTTP methods correctly
  • Return proper status codes
  • Version APIs
  • Use plural nouns

Instructions

REST Principles

code
GET    /api/users      - List users
GET    /api/users/1    - Get user
POST   /api/users      - Create user
PUT    /api/users/1    - Update user
PATCH  /api/users/1    - Partial update
DELETE /api/users/1    - Delete user

Nested Resources

code
GET    /api/users/1/orders        - User's orders
POST   /api/users/1/orders        - Create order for user
GET    /api/orders/123            - Single order (flat)

Status Codes

php
<?php

return $this->respond($data, 200);        // OK
return $this->respondCreated($data);      // 201
return $this->respondNoContent();         // 204
return $this->failValidationErrors($e);   // 400
return $this->failUnauthorized();         // 401
return $this->failForbidden();            // 403
return $this->failNotFound();             // 404
return $this->failServerError($message);  // 500

Resources

  • Use nouns, not verbs
  • HTTP methods for actions
  • Proper status codes