POST
/
chat
/
completions
Chat Completion
curl --request POST \
  --url https://api.xtrix.workers.dev/v1/chat/completions \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "system",
      "content": "<string>"
    }
  ]
}'
{
  "id": "<string>",
  "object": "chat.completion",
  "created": 123,
  "model": "<string>",
  "choices": [
    {
      "index": 123,
      "message": {
        "role": "system",
        "content": "<string>"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 123,
    "completion_tokens": 123,
    "total_tokens": 123
  }
}

Request Body

model
string
default:"claude-3-5-sonnet-20241022"
Model to use. Default is claude-3-5-sonnet-20241022.
messages
array
required
Array of message objects with role and content.
stream
boolean
default:false
Stream the response.
temperature
number
default:1
Sampling temperature (0-2).
max_tokens
integer
Maximum tokens to generate.

Example Request

curl https://api.xtrix.workers.dev/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "claude-3-5-sonnet-20241022",
    "messages": [
      {
        "role": "user",
        "content": "What is the capital of France?"
      }
    ]
  }'

Response

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1677858242,
  "model": "claude-3-5-sonnet-20241022",
  "system_fingerprint": "fp_44709d6f",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The capital of France is Paris."
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 8,
    "total_tokens": 18
  }
}

Streaming

Set stream: true to receive responses as they’re generated:
const stream = await openai.chat.completions.create({
  model: 'claude-3-5-sonnet-20241022',
  messages: [{ role: 'user', content: 'Tell me a story' }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || '');
}

Streaming Response Format

Each chunk in the stream follows this format:
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion.chunk",
  "created": 1677858242,
  "model": "claude-3-5-sonnet-20241022",
  "system_fingerprint": "fp_44709d6f",
  "choices": [
    {
      "index": 0,
      "delta": {
        "content": "The"
      },
      "logprobs": null,
      "finish_reason": null
    }
  ]
}
The final chunk includes usage information:
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion.chunk",
  "created": 1677858242,
  "model": "claude-3-5-sonnet-20241022",
  "system_fingerprint": "fp_44709d6f",
  "choices": [
    {
      "index": 0,
      "delta": {},
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 25,
    "total_tokens": 35
  }
}
The stream ends with:
data: [DONE]

Authorizations

Authorization
string
header
required

API key authentication

Body

application/json

Response

200
application/json

Successful response

The response is of type object.