Skip to content
API quickstart

API quickstart

Create an API key and call a published model with curl, Python or JavaScript.

Your applications call your published models through Tensorant's API. It follows the OpenAI chat completions format, so the official OpenAI SDKs work with it: you change the base URL, the key and the model name.

This page gets you to a first answer. Chat completions is the full reference.

A published decision model answers on its own route, POST /v1/decisions, with a probability for each candidate of your questions, and refuses chat requests. Serve decision models has its requests and answers; keys, limits and errors are as on this page.

Before you start

  • A published model, from the Models page. Its name, such as support, is what you send as model.
  • An owner of the organization to create the key.

Step 1: Copy the API address

Tensorant's API base URL is https://api.tensorant.io/v1. You can also copy it from API keys under Use in the sidebar with Copy address.

Step 2: Create a key

  1. On API keys, choose Create key. Beside the list of keys, "API keys: N of M" shows your organization's limit; once it's reached, Create key is unavailable.
  2. Enter a Name, such as "Production website". Keep the other settings for now.
  3. Choose Create key.
  4. In Copy your key, choose Copy key, store the key with your application's secrets, then choose I copied it. The key is shown only this once.

Keys start with ti_. Keep them on your servers, never in a web page or an app you ship. The examples below read the key from the TENSORANT_API_KEY environment variable:

bash
export TENSORANT_API_KEY="ti_..."

Step 3: Send a request

Replace your-model-name with the name of your published model.

curl

bash
curl https://api.tensorant.io/v1/chat/completions \
  -H "Authorization: Bearer $TENSORANT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "your-model-name", "messages": [{"role": "user", "content": "Hello"}]}'

Python

Install the SDK with pip install openai, then:

python
import os

from openai import OpenAI

client = OpenAI(base_url="https://api.tensorant.io/v1", api_key=os.environ["TENSORANT_API_KEY"])

answer = client.chat.completions.create(
    model="your-model-name",
    messages=[{"role": "user", "content": "Hello"}],
    max_tokens=512,
)
print(answer.choices[0].message.content)

JavaScript

Install the SDK with npm install openai, then, in an ES module on Node.js:

javascript
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.tensorant.io/v1", apiKey: process.env.TENSORANT_API_KEY });

const answer = await client.chat.completions.create({
  model: "your-model-name",
  messages: [{ role: "user", content: "Hello" }],
  max_tokens: 512,
});
console.log(answer.choices[0].message.content);

The Quickstart panel on API keys shows the same request with a published model filled in, a system prompt, and the API's defaults written out (temperature 0 and max_tokens 512); with several models, choose one under Model. Pick the curl, Python or JavaScript tab, then Copy code. Like the examples, it reads the key from TENSORANT_API_KEY. In the Playground, Get code gives this code for a published model you are trying, with the settings you chose.

The API has only two routes: chat completions and list models. Others, such as OpenAI's responses, embeddings or client.models.retrieve(), answer 404 not_found.

Stream the answer

Add stream: true to receive the answer as it is written. Add stream_options with include_usage to get the token counts in a last chunk.

python
stream = client.chat.completions.create(
    model="your-model-name",
    messages=[{"role": "user", "content": "Write a haiku about the sea."}],
    stream=True,
    stream_options={"include_usage": True},
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
    if chunk.usage:
        print(f"\n{chunk.usage.prompt_tokens} in, {chunk.usage.completion_tokens} out")

In JavaScript, pass the same stream: true and loop with for await (const chunk of stream). See Streaming for the format.

List your models

GET /v1/models lists the published models the key may call, including stopped ones. In the SDKs: client.models.list().

bash
curl https://api.tensorant.io/v1/models -H "Authorization: Bearer $TENSORANT_API_KEY"

When the model is starting

A wake-on-request model that is asleep starts on the first request, and the API waits up to a minute for it. If the model is still starting, the API answers 503 with the code model_starting and a Retry-After header. Wait that many seconds and send the request again. See Cold starts and retries.

When something goes wrong

What you see What to do
401 invalid_api_key Check that TENSORANT_API_KEY holds the whole key, and that the key shows Active on API keys.
404 model_not_found Check the name with GET /v1/models, and the key's May call.
404 not_found Send to /v1/chat/completions or /v1/models, after the address that ends in /v1.
503 model_unavailable: "The model 'name' is stopped. Start it on the Models page of the console." An owner starts it on the Models page.
403 plan_upgrade_required A plan Tensorant set up for your organization leaves images out. Send text only, or write to [email protected]. See Images, RL and decision models.

For every other answer, see Errors.

Next steps

Need a hand? Visit troubleshooting