API 文档
兼容 OpenAI SDK,单一端点访问 DeepSeek、Qwen、Claude、Gemini、GPT-4 等所有模型。
基础 URL
https://opencodex.hk/api/v1认证
所有 API 请求需要在 HTTP Header 中携带 API Key。在控制台创建 API Key。
Authorization: Bearer YOUR_API_KEY快速开始
使用 cURL 或 PowerShell 快速测试 API 连接
cURL 示例
curl -X POST https://opencodex.hk/api/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-live-YOUR_API_KEY" \
-d '{
"model": "deepseek-chat",
"messages": [{"role": "user", "content": "Hello!"}]
}'PowerShell 示例
Invoke-RestMethod -Uri "https://opencodex.hk/api/v1/chat/completions" -Method POST -Headers @{'Authorization'='Bearer sk-live-YOUR_API_KEY';'Content-Type'='application/json'} -Body '{"model":"deepseek-chat","messages":[{"role":"user","content":"Hello!"}]}'代码示例
Python
from openai import OpenAI
client = OpenAI(
api_key="sk-live-YOUR_API_KEY",
base_url="https://opencodex.hk/api/v1"
)
response = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
temperature=0.7,
max_tokens=2048
)
print(response.choices[0].message.content)JavaScript / TypeScript
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'sk-live-YOUR_API_KEY',
baseURL: 'https://opencodex.hk/api/v1'
});
const response = await client.chat.completions.create({
model: 'deepseek-chat',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Hello!' }
],
temperature: 0.7,
max_tokens: 2048
});
console.log(response.choices[0].message.content);cURL
curl -X POST https://opencodex.hk/api/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-live-YOUR_API_KEY" \
-d '{
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"temperature": 0.7,
"max_tokens": 2048
}'错误码
API 可能返回的常见错误码及说明
| 状态码 | 错误码 | 描述 |
|---|---|---|
| 400 | Bad Request | 请求参数错误(如缺少必填字段、格式不正确) |
| 401 | Unauthorized | API Key 无效、过期或格式错误(必须以 sk-live- 开头) |
| 402 | Payment Required | 积分余额不足,请充值后再试 |
| 429 | Too Many Requests | 请求过于频繁,触发速率限制,请稍后重试 |
| 500 | Internal Server Error | 服务器内部错误,请稍后重试或联系客服 |
最佳实践
🔑 API Key 安全
- 不要将 API Key 硬编码在前端代码中,使用环境变量存储
- 定期轮换 API Key,避免泄露
- 为不同应用创建不同的 API Key,便于管理和撤销
⚡ 性能优化
- 复用连接池,避免频繁建立 HTTPS 连接
- 合理设置 max_tokens,避免生成过长的回复
- 使用流式输出提升用户体验,减少等待时间
🔧 兼容的工具
我们的 API 完全兼容以下主流 AI 编程工具:
CursorClaude CodeContinueGitHub CopilotTraeClineCodeiumTabnineWindsurf
技术支持
聊天补全
发送消息列表给模型,获取回复。完全兼容 OpenAI /v1/chat/completions 格式。
接口地址
POST /v1/chat/completions请求参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | ✓ | 模型 ID,如 deepseek-chat, gpt-4o, claude-3-sonnet |
| messages | array | ✓ | 对话消息数组,包含 role (system/user/assistant) 和 content |
| max_tokens | integer | 最大生成 token 数,默认 4096 | |
| temperature | number | 温度参数 (0-2),控制随机性,默认 1 | |
| top_p | number | 核采样参数 (0-1),默认 1 | |
| stream | boolean | 是否使用流式输出,默认 false | |
| stop | array|string | 停止序列,遇到此内容时停止生成 |
请求体
{
"model": "deepseek-chat",
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "Hello!" }
],
"temperature": 0.7,
"max_tokens": 2048,
"stream": false
}响应示例
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1700000000,
"model": "deepseek-chat",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 10,
"total_tokens": 30
}
}流式传输
设置 stream: true 可以逐字接收模型输出,适合实时对话场景。
请求体
{
"model": "deepseek-chat",
"messages": [{ "role": "user", "content": "Hello!" }],
"stream": true
}流式响应示例
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1700000000,"model":"deepseek-chat","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1700000000,"model":"deepseek-chat","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1700000000,"model":"deepseek-chat","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]可用模型列表
获取当前账号有权访问的所有模型 ID 列表。
GET /v1/models余额查询
查询当前账号的剩余积分余额。
GET /v1/balanceSDK 示例
Python
from openai import OpenAI
client = OpenAI(
base_url="https://opencodex.hk/api/v1",
api_key="YOUR_API_KEY"
)
response = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)Node.js
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://opencodex.hk/api/v1",
apiKey: process.env.OPENCODEX_API_KEY,
});
const response = await client.chat.completions.create({
model: "deepseek-chat",
messages: [{ role: "user", content: "Hello!" }],
});
console.log(response.choices[0].message.content);错误码
| 状态码 | 含义 |
|---|---|
| 200 | 请求成功 |
| 400 | 请求参数错误(如缺少必填字段) |
| 401 | API Key 无效或已过期 |
| 402 | 积分余额不足,请充值 |
| 429 | 请求过于频繁,触发速率限制 |
| 500 | 服务器内部错误 |
准备好开始构建了吗?
注册即送 500 免费积分,无需信用卡。
免费注册