OpenCodex

API Documentation

OpenAI-compatible SDK. Access DeepSeek, Qwen, Claude, Gemini, GPT-4 and more through a single endpoint.

Base URL

https://opencodex.hk/api/v1

Authentication

All API requests must include an API Key in the HTTP Authorization header. Create an API Key in the dashboard.

Authorization: Bearer YOUR_API_KEY

Quick 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
  }'

Error Codes

Common error codes that the API may return

Status CodeError CodeDescription
400Bad RequestBad Request (e.g. missing required fields, invalid format)
401UnauthorizedInvalid, expired, or malformed API Key (must start with sk-live-)
402Payment RequiredInsufficient Credits — please top up and try again
429Too Many RequestsToo many requests — rate limit triggered, please retry later
500Internal Server ErrorInternal 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:

CursorClaude CodeContinueGitHub CopilotTraeClineCodeiumTabnineWindsurf

Support

If you have any questions, feel free to contact us through:

Chat Completions

Send a list of messages to the model and receive a response. Fully compatible with OpenAI /v1/chat/completions format.

Endpoint

POST /v1/chat/completions

Request Parameters

ParameterTypeRequiredDescription
modelstring模型 ID,如 deepseek-chat, gpt-4o, claude-3-sonnet
messagesarray对话消息数组,包含 role (system/user/assistant) 和 content
max_tokensinteger最大生成 token 数,默认 4096
temperaturenumber温度参数 (0-2),控制随机性,默认 1
top_pnumber核采样参数 (0-1),默认 1
streamboolean是否使用流式输出,默认 false
stoparray|string停止序列,遇到此内容时停止生成

Request Body

{
  "model": "deepseek-chat",
  "messages": [
    { "role": "system", "content": "You are a helpful assistant." },
    { "role": "user", "content": "Hello!" }
  ],
  "temperature": 0.7,
  "max_tokens": 2048,
  "stream": false
}

Response

{
  "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
  }
}

Streaming

Set stream: true to receive model output token by token, ideal for real-time chat interfaces.

Request Body

{
  "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]

Available Models

Get the list of all model IDs your account has access to.

GET /v1/models

Balance Check

Query the remaining Credits balance for your account.

GET /v1/balance

SDK Examples

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);

Error Codes

CodeMeaning
200Success
400Bad Request (e.g. missing required fields)
401Invalid or expired API Key
402Insufficient Credits — please top up
429Too many requests — rate limit triggered
500Internal server error

Ready to start building?

Get 500 free Credits on signup. No credit card required.

Sign Up Free