---
title: "Call a model with REST"
description: "Call a TIR Token Factory model with a plain HTTP request from curl or any HTTP client, including a streamed reply."
content_type: "how-to"
product: "token-factory"
platform: "TIR"
url: "/tir/token-factory/how-to/call-a-model-with-rest/"
topic: ["call-a-model"]
---

# Call a model with REST

<!-- cspell:ignore gemma -->

Send a chat completion to any Token Factory model with one HTTP request, from curl or any language
with an HTTP client. Every model shares the base URL `https://blaze.e2enetworks.net/v1`, and the
model ID in the request body decides which model answers.

You need a Token Factory API key from your project; to create one, see [Create an API
key](/tir/token-factory/how-to/create-an-api-key/). The examples read it from an environment
variable:

```bash
export TOKEN_FACTORY_API_KEY="<your-api-key>"
```

`<your-api-key>` is the key you copied when you created it.

## Send a request

POST the conversation to `/chat/completions` under the base URL, with the key in the Authorization
header:

```bash
curl https://blaze.e2enetworks.net/v1/chat/completions \
  -H "Authorization: Bearer $TOKEN_FACTORY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "google/gemma-4-31b-it",
    "messages": [{"role": "user", "content": "Implement hello world in Python"}],
    "max_tokens": 1024
  }'
```

Write the header exactly as `Authorization: Bearer <your-api-key>`, with a capital B. A key sent in any
other header, such as `x-api-key`, is not read, and the request is refused.

The body's `model` is the full model ID from Token Factory's model list, such as
`google/gemma-4-31b-it`. `messages` is the conversation so far, and each message has the role
system, user, assistant, or tool. `max_tokens` caps how many tokens the reply can use. Other
optional fields include `temperature`, `stop`, and `seed`. Each model accepts its own set of
optional fields, and a field it does not support can be refused with a 400 error.

## Read the reply

The reply is JSON in the OpenAI shape:

```json
{
  "id": "<completion-id>",
  "object": "chat.completion",
  "model": "google/gemma-4-31b-it",
  "choices": [
    {
      "index": 0,
      "message": { "role": "assistant", "content": "<the model's reply>" },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 14,
    "completion_tokens": 52,
    "total_tokens": 66,
    "prompt_tokens_details": { "cached_tokens": 0 }
  }
}
```

The reply text is in the first choice's message. The usage counts the tokens your project is billed
for, and the cached tokens are the part of the prompt served from the prompt cache; on some models
that detail is empty. Every reply also carries an `X-Request-Id` header. Keep it with your logs: it
is what support asks for when you report a failed request. To see the headers with curl, add `-i` to
the command.

## Stream the reply

Add `"stream": true` to get the reply as server-sent events while the model writes it, and pass
`-N` so curl prints each event as it arrives. Stream long replies: a request that is not streamed
fails if the whole reply takes longer than four minutes.

```bash
curl https://blaze.e2enetworks.net/v1/chat/completions \
  -H "Authorization: Bearer $TOKEN_FACTORY_API_KEY" \
  -H "Content-Type: application/json" \
  -N \
  -d '{
    "model": "google/gemma-4-31b-it",
    "messages": [{"role": "user", "content": "Count from 1 to 10."}],
    "stream": true
  }'
```

Each event is a line that starts with `data:` and holds one chunk of the reply. The chunk that ends
the reply has its finish reason set; after it comes one chunk with the token usage and no choices,
and the stream ends with `data: [DONE]`. When your code reads the stream, have it skip chunks
that have no choices.

To leave out the usage chunk, add `stream_options` to the request body, next to `"stream": true`:

```bash
curl https://blaze.e2enetworks.net/v1/chat/completions \
  -H "Authorization: Bearer $TOKEN_FACTORY_API_KEY" \
  -H "Content-Type: application/json" \
  -N \
  -d '{
    "model": "google/gemma-4-31b-it",
    "messages": [{"role": "user", "content": "Count from 1 to 10."}],
    "stream": true,
    "stream_options": {"include_usage": false}
  }'
```

A stream that stops before a chunk with a finish reason has failed partway. Send the request again.

To call a model from Python or Node instead, see [Call a model with the OpenAI
SDK](/tir/token-factory/how-to/call-a-model-with-the-openai-sdk/). If a request returns an error,
[Troubleshooting TIR Token Factory](/tir/token-factory/troubleshooting/token-factory/) explains each
one.
