TokenFlux Docs

Chat Completions

Create OpenAI-compatible chat completions

POST /v1/chat/completions
Authorization: Bearer YOUR_TOKENFLUX_KEY
Content-Type: application/json
curl https://tokenflux.ai/v1/chat/completions \
  -H "Authorization: Bearer YOUR_TOKENFLUX_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "YOUR_MODEL_ID",
    "messages": [
      {"role": "user", "content": "Explain this API in one sentence."}
    ]
  }'

The endpoint accepts OpenAI-compatible chat-completion payloads. Select an available model ID from Models & pricing. Set stream to true when your client supports streamed responses.

Request fields

model selects a model available to your key. messages is the ordered conversation history; send earlier user and assistant messages to continue a conversation. A system message can set instructions where the model supports it.

Optional fields include stream, temperature, top_p, max_tokens, tools, tool_choice and response_format. Support and limits depend on the model and its native protocol. Do not assume that every model accepts every OpenAI option. For image-capable models, message content can contain text and image_url parts.

Response

A non-streaming response contains choices. Read the reply from choices[0].message.content, and inspect finish_reason to see why generation stopped. A tool call can return tool_calls in the assistant message instead of text. Token counts are in usage when supplied. This is an illustrative text response:

{
  "id": "chatcmpl-example",
  "object": "chat.completion",
  "created": 1734470400,
  "model": "YOUR_MODEL_ID",
  "choices": [
    {
      "index": 0,
      "message": { "role": "assistant", "content": "Hello!" },
      "finish_reason": "stop"
    }
  ],
  "usage": { "prompt_tokens": 12, "completion_tokens": 8, "total_tokens": 20 }
}

Streaming

With stream: true, the response uses text/event-stream. Read text from each chunk's choices[0].delta.content, rather than message.content. Not every chunk contains text. The stream ends with data: [DONE]; clients must also handle errors or a connection closing before completion.

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.TOKENFLUX_API_KEY,
  baseURL: "https://tokenflux.ai/v1",
});

const stream = await client.chat.completions.create({
  model: process.env.TOKENFLUX_MODEL,
  messages: [
    { role: "system", content: "You write concise summaries." },
    { role: "user", content: "Explain semantic search." },
  ],
  stream: true,
});

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

See the API overview for errors and retry guidance. Use Models to select an accessible model.

On this page