One API 是一个开源的 AI API 管理和分发平台,支持将多个 AI 供应商的API统一为一个接口格式(兼容OpenAI格式),实现负载均衡、令牌管理、用量计费等功能。非常适合需要管理多个API Key或搭建API中转服务的场景。
mkdir -p /opt/one-api/data
docker run --name one-api -d
--restart always
-p 3000:3000
-e TZ=Asia/Shanghai
-v /opt/one-api/data:/data
justsong/one-api
server {
listen 443 ssl http2;
server_name api.example.com;
ssl_certificate /etc/ssl/certs/api.example.com.pem;
ssl_certificate_key /etc/ssl/certs/api.example.com.key;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# SSE支持
proxy_buffering off;
proxy_cache off;
chunked_transfer_encoding on;
}
}
访问 https://api.example.com,默认管理员账号:
root123456⚠️ 首次登录后立即修改密码!
渠道是API供应商的接入配置:
| 字段 | 说明 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 类型 | 选择供应商(OpenAI、Anthropic、DeepSeek等) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 名称 | 自定义渠道名称 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Base URL | API端点地址 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 密钥 | API Key | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 模型 | 选择该渠道支持的模型 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
令牌是分发给用户的访问密钥:
| 类型 | OpenAI |
| Base URL | https://api.openai.com |
| 密钥 | sk-xxx |
| 模型 | gpt-4o, gpt-4o-mini, gpt-5.2 |
| 类型 | DeepSeek |
| Base URL | https://api.deepseek.com |
| 密钥 | sk-xxx |
| 模型 | deepseek-v4-pro, deepseek-v4-flash |
| 类型 | Anthropic |
| Base URL | https://api.anthropic.com |
| 密钥 | sk-ant-xxx |
| 模型 | claude-sonnet-4-20250514, claude-opus-4-20250514 |
同一模型可配置多个渠道,One API自动负载均衡:
部署完成后,将 Base URL 改为 One API 地址,使用 One API 生成的令牌即可:
from openai import OpenAI
client = OpenAI(
api_key="sk-one-api-token-xxx", # One API 令牌
base_url="https://api.example.com/v1" # One API 地址
)
# 像使用 OpenAI 一样调用
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}]
)
# 备份SQLite数据库
cp /opt/one-api/data/one-api.db /opt/one-api/data/one-api.db.bak
# 或导出MySQL
mysqldump -u root -p one_api > one_api_backup.sql
Q: 渠道报错 401?
A: 检查API Key是否正确,是否已过期。
Q: 流式输出中断?
A: Nginx需关闭 proxy_buffering,确保SSE正常传输。
Q: 如何查看日志?
A: docker logs one-api -f --tail 100
评论区