> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tokenchange.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a text completion

> Stream a text completion from a TokenChange model with POST /v1/completions, using the OpenAI legacy completions format with a prompt string.

Streams a text completion for a prompt. The request and response follow the OpenAI completions format. For conversations, use [chat completions](https://docs.tokenchange.io/inference/chat-completions.md).

## Request

**Authorization** `string` *required*

`Bearer ask_...`, an [inference key](https://docs.tokenchange.io/guides/api-keys.md).

**model** `string` *required*

A model slug from the catalog, or `<model-slug>/ep/<endpoint>` to [pin one endpoint](https://docs.tokenchange.io/guides/models-and-endpoints.md#pin-a-request-to-one-endpoint).

**prompt** `string` *required*

The text to complete.

**stream** `boolean` *required*

Must be `true`.

**max\_tokens** `integer`

Maximum tokens to generate. At least `1`.

**temperature** `number`

Sampling temperature, from `0` to `2`.

**stream\_options.include\_usage** `boolean` (default: false)

Set to `true` to receive token usage in the final chunk.

**prompt\_cache\_key** `string`

Requests with the same key are routed to the same endpoint. See [Prompt caching](https://docs.tokenchange.io/guides/routing.md#prompt-caching).

**provider** `object`

Price options. See [Price options](https://docs.tokenchange.io/guides/routing.md#price-options).

Other OpenAI completion fields, such as `top_p`, `stop` and `seed`, are passed to the endpoint unchanged.

```bash curl theme={null}
curl https://inference.tokenchange.io/v1/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKENCHANGE_API_KEY" \
  -d '{
    "model": "<model-slug>",
    "stream": true,
    "max_tokens": 32,
    "prompt": "The three primary colors are"
  }'
```

```python Python theme={null}
stream = client.completions.create(
    model="<model-slug>",
    prompt="The three primary colors are",
    max_tokens=32,
    stream=True,
)

for chunk in stream:
    if chunk.choices:
        print(chunk.choices[0].text, end="")
```

```javascript JavaScript theme={null}
const stream = await client.completions.create({
  model: "<model-slug>",
  prompt: "The three primary colors are",
  max_tokens: 32,
  stream: true,
});

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

## Response

A stream of server-sent events. Each event's `data` is a JSON completion chunk with `object` set to `text_completion`, and the stream ends with `data: [DONE]`. Each choice carries the new `text` and, in its last chunk, a `finish_reason`. Usage works as in [chat completions](https://docs.tokenchange.io/inference/chat-completions.md#response).

```text Stream theme={null}
data: {"id":"cmpl-123","object":"text_completion","created":1767225600,"model":"<model-slug>","choices":[{"index":0,"text":" red, yellow and blue.","finish_reason":"stop"}]}

data: [DONE]
```

The values above are illustrative.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.