Chat Completions
Generates a model response from a chat history. Uses the industry-standard Chat Completions protocol format — mainstream SDKs work as drop-in.
POST https://wrouter.ai/v1/chat/completionsHeaders
| Header | Required | Notes |
|---|---|---|
Authorization | ✓ | Bearer sk-... |
Content-Type | ✓ | application/json |
Body parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | ✓ | e.g. gpt-4o, claude-sonnet-4-5, gemini-2.5-pro, deepseek-chat |
messages | array | ✓ | Conversation history (see below) |
temperature | number | 0–2, default 1 | |
top_p | number | 0–1 | |
n | integer | Default 1 | |
stream | boolean | Enable SSE streaming | |
stream_options | object | e.g. {"include_usage": true} | |
stop | string | string[] | Stop sequences | |
max_tokens | integer | ||
max_completion_tokens | integer | For o-series reasoning models | |
presence_penalty | number | -2 to 2 | |
frequency_penalty | number | -2 to 2 | |
logit_bias | object | ||
user | string | End-user identifier | |
tools | array | Function-calling definitions | |
tool_choice | string | object | "auto" | "none" | "required" | specific function | |
response_format | object | e.g. {"type":"json_object"} or a JSON Schema | |
seed | integer | Deterministic sampling | |
reasoning_effort | string | "low" | "medium" | "high" for reasoning models | |
modalities | array | e.g. ["text","audio"] | |
audio | object | Audio output options |
messages structure
json
[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hi"},
{"role": "assistant", "content": "Hello!"},
{"role": "user", "content": [
{"type": "text", "text": "Describe this image"},
{"type": "image_url", "image_url": {"url": "https://..."}}
]}
]role ∈ system | user | assistant | tool. content is a string or a multimodal array (text / image_url / input_audio).
Response (non-streaming)
json
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1718000000,
"model": "gpt-4o-2024-08-06",
"choices": [
{"index": 0, "message": {"role": "assistant", "content": "Hello!"}, "finish_reason": "stop"}
],
"usage": {"prompt_tokens": 12, "completion_tokens": 6, "total_tokens": 18}
}Response (streaming SSE)
With stream: true responses are text/event-stream:
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"He"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","choices":[{"index":0,"delta":{"content":"llo"}}]}
data: [DONE]Setting stream_options.include_usage = true adds a final usage chunk before [DONE].
Examples
curl
bash
curl https://wrouter.ai/v1/chat/completions \
-H "Authorization: Bearer $WROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"messages": [{"role":"user","content":"Introduce WRouter in one sentence."}],
"temperature": 0.7
}'Python (streaming + tools)
python
from openai import OpenAI
client = OpenAI(api_key="sk-...", base_url="https://wrouter.ai/v1")
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Look up weather",
"parameters": {"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}
}
}]
stream = client.chat.completions.create(
model="claude-sonnet-4-5",
messages=[{"role":"user","content":"Weather in Shanghai?"}],
tools=tools,
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta
if delta.content:
print(delta.content, end="", flush=True)Calling Claude / Gemini
Swap model:
| Family | Sample IDs |
|---|---|
| OpenAI | gpt-4o, gpt-4o-mini, gpt-5, o3-mini |
| Anthropic | claude-sonnet-4-5, claude-opus-4-1, claude-haiku-4-5 |
gemini-2.5-pro, gemini-2.5-flash | |
| DeepSeek | deepseek-chat, deepseek-reasoner |
| Qwen | qwen-max, qwen-plus, qwen3-coder |
| Doubao | doubao-1-5-pro-256k, doubao-seed-1-6 |
Full list at https://wrouter.ai/models.
Errors
See Errors.