团队管理 API
团队管理相关接口,包括邀请链接、API Key 管理和团队密钥管理。
路由前缀:/teams
源码:apps/backend/src/routes/teams.ts
认证
所有接口需要 JWT 认证:
Authorization: Bearer <JWT>端点
1. 生成团队邀请链接
POST /teams/generate-invite-link生成一个团队邀请 JWT Token,有效期 7 天。仅团队 owner 可操作。
请求体:
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
teamId | string | 是 | 团队 ID |
响应 200 OK:
json
{
"success": true,
"data": {
"token": "eyJhbGciOiJIUzI1NiIs...",
"expiresAt": "2026-03-14T10:00:00.000Z"
}
}错误码:
| HTTP | 错误 | 说明 |
|---|---|---|
| 400 | teamId is required | 未提供 teamId |
| 401 | Unauthorized | 未认证 |
| 403 | Only team owner can generate invite links | 非团队 owner |
| 404 | Team not found | 团队不存在 |
| 500 | Internal server error | 服务端错误 |
2. 接受团队邀请
POST /teams/accept-invite使用邀请 Token 加入团队。如果用户已是成员,返回 alreadyMember: true(幂等)。
请求体:
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
token | string | 是 | 邀请 JWT Token |
响应 200 OK:
json
{
"success": true,
"data": {
"team": {
"id": "uuid",
"slug": "my-team",
"name": "My Team"
},
"alreadyMember": false
}
}错误码:
| HTTP | 错误 | 说明 |
|---|---|---|
| 400 | token is required | 未提供 token |
| 400 | Invalid or expired invite token | Token 无效或过期 |
| 401 | Unauthorized | 未认证 |
| 404 | Team not found | 团队不存在 |
| 500 | Internal server error | 服务端错误 |
3. 创建团队 API Key
POST /teams/:teamId/api-keys为团队创建 API Key。密钥明文仅在创建时返回一次,数据库只存储哈希值。仅团队 owner 可操作。
路径参数:
| 参数 | 说明 |
|---|---|
teamId | 团队 ID |
请求体:
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
name | string | 是 | Key 名称 |
description | string | 否 | 描述 |
limitCredits | number | null | 否 | 积分限额 |
响应 201 Created:
json
{
"success": true,
"data": {
"id": "uuid",
"name": "My API Key",
"prefix": "wn-",
"suffix": "abc...defg",
"apiKey": "wn-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"createdAt": "2026-03-07T10:00:00.000Z"
},
"message": "请立即保存此 API Key,它不会再次显示!"
}注意:
apiKey字段包含完整明文密钥,仅在此次响应中返回,请立即保存。
错误码:
| HTTP | 错误 | 说明 |
|---|---|---|
| 400 | name is required | 未提供 name |
| 401 | Unauthorized | 未认证 |
| 403 | Only team owner can create API keys | 非团队 owner |
| 404 | Team not found | 团队不存在 |
| 500 | Internal server error | 服务端错误 |
4. 列出团队密钥
GET /teams/:teamId/secrets获取团队的所有密钥列表。团队成员(owner 和 member)均可查看。
路径参数:
| 参数 | 说明 |
|---|---|
teamId | 团队 ID |
响应 200 OK:
json
{
"success": true,
"data": [
{
"id": "uuid",
"key": "MY_SECRET_KEY",
"description": "第三方 API 密钥",
"created_at": "2026-03-07T10:00:00.000Z",
"updated_at": "2026-03-07T10:00:00.000Z"
}
]
}错误码:
| HTTP | 错误 | 说明 |
|---|---|---|
| 401 | Unauthorized | 未认证 |
| 403 | Permission denied | 非团队成员 |
| 500 | Internal server error | 服务端错误 |
5. 创建团队密钥
POST /teams/:teamId/secrets创建团队密钥。仅团队 owner 可操作。
路径参数:
| 参数 | 说明 |
|---|---|
teamId | 团队 ID |
请求体:
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
key | string | 是 | 密钥名(大写字母开头,仅大写字母/数字/下划线,最长 64 字符) |
value | string | 是 | 密钥值 |
description | string | 否 | 描述 |
key 格式要求:匹配 ^[A-Z][A-Z0-9_]*$,最大 64 字符。
响应 201 Created:
json
{
"success": true,
"data": {
"id": "uuid",
"key": "MY_SECRET_KEY",
"description": "描述",
"created_at": "2026-03-07T10:00:00.000Z"
}
}错误码:
| HTTP | 错误 | 说明 |
|---|---|---|
| 400 | key and value are required | 缺少必填字段 |
| 400 | key must match ... | key 格式不合法 |
| 401 | Unauthorized | 未认证 |
| 403 | Only team owner can create secrets | 非团队 owner |
| 409 | Key already exists in this team | key 重复 |
| 500 | Internal server error | 服务端错误 |
6. 更新团队密钥
PUT /teams/:teamId/secrets/:id更新团队密钥的值或描述。仅团队 owner 可操作。
路径参数:
| 参数 | 说明 |
|---|---|
teamId | 团队 ID |
id | 密钥 ID |
请求体:
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
value | string | 否 | 新密钥值 |
description | string | 否 | 新描述 |
value和description至少提供一个。
响应 200 OK:
json
{
"success": true,
"data": {
"id": "uuid",
"key": "MY_SECRET_KEY",
"description": "新描述",
"updated_at": "2026-03-07T12:00:00.000Z"
}
}错误码:
| HTTP | 错误 | 说明 |
|---|---|---|
| 400 | At least one of value or description is required | 未提供任何更新字段 |
| 401 | Unauthorized | 未认证 |
| 403 | Only team owner can update secrets | 非团队 owner |
| 500 | Internal server error | 服务端错误 |
7. 删除团队密钥
DELETE /teams/:teamId/secrets/:id删除团队密钥。仅团队 owner 可操作。
路径参数:
| 参数 | 说明 |
|---|---|
teamId | 团队 ID |
id | 密钥 ID |
响应 200 OK:
json
{
"success": true
}错误码:
| HTTP | 错误 | 说明 |
|---|---|---|
| 401 | Unauthorized | 未认证 |
| 403 | Only team owner can delete secrets | 非团队 owner |
| 500 | Internal server error | 服务端错误 |