跳转到主要内容

概述

FIM One API 支持两种身份验证方法:
  1. API 密钥 — 用于服务间集成的简单、长期有效的令牌
  2. JWT 令牌 — 来自用户登录的短期令牌(用于 SSE 端点)
大多数集成应使用 API 密钥。

API 密钥

API 密钥是与您的用户账户关联的长期凭证。它们适用于:
  • 服务器到服务器的集成
  • 计划脚本和自动化
  • 外部应用程序访问 FIM One

创建 API 密钥

  1. 登录到您的 FIM One 门户
  2. 转到 设置 → API 密钥
  3. 点击 创建 API 密钥
  4. 输入名称(例如,“生产集成”)
  5. (可选)设置作用域以限制访问权限
  6. (可选)设置过期日期
  7. 点击 创建
  8. 立即复制密钥 — 它只会显示一次
完整密钥看起来像:fim_your44characterkeystringhere(以 fim_ 前缀开头)

使用 API 密钥

在 Authorization 标头中将密钥作为 Bearer 令牌包含:
或在 Python 中:

API 密钥功能

可见性: 每个密钥显示:
  • 密钥前缀(前 8 个字符用于识别)
  • 创建日期
  • 最后使用时间戳
  • 总请求数
  • 活跃状态
  • 过期日期(如果设置)
管理: 您可以:
  • 启用/禁用密钥而无需删除它们
  • 设置自动过期日期
  • 永久删除密钥
  • 跟踪使用模式

作用域

作用域限制API密钥可以访问的内容。如果未设置作用域,密钥具有完全访问权限。 具有作用域的密钥只能调用其作用域允许的端点。所有其他端点返回403,包括/api/me/api-keys下的API密钥管理,该管理需要没有作用域的密钥或已登录的会话。密钥永远无法创建比其自身作用域更广泛的密钥。 可用作用域: 创建具有作用域的密钥:

JWT Tokens

JWT tokens are short-lived and issued at login. They’re used for:
  • SSE Streaming Endpoints: Pass tokens in the request body for /api/react, /api/dag and /api/auto
  • Portal Session: Frontend authentication

获取 JWT Token

当您通过网络门户登录或调用身份验证端点时,JWT token 会自动颁发:
响应:

Two-factor authentication

When the account has two-factor authentication enabled, sign-in does not return tokens. POST /api/auth/login and POST /api/auth/login-with-code answer with a short-lived temporary token instead:
Send it with the TOTP code (or a backup code) to finish signing in. The response is the same token payload as a normal login:
The temporary token is valid for 5 minutes. Signing in through an OAuth provider (GitHub, Google, Discord, Feishu) stops at the same step: the callback hands the browser a 2FA challenge instead of tokens. After 5 incorrect codes within 15 minutes for the same account, the endpoint answers 429 with error_code 2fa_too_many_attempts; starting a new sign-in does not reset the count.

Using JWT for Streaming

For SSE endpoints, include the token in the request body. The field accepts a JWT access token or an API key (a scoped key needs the chat scope):
Or from JavaScript using fetch with a readable stream (the endpoint is POST-only, so native EventSource which only supports GET cannot be used):

令牌刷新

访问令牌在 2 小时后过期。使用刷新令牌获取新的访问令牌,无需重新登录:

安全最佳实践

API 密钥

永远不要将 API 密钥提交到版本控制。 它们提供对您数据的完全访问权限。
  1. 存储在环境变量中:
    然后在代码中引用:
  2. 使用密钥轮换:
    • 创建新密钥
    • 更新您的应用程序
    • 删除旧密钥
    • 每季度重复一次
  3. 设置过期日期:
    • 为临时集成使用短期密钥
    • 定期要求重新身份验证
  4. 使用作用域密钥:
    • 除非必要,否则不要使用”完全访问”密钥
    • 为不同的服务创建单独的密钥
    • 限制密钥泄露时的损害
  5. 监控使用情况:
    • 检查”最后使用”时间戳
    • 查看请求计数
    • 删除未使用的密钥

JWT 令牌

  1. 保持短生命周期:访问令牌在 2 小时后过期
  2. 安全存储:
    • 在浏览器中:使用 HttpOnly cookies(比 localStorage 更安全)
    • 在服务器中:使用安全的会话存储
  3. 不要在日志中暴露:仅记录令牌前缀,不记录完整令牌
  4. 仅使用 HTTPS:不要通过未加密的连接发送令牌

速率限制

计划在未来版本中实现按密钥的速率限制。目前没有强制执行的按密钥请求限制。

Troubleshooting

Every error response has the shape {"detail", "error_code", "error_args"}. Errors raised while checking the Authorization header carry error_code: null; tell them apart by the status code and detail.

Invalid Token Error

Causes (401; detail is Invalid token, Invalid API key, API key is disabled or API key has expired):
  • API key prefix is wrong (must start with fim_)
  • Token is malformed or corrupted
  • API key has been deleted, disabled, or is past its expiration date
Solution: Verify your token format and re-generate if necessary

Unauthorized Error

Causes (401):
  • Token is missing from the request body (SSE endpoints return the body above)
  • No Authorization header provided (other endpoints answer Not authenticated with error_code: null)
Solution: Include a valid token in every request

Forbidden Error

Causes (403):
  • API key has scoped access and lacks the required scope (api_key_scope_denied)
  • The client address is refused by the instance’s IP rules (ip_not_allowed, see below)
  • The account is disabled (detail: Account disabled) or lacks the role the endpoint needs
Solution: Use a key with the appropriate scope or increase key privileges

IP Rules

Administrators can restrict API access by client address under admin Security → IP Rules. A request from an address that matches a deny rule, or that matches no allow rule when allow rules exist, is refused before authentication:
/api/version, /api/webhooks/ and CORS preflight requests are exempt. Behind a reverse proxy the instance must be told which proxy to trust (FORWARDED_ALLOW_IPS), otherwise every request appears to come from the proxy; see Environment Variables.

Expired Token

On the SSE endpoints, which read the token from the request body, the same condition returns error_code token_expired. Solution: Use the refresh token to get a new access token:

环境变量设置

Python

Node.js

Bash

Support

For authentication issues:
  • Check the API Overview for response format details
  • Check that a token is still accepted with GET /api/auth/me
  • Contact support if you believe a key has been compromised