跳转至

从示例项目开始

除了五个级别之外,fastkit 还自带五个示例项目。每个示例项目都通过一个带测试、可以运行的 API 展示一个主题。fastkit init --template <name> 会按原样复制其中一个示例项目,你不能再为它添加功能。下面每一节都会说明这个示例展示了什么、如何创建它、它提供哪些端点,以及运行它需要什么。项目的 README.md 会进一步介绍细节。

模板 说明 首先阅读
fastapi-custom-response 带有自定义响应系统的异步条目管理 API 先阅读 src/main.py,再阅读 src/helper/responses.py 和 src/api/routes/items.py,了解统一的响应封装格式。
fastapi-auth-jwt 带 JWT 认证的 FastAPI 项目 先阅读 src/app/main.py,再阅读 src/app/domains/auth/ 和 src/app/core/security.py。
fastapi-sqlmodel 使用 SQLModel 的 FastAPI 项目 先阅读 src/app/main.py,再阅读 src/app/domains/items/ 和 src/app/db/session.py。README 说明了如何切换到 PostgreSQL 或 MySQL。
fastapi-mcp 使用 MCP 的 FastAPI 项目 先阅读 src/main.py,再阅读 src/mcp_server/router.py 和 src/auth/。
fastapi-llm-agent 使用 LLM 智能体的 FastAPI 项目 先阅读 src/app/main.py,再阅读 src/app/api/chat.py 和 src/app/llm/agent.py。

端点列表来自每个项目的 /openapi.json,项目在 uv sync 之后立即用 uv run uvicorn 启动。每个示例还会对 GET /health 返回 {"status":"ok"},表格中省略了这个端点。

fastapi-custom-response

所有 JSON 响应(包括错误响应)都使用统一的响应封装格式:timestamp、status、code、path 和 message。异常处理器会把 HTTPException、校验错误和未处理的异常都包装成同样的格式。条目保存在一个 JSON 文件(src/mocks/mock_items.json)中,以异步方式读写。除了 uv sync 之外,不需要其他准备工作。表格后面的请求展示了采用这种封装格式的 404 响应。

$ fastkit init --template fastapi-custom-response --project-name items_api
方法 路径 摘要
GET /api/v1/items List items
POST /api/v1/items Create item
GET /api/v1/items/{item_id} Get item by ID
PUT /api/v1/items/{item_id} Replace item
PATCH /api/v1/items/{item_id} Update item
DELETE /api/v1/items/{item_id} Delete item
$ curl -s http://127.0.0.1:8000/api/v1/items/999
{"timestamp":"2026-12-03T17:26:02.677Z","status":404,"code":"DATA-002","path":"/api/v1/items/999","message":"Item not found"}

fastapi-auth-jwt

带有访问令牌和刷新令牌的用户账号。刷新令牌每使用一次就轮换一次,密码使用 Argon2id 进行哈希,依赖项按角色和权限范围(scope)保护路由。用户数据用 SQLModel 存储;登录、注册和刷新端点都有限流。

$ fastkit init --template fastapi-auth-jwt --project-name accounts
方法 路径 摘要
GET /ready Ready
POST /api/v1/auth/register Register
POST /api/v1/auth/login Login
POST /api/v1/auth/refresh Refresh
POST /api/v1/auth/logout Logout
POST /api/v1/auth/logout-all Logout All
GET /api/v1/users/me Read Current User
PATCH /api/v1/users/me Update Current User
GET /api/v1/users/me/scopes Read Current User Scopes
GET /api/v1/users List Users
GET /api/v1/users/{user_id} Read User

准备工作:首次运行不需要任何准备。应用使用本地的 SQLite 文件(.env 中的 DATABASE_URL=sqlite:///./app.db),fastkit 已经生成了 .env,并写入了唯一的 SECRET_KEY。“Next steps”面板列出了首次启动之前要运行的 uv run alembic upgrade head。README 介绍了如何通过 DATABASE_URL 改用 PostgreSQL,以及如何通过 FIRST_SUPERUSER_EMAIL 和 FIRST_SUPERUSER_PASSWORD 预先创建一个超级用户。

fastapi-sqlmodel

基于异步引擎的 SQLModel、Alembic 迁移、通用的 CRUD 基类,以及支持分页的列表端点。只需一个 DATABASE_URL 就能选择 SQLite、PostgreSQL 或 MySQL,而测试始终使用 SQLite。

$ fastkit init --template fastapi-sqlmodel --project-name notes
方法 路径 摘要
GET /ready Ready
GET /api/v1/items List Items
POST /api/v1/items Create Item
GET /api/v1/items/{item_id} Read Item
PUT /api/v1/items/{item_id} Replace Item
PATCH /api/v1/items/{item_id} Update Item
DELETE /api/v1/items/{item_id} Delete Item

准备工作:SQLite 不需要服务器。数据库表结构由 Alembic 管理(.env 中的 CREATE_TABLES_ON_STARTUP=false),因此请在 uv sync 之后、首次启动之前创建数据表:

$ uv run alembic upgrade head
...
INFO  [alembic.runtime.migration] Running upgrade  -> 0001, create items table
INFO  [alembic.runtime.migration] Running upgrade 0001 -> 0002, store items.created_at with a time zone

如果要使用 PostgreSQL 或 MySQL,请参阅 README 中的“Use PostgreSQL or MySQL”一节:它用项目自带的 docker-compose.yml 启动本地服务器(docker compose --profile postgres up -d --wait),安装驱动(uv sync --extra postgresql 或 --extra mysql),并在 .env 中设置 DATABASE_URL。

fastapi-mcp

一个 FastAPI 应用,它的条目端点同时也是 MCP(Model Context Protocol)工具:list_items、get_item、create_item、replace_item、update_item 和 delete_item。用户通过 OAuth2 密码流程登录。读取条目无需认证,写入则需要条目所有者的令牌。

$ fastkit init --template fastapi-mcp --project-name mcp_api
方法 路径 摘要
GET / Root
POST /api/v1/auth/token Get an access token
GET /api/v1/users/me Get the current user
GET /api/v1/users List users (superusers only)
GET /api/v1/items List items
POST /api/v1/items Create an item
GET /api/v1/items/{item_id} Get item by ID
PUT /api/v1/items/{item_id} Replace an item
PATCH /api/v1/items/{item_id} Update some fields of an item
DELETE /api/v1/items/{item_id} Delete an item
GET /api/v1/mcp-server Describe the MCP server

MCP 服务器挂载在 /mcp,始终需要认证:没有凭据时,它返回 401。

$ curl -s http://127.0.0.1:8000/api/v1/mcp-server
{"enabled":true,"mount_path":"/mcp","title":"FastAPI MCP Server","description":"FastAPI endpoints exposed as MCP tools","tool_tags":["items"]}
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8000/mcp
401

准备工作:在你接入自己的用户存储(src/auth/users.py 中的 get_users_db())之前,用户存储是空的。在本地试验时,可以在 .env 中设置 ENABLE_DEMO_USERS=true,启用三个密码公开的演示账号(在 development 以外的环境中会拒绝启用演示账号)。MCP 端点接受用户的 JWT,或者你在 .env 中设置为 MCP_API_KEY 的密钥;README 中的“MCP Authentication”一节演示了这两种方式。

fastapi-llm-agent

一个 Claude 聊天智能体,它通过服务器发送事件以流式方式返回回复,并使用两个内置工具(calculator、current_time)运行工具调用循环。对话保存在内存中,存储方式隐藏在一个可以替换的接口之后。测试用一个按脚本应答的模拟对象替换 Anthropic 客户端,因此测试既不需要密钥,也不需要网络。

$ fastkit init --template fastapi-llm-agent --project-name chat_agent
方法 路径 摘要
POST /api/v1/chat Chat
GET /api/v1/conversations/{conversation_id} Read Conversation
DELETE /api/v1/conversations/{conversation_id} Delete Conversation

准备工作:运行测试和调用端点都不需要准备;应用在没有密钥的情况下也能启动,用 uv run pytest 运行时 88 个测试全部通过。要进行真正的聊天,需要一个 Anthropic API 密钥。在 .env 中设置 ANTHROPIC_API_KEY(fastkit 会把它留空),然后用 bash scripts/chat.sh "What is 12 * 34?" 发送一条消息。如果要改用兼容 Anthropic 的网关,请设置 LLM_BASE_URL;如果网关使用 bearer 认证,还要设置 LLM_AUTH_TOKEN。