Dokumentasi API
SDK serasi OpenAI. Akses DeepSeek, Qwen, Claude, Gemini, GPT-4 dan banyak lagi melalui satu endpoint.
URL Asas
https://opencodex.hk/api/v1Pengesahan
Semua permintaan API mesti menyertakan Kunci API dalam header Pengesahan HTTP. Cipta Kunci API di papan pemuka.
Authorization: Bearer YOUR_API_KEYQuick Start
Test API connection quickly with cURL or PowerShell
cURL Example
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 Example
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!"}]}'Code Examples
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
}'Kod Ralat
Common error codes that the API may return
| Status Code | Error Code | Description |
|---|---|---|
| 400 | Bad Request | Bad Request (e.g. missing required fields, invalid format) |
| 401 | Unauthorized | Invalid, expired, or malformed API Key (must start with sk-live-) |
| 402 | Payment Required | Insufficient Credits — please top up and try again |
| 429 | Too Many Requests | Too many requests — rate limit triggered, please retry later |
| 500 | Internal Server Error | Internal server error — please retry later or contact support |
Best Practices
🔑 API Key Security
- Never hardcode API Keys in frontend code; use environment variables
- Rotate API Keys regularly to prevent unauthorized access
- Create separate API Keys for different applications for easier management and revocation
⚡ Performance Tips
- Reuse connection pools to avoid frequent HTTPS connection establishment
- Set max_tokens appropriately to avoid generating overly long responses
- Use streaming output to enhance user experience and reduce wait time
🔧 Compatible Tools
Our API is fully compatible with the following popular AI coding tools:
Support
If you have any questions, feel free to contact us through:
Penyiapan Sembang
Hantar senarai mesej kepada model dan terima respons. Sepenuhnya serasi dengan format OpenAI /v1/chat/completions.
Titik Akhir
POST /v1/chat/completionsRequest Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| 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 | 停止序列,遇到此内容时停止生成 |
Badan Permintaan
{
"model": "deepseek-chat",
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "Hello!" }
],
"temperature": 0.7,
"max_tokens": 2048,
"stream": false
}Respons
{
"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
}
}Strim
Tetapkan stream: true untuk menerima output model token demi token, sesuai untuk antara muka sembang masa nyata.
Badan Permintaan
{
"model": "deepseek-chat",
"messages": [{ "role": "user", "content": "Hello!" }],
"stream": true
}Stream Response Example
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]Senarai Model
Dapatkan senarai semua ID model yang akaun anda boleh akses.
GET /v1/modelsSemakan Baki
Semak baki Kredit yang tinggal untuk akaun anda.
GET /v1/balanceContoh SDK
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);Kod Ralat
| Kod | Makna |
|---|---|
| 200 | Berjaya |
| 400 | Permintaan Buruk (contohnya medan yang diperlukan tiada) |
| 401 | Kunci API tidak sah atau tamat tempoh |
| 402 | Kredit tidak mencukupi — sila tambah nilai |
| 429 | Terlalu banyak permintaan — had kadar dicetuskan |
| 500 | Ralat pelayan dalaman |
Sedia untuk mula membina?
500 Kredit percuma apabila mendaftar. Tiada kad kredit diperlukan.
Daftar Percuma