API Reference
Chat Completions
Create chat completions with streaming support.
Chat Completions
POST
/v1/chat/completionsCreates a chat completion. This endpoint is fully compatible with the OpenAI Chat Completions API format.
You can use this API in two modes:
- With Assistant: Provide
assistant_idto use a pre-configured assistant with system prompt, model, and tools from the dashboard - Direct Mode: Provide
modeldirectly for a pure OpenAI-compatible experience without assistant features
Request Body
Prop
Type
Messages Format
Each message in the messages array has the following structure:
Prop
Type
Example Request (With Assistant)
Use an assistant to leverage pre-configured system prompts, model settings, and tools:
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://api.assistantrouter.com/v1',
apiKey: process.env.ASSISTANTROUTER_API_KEY,
});
const response = await client.chat.completions.create({
// @ts-ignore - assistant_id is our extension
assistant_id: 'your-assistant-uuid',
messages: [
{ role: 'user', content: 'What is the capital of France?' },
],
});
console.log(response.choices[0].message.content);import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.assistantrouter.com/v1",
api_key=os.environ["ASSISTANTROUTER_API_KEY"],
)
response = client.chat.completions.create(
extra_body={"assistant_id": "your-assistant-uuid"},
messages=[
{"role": "user", "content": "What is the capital of France?"},
],
)
print(response.choices[0].message.content)curl https://api.assistantrouter.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ASSISTANTROUTER_API_KEY" \
-d '{
"assistant_id": "your-assistant-uuid",
"messages": [
{"role": "user", "content": "What is the capital of France?"}
]
}'Example Request (Direct Mode)
Use the API directly without an assistant - pure OpenAI compatibility:
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://api.assistantrouter.com/v1',
apiKey: process.env.ASSISTANTROUTER_API_KEY,
});
const response = await client.chat.completions.create({
model: 'anthropic/claude-haiku-4.5',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is the capital of France?' },
],
});
console.log(response.choices[0].message.content);import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.assistantrouter.com/v1",
api_key=os.environ["ASSISTANTROUTER_API_KEY"],
)
response = client.chat.completions.create(
model="anthropic/claude-haiku-4.5",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"},
],
)
print(response.choices[0].message.content)curl https://api.assistantrouter.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ASSISTANTROUTER_API_KEY" \
-d '{
"model": "anthropic/claude-haiku-4.5",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"}
]
}'In direct mode, RAG (file_search) and web search tools are not available. Use an assistant to access these features.
Response
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1704067200,
"model": "anthropic/claude-haiku-4.5",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The capital of France is Paris."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 8,
"total_tokens": 33
},
"x_assistantrouter": {
"cost_cents": 0.15,
"assistant_id": "your-assistant-uuid",
"conversation_id": "conv_abc123",
"tools_used": [],
"was_fallback": false
}
}The x_assistantrouter extension includes:
cost_cents: The cost of this request in centsassistant_id: The assistant ID used for this requestconversation_id: The conversation ID (if provided or auto-created)tools_used: Array of tools that were invoked (e.g.,["web_search"])was_fallback: Whether a fallback model was used
Streaming
Set stream: true to receive partial responses as Server-Sent Events (SSE):
const stream = await client.chat.completions.create({
// @ts-ignore
assistant_id: 'your-assistant-uuid',
messages: [{ role: 'user', content: 'Tell me a story.' }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || '';
process.stdout.write(content);
}stream = client.chat.completions.create(
extra_body={"assistant_id": "your-assistant-uuid"},
messages=[{"role": "user", "content": "Tell me a story."}],
stream=True,
)
for chunk in stream:
content = chunk.choices[0].delta.content or ""
print(content, end="", flush=True)Tool Calling
Enable function calling by providing tools in your request:
const response = await client.chat.completions.create({
// @ts-ignore
assistant_id: 'your-assistant-uuid',
messages: [{ role: 'user', content: 'What is the weather in Paris?' }],
tools: [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather for a location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'The city and country',
},
},
required: ['location'],
},
},
},
],
});Errors
| Code | Description |
|---|---|
authentication_error | Invalid or missing API key |
insufficient_balance | Not enough credits in wallet |
rate_limit_exceeded | Request rate limit exceeded |
not_found | Assistant not found |
bad_request | Invalid request parameters |