Create chat completion
Creates an OpenAI-style chat completion.
POST /v1/chat/completions
Useful for migrating projects that already use an OpenAI SDK. Requests use OpenAI-style messages, while model IDs come from the GlobalRouter catalog.
Back to Chat endpoint listRequest parameters
| Field | Type | Required | Description |
|---|---|---|---|
model | string | Yes | GlobalRouter model ID. |
messages | array | Yes | OpenAI-style message array containing role and content. |
messages[] | object | Yes | Single OpenAI-style message. |
messages[].role | string | Yes | Message role, such as system, user, assistant, or tool. |
messages[].content | string | array | No | Message text or multimodal content parts. |
messages[].name | string | No | Participant name. |
messages[].tool_calls | array | No | Tool calls in an assistant message. |
messages[].tool_call_id | string | No | Tool call ID associated with a tool message. |
provider | string | No | GlobalRouter provider preference; this is a GR provider string, not the official OpenRouter provider object. |
stream | boolean | No | When true, returns text/event-stream. |
stream_options | object | No | Streaming options, such as include_usage. |
temperature | number | No | Sampling temperature. |
top_p | number | No | Nucleus sampling probability. |
max_completion_tokens | integer | No | Maximum output token count. |
max_tokens | integer | No | Maximum output tokens for legacy requests; prefer max_completion_tokens for new requests. |
reasoning | object | No | Reasoning configuration, such as effort, summary, or reasoning options supported by the provider. |
reasoning_effort | string | No | Shorthand for reasoning.effort; the two fields must not have different values. |
tools | array | No | Tool call definitions. |
tool_choice | string | object | No | Tool selection strategy, such as auto or a specific tool. |
response_format | object | No | Structured output constraints. |
stop | string | string[] | No | Stop sequences. |
user | string | No | Application-side user identifier. |
_router | object | No | GlobalRouter routing metadata, such as provider, route, or metadata. |
Request examples
curl "https://api.globalrouter.com/v1/chat/completions" \
-H "Authorization: Bearer $GR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "kimi-k3",
"messages": [
{ "role": "user", "content": "用三句话介绍 GlobalRouter。" }
],
"provider": "moonshot",
"stream": false,
"stream_options": { "include_usage": true },
"temperature": 0.7,
"top_p": 0.9,
"max_completion_tokens": 512,
"reasoning_effort": "high",
"tools": [
{
"type": "function",
"function": {
"name": "lookup_model",
"description": "查询模型信息",
"parameters": {
"type": "object",
"properties": { "model": { "type": "string" } },
"required": ["model"]
}
}
}
],
"tool_choice": "auto",
"response_format": { "type": "json_object" },
"stop": ["###"],
"user": "user_123",
"_router": {
"route": "balanced",
"metadata": { "project": "docs-demo" }
}
}'Response fields
| Field | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Generation record ID. |
choices | array | Yes | Model output; streaming responses include delta. |
usage | object | No | Token and billing statistics. |
model | string | Yes | Actual model ID used. |
Response example
{
"id": "chatcmpl_01HV...",
"object": "chat.completion",
"created": 1716283200,
"model": "kimi-k3",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "GlobalRouter 提供统一模型 API..." },
"finish_reason": "stop"
}
],
"usage": { "prompt_tokens": 18, "completion_tokens": 42, "total_tokens": 60 }
}Errors
| HTTP | Code | Description |
|---|---|---|
| 401 | UNAUTHORIZED | Missing or invalid Authorization Bearer token. |
| 403 | FORBIDDEN | The current API key is not allowed to call this scope or model. |
| 429 | RATE_LIMITED | Requests are too frequent or the asynchronous task queue is full; reduce concurrency or retry later. |
Notes
- `provider` is a GlobalRouter provider string; the field with the same name in the official OpenRouter Chat Completion documentation is an object.