API Documentation

OpenAI-compatible API for China's best AI models

Contents

Quick Start

TokenOutput is a drop-in replacement for the OpenAI API. Just change the base URL and model name.

curl https://token-output.klk1129635667.workers.dev/v1/chat/completions \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-chat",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Hello!"}
    ]
  }'

Authentication

All API requests require an API key sent in the Authorization header.

Authorization: Bearer sk-your-api-key

Get your API key from the dashboard.

Chat Completions

POST /v1/chat/completions

Creates a chat completion. Fully compatible with the OpenAI Chat Completions API.

Request Body

ParameterTypeRequiredDescription
modelstringYesModel ID (e.g. deepseek-chat)
messagesarrayYesArray of message objects
streambooleanNoEnable SSE streaming (default: false)
temperaturenumberNoSampling temperature (0-2)
max_tokensintegerNoMax tokens to generate
top_pnumberNoNucleus sampling
stopstring|arrayNoStop sequences

Response

{
  "id": "chatcmpl-abc123",
  "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 Server-Sent Events (SSE).

curl https://token-output.klk1129635667.workers.dev/v1/chat/completions \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"model": "deepseek-chat", "stream": true,
       "messages": [{"role": "user", "content": "Hello!"}]}'

Available Models

GET /v1/models
Model IDProviderBest for
deepseek-chatDeepSeekGeneral purpose, coding
deepseek-reasonerDeepSeekComplex reasoning, math
qwen-turboAlibabaFast, low cost
qwen-plusAlibabaBalanced quality/cost
qwen-maxAlibabaHighest quality
qwen-longAlibabaLong documents
glm-4-plusZhipu AIFlagship, best quality
glm-4-airZhipu AIBalanced
glm-4-flashZhipu AIUltra fast, cheapest

SDK Examples

Python (OpenAI SDK)

from openai import OpenAI

client = OpenAI(
    api_key="sk-your-api-key",
    base_url="https://token-output.klk1129635667.workers.dev/v1"
)

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
)
print(response.choices[0].message.content)

Node.js (OpenAI SDK)

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'sk-your-api-key',
  baseURL: 'https://token-output.klk1129635667.workers.dev/v1',
});

const response = await client.chat.completions.create({
  model: 'deepseek-chat',
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'Hello!' },
  ],
});
console.log(response.choices[0].message.content);

Python (Streaming)

stream = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": "Write a poem"}],
    stream=True
)
for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Billing

Check Balance

GET /v1/billing/balance
curl https://token-output.klk1129635667.workers.dev/v1/billing/balance \
  -H "Authorization: Bearer sk-your-api-key"

# Response: {"balance": 9.50}

Top Up

Top up your balance from the dashboard, or create a checkout session via API:

POST /v1/billing/checkout
curl -X POST https://token-output.klk1129635667.workers.dev/v1/billing/checkout \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"amount": 10}'

# Response: {"url": "https://checkout.stripe.com/...", "session_id": "cs_..."}

Errors

CodeMeaning
400Bad request — invalid model or missing fields
401Unauthorized — invalid or missing API key
402Insufficient balance — top up your account
429Rate limited — slow down requests
502Upstream provider error