fastapi-endpoint
它能做什么
这项技能指导你的智能体为现有的 FastAPI 项目添加新的 API 接口。适合希望路由经过充分测试的 Python 后端开发人员。
适用场景
- ✓需要为现有 FastAPI 项目添加增、查、改、删路由的时候。
- ✓某个接口必须通过依赖注入进行身份验证保护的时候。
- ✓想为新的 API 路由编写 pytest 测试的时候。
技能链接
https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/web-development/fastapi-endpoint/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请在我的 FastAPI 应用中添加一个受保护的接口,用于列出发票,并附上测试。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体先了解项目的结构和模式,规划数据模型和登录方式,然后编写接口和测试。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
FastAPI Endpoint Builder
When to use
Use this skill when you need to:
- Add new API endpoints to an existing FastAPI project
- Build CRUD operations with proper validation and error handling
- Set up authenticated endpoints with dependency injection
- Create async database queries with SQLAlchemy 2.0
- Generate complete test coverage for API routes
Phase 1: Explore (Plan Mode)
Enter plan mode. Before writing any code, explore the existing project to understand:
Project structure
- Find the FastAPI app entry point (
main.py,app.py, orapp/__init__.py) - Identify the router organization pattern (single file vs
routers/directory) - Check for existing
models/,schemas/,crud/, orservices/directories - Look at
pyproject.tomlorrequirements.txtfor installed dependencies
Existing patterns
- How are existing endpoints structured? (function-based vs class-based)
- What ORM is used? (SQLAlchemy 2.0 async, Tortoise, raw SQL, none)
- How is the database session managed? (
Depends(get_db), middleware, other) - What auth pattern exists? (OAuth2PasswordBearer, API key header, custom)
- Are there existing Pydantic base models or shared schemas?
- What response format is standard? (direct model, wrapped
{"data": ..., "meta": ...})
Test patterns
- Where do tests live? (
tests/,test_*.py,*_test.py) - What test client is used? (httpx AsyncClient, TestClient, pytest-asyncio)
- Are there test fixtures for database and auth?
Phase 2: Interview (AskUserQuestion)
Use AskUserQuestion to clarify requirements. Ask in rounds — do NOT dump all questions at once.
Round 1: Core endpoint
Question: "What resource does this endpoint manage?"
Header: "Resource"
Options:
- "New resource (I'll describe the fields)" — Creating a new data model from scratch
- "Existing model (extend it)" — Adding endpoints for a model that already exists in the codebase
- "Relationship endpoint (nested)" — e.g., /users/{id}/orders — endpoint on a related resource
Question: "Which HTTP methods do you need?"
Header: "Methods"
multiSelect: true
Options:
- "Full CRUD (GET list, GET detail, POST, PUT/PATCH, DELETE)" — All standard operations
- "Read-only (GET list + GET detail)" — No mutations
- "Custom action (POST /resource/{id}/action)" — Business logic endpoint, not standard CRUD
Round 2: Data model (if new resource)
Question: "What fields does the resource have? (describe briefly)"
Header: "Fields"
Options:
- "Simple (< 6 fields, basic types)" — Strings, ints, booleans, dates
- "Medium (6-15 fields, some relations)" — Includes foreign keys or enums
- "Complex (nested objects, polymorphic)" — JSON fields, discriminated unions, computed fields
Round 3: Auth and access control
Question: "How should this endpoint be authenticated?"
Header: "Auth"
Options:
- "JWT Bearer token (Recommended)" — OAuth2PasswordBearer with JWT decode
- "API Key header" — X-API-Key header validation
- "No auth (public)" — Open endpoint, no authentication required
- "Use existing auth" — Reuse the auth dependency already in the project
Question: "Do you need role-based access control?"
Header: "RBAC"
Options:
- "No — any authenticated user" — Single permission level
- "Yes — role check (admin, user, etc.)" — Require specific roles per endpoint
- "Yes — ownership check" — Users can only access their own resources
Round 4: Pagination, filtering, caching
Question: "What pagination style for list endpoints?"
Header: "Pagination"
Options:
- "Cursor-based (Recommended)" — Best for real-time data, no offset drift
- "Offset/limit" — Simple, good for admin panels with page numbers
- "No pagination" — Small datasets, return all results
Question: "Do you need response caching?"
Header: "Caching"
Options:
- "No caching" — Fresh data on every request
- "Cache-Control headers" — Client-side caching via HTTP headers
- "Redis/in-memory cache" — Server-side caching with TTL
Phase 3: Plan (ExitPlanMode)
Write a concrete implementation plan covering:
- Files to create/modify — exact paths based on project structure discovered in Phase 1
- Pydantic schemas —
Create,Update,Response, andListschemas with field types - SQLAlchemy model — table name, columns, relationships, indexes
- CRUD/service layer — async functions for each operation
- Router — endpoint signatures, status codes, response models
- **Dependencie
常见问题
编码前会先提问吗?
会。它会进行类似访谈的规划步骤,以明确数据模型、登录方式和分页方式。
它使用哪个数据库库?
该技能基于异步 SQLAlchemy 2.0 和 Pydantic v2 模型构建。
它也会编写测试吗?
会。它会为 API 路由生成 pytest 测试。
详情
作者的原始描述
Plan and build production-ready FastAPI endpoints with async SQLAlchemy, Pydantic v2 models, dependency injection for auth, and pytest tests. Uses interview-driven planning to clarify data models, authentication method, pagination strategy, and caching before writing any code.
热度
- GitHub 星标
- 33k
- 排名
- #9105
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
owasp-security
33k教你的智能体掌握面向网页应用、API、移动端、容器和 AI 智能体的 OWASP 安全指南,以便发现常见弱点并提出修复建议。
agent-management
33k让你的智能体使用 AI Maestro 命令行工具创建、列出、休眠、唤醒、重命名和删除 AI 智能体,管理每个智能体的生命周期。
agent-messaging
33k让你的智能体使用智能体消息协议,在 AI 智能体之间发送、接收和回复带签名的消息,大部分在你自己的电脑上完成。
docs-search
33k让你的智能体搜索自动生成的代码文档,查找函数签名、类定义和注释,从而在修改代码之前核对真实的 API。