一键导入
api-endpoint-creation
Guide for creating RESTful API endpoints with validation and error handling. Use when implementing backend API routes.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Guide for creating RESTful API endpoints with validation and error handling. Use when implementing backend API routes.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | api-endpoint-creation |
| description | Guide for creating RESTful API endpoints with validation and error handling. Use when implementing backend API routes. |
Follow this process to create robust API endpoints:
// Example: Express.js endpoint
import { Router } from 'express';
import { z } from 'zod';
const router = Router();
router.post('/api/users', async (req, res) => {
try {
// 1. Validate input
// 2. Business logic
// 3. Database operation
// 4. Return response
} catch (error) {
// 5. Error handling
}
});
Use Zod for schema validation:
const createUserSchema = z.object({
email: z.string().email(),
name: z.string().min(2).max(100),
age: z.number().int().positive().optional(),
});
// In route handler
const validatedData = createUserSchema.parse(req.body);
Use appropriate status codes:
200 OK - Successful GET, PUT, PATCH201 Created - Successful POST204 No Content - Successful DELETE400 Bad Request - Validation error401 Unauthorized - Authentication required403 Forbidden - No permission404 Not Found - Resource not found409 Conflict - Resource conflict500 Internal Server Error - Server errorConsistent JSON response structure:
// Success
res.status(201).json({
data: user,
message: 'User created successfully',
});
// Error
res.status(400).json({
error: 'Validation failed',
details: validationErrors,
});
Centralized error handler:
class ApiError extends Error {
constructor(public statusCode: number, message: string) {
super(message);
}
}
// In route
if (!user) {
throw new ApiError(404, 'User not found');
}
// Error middleware
app.use((err, req, res, next) => {
if (err instanceof ApiError) {
return res.status(err.statusCode).json({ error: err.message });
}
res.status(500).json({ error: 'Internal server error' });
});
GET /api/users - List all users
POST /api/users - Create new user
GET /api/users/:id - Get single user
PUT /api/users/:id - Update user (full)
PATCH /api/users/:id - Update user (partial)
DELETE /api/users/:id - Delete user
For list endpoints:
router.get('/api/users', async (req, res) => {
const page = parseInt(req.query.page as string) || 1;
const limit = parseInt(req.query.limit as string) || 10;
const offset = (page - 1) * limit;
const [users, total] = await Promise.all([
db.user.findMany({ skip: offset, take: limit }),
db.user.count(),
]);
res.json({
data: users,
pagination: {
page,
limit,
total,
totalPages: Math.ceil(total / limit),
},
});
});
Checklist for auditing and fixing accessibility issues (WCAG 2.1 AA compliance). Use when reviewing components or pages for accessibility.
Guide for implementing secure authentication with JWT and session management. Use when adding authentication to an application.
Guide for creating responsive layouts with CSS. Use when implementing responsive design or fixing layout issues.
Guide for designing database schemas with Prisma ORM. Use when creating or modifying database models.
Guide for consistent error handling across frontend and backend. Use when implementing error handling logic.
Guide for implementing form handling with React Hook Form and Zod validation. Use when creating forms with validation.