Chat Completions
POST /v1/chat/completions
Creates a chat completion. This endpoint is fully compatible with the OpenAI Chat Completions API.
Authentication
Include your API key in the Authorization header:
Authorization: Bearer sk-your-xmai-keyRequest Body
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Model ID, e.g. gpt-4o, claude-sonnet-4-20250514, gemini-2.5-pro |
messages | array | Yes | Array of message objects [{role, content}] |
stream | boolean | No | Whether to stream the response via SSE. Default: false |
temperature | number | No | Sampling temperature, range 0–2. Default: 1 |
max_tokens | number | No | Maximum number of tokens to generate |
top_p | number | No | Nucleus sampling parameter, range 0–1 |
frequency_penalty | number | No | Frequency penalty, range -2 to 2 |
presence_penalty | number | No | Presence penalty, range -2 to 2 |
stop | string | array | No | Stop sequence(s) |
Message Object
| Field | Type | Description |
|---|---|---|
role | string | One of system, user, assistant |
content | string | The message content |
Response (Non-Streaming)
json
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1710000000,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 12,
"total_tokens": 22
}
}Response (Streaming)
When stream: true, the response is sent as Server-Sent Events (SSE).
Each event is a JSON object prefixed with data: :
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1710000000,"model":"gpt-4o","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1710000000,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1710000000,"model":"gpt-4o","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]Code Examples
Basic Request
python
from openai import OpenAI
client = OpenAI(
api_key="sk-your-xmai-key",
base_url="https://api.xmai.sg/v1",
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"},
],
temperature=0.7,
)
print(response.choices[0].message.content)javascript
import OpenAI from 'openai'
const client = new OpenAI({
apiKey: 'sk-your-xmai-key',
baseURL: 'https://api.xmai.sg/v1',
})
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is the capital of France?' },
],
temperature: 0.7,
})
console.log(response.choices[0].message.content)go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
body := map[string]interface{}{
"model": "gpt-4o",
"messages": []map[string]string{
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"},
},
"temperature": 0.7,
}
jsonBody, _ := json.Marshal(body)
req, _ := http.NewRequest("POST",
"https://api.xmai.sg/v1/chat/completions",
bytes.NewBuffer(jsonBody))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer sk-your-xmai-key")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
fmt.Println(string(data))
}bash
curl https://api.xmai.sg/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-xmai-key" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"}
],
"temperature": 0.7
}'Streaming
python
from openai import OpenAI
client = OpenAI(
api_key="sk-your-xmai-key",
base_url="https://api.xmai.sg/v1",
)
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Tell me a short story."}],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")javascript
import OpenAI from 'openai'
const client = new OpenAI({
apiKey: 'sk-your-xmai-key',
baseURL: 'https://api.xmai.sg/v1',
})
const stream = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Tell me a short story.' }],
stream: true,
})
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content
if (content) process.stdout.write(content)
}go
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
)
func main() {
body := map[string]interface{}{
"model": "gpt-4o",
"messages": []map[string]string{{"role": "user", "content": "Tell me a short story."}},
"stream": true,
}
jsonBody, _ := json.Marshal(body)
req, _ := http.NewRequest("POST",
"https://api.xmai.sg/v1/chat/completions",
bytes.NewBuffer(jsonBody))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer sk-your-xmai-key")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "data: ") {
continue
}
data := strings.TrimPrefix(line, "data: ")
if data == "[DONE]" {
break
}
var chunk map[string]interface{}
if err := json.Unmarshal([]byte(data), &chunk); err != nil {
continue
}
choices := chunk["choices"].([]interface{})
delta := choices[0].(map[string]interface{})["delta"].(map[string]interface{})
if content, ok := delta["content"].(string); ok {
fmt.Print(content)
}
}
}bash
curl https://api.xmai.sg/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-xmai-key" \
-N \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Tell me a short story."}],
"stream": true
}'OpenAI SDK Compatibility
xmAI is fully compatible with the OpenAI SDK. To migrate from OpenAI, simply change the base_url:
| SDK | Parameter | Value |
|---|---|---|
| Python | base_url | https://api.xmai.sg/v1 |
| Node.js | baseURL | https://api.xmai.sg/v1 |
All other code remains exactly the same. You can use any model available on xmAI (including Claude, Gemini, etc.) with the same OpenAI SDK interface.