API documentation

The Trex API lets your software send messages to Trex and receive replies. Replies are generated by Trex's own model.

Base URL: https://api.trex.ai/v1

The API is in early access. Endpoints on this page are stable for the early access period.

Authentication

Send your secret key in the Authorization header as a bearer token on every request.

Authorization: Bearer trex_your_key

Keys begin with trex_. A key is shown once when created. Store it in a secrets manager or an environment variable.

Call the API from your server only. A key in browser code or a mobile app can be read by anyone who has the app.

Chat

POST /v1/chat

Sends one message and returns the reply.

Request body

FieldTypeDescription
message requiredstringThe user's message. Up to 8,000 characters.
system optionalstringInstructions that shape the reply. Up to 2,000 characters.
history optionalarrayEarlier turns, each an object with role (user or assistant) and content. The last 20 turns are used.

Example

curl https://api.trex.ai/v1/chat \
  -H "Authorization: Bearer trex_your_key" \
  -H "Content-Type: application/json" \
  -d '{"message": "Summarize photosynthesis in two sentences."}'
import requests

r = requests.post(
    "https://api.trex.ai/v1/chat",
    headers={"Authorization": "Bearer trex_your_key"},
    json={"message": "Summarize photosynthesis in two sentences."},
)
r.raise_for_status()
print(r.json()["reply"])
const res = await fetch("https://api.trex.ai/v1/chat", {
  method: "POST",
  headers: {
    "Authorization": "Bearer trex_your_key",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ message: "Summarize photosynthesis in two sentences." }),
});
const data = await res.json();
console.log(data.reply);

Response

{
  "reply": "Photosynthesis converts light, water, and carbon dioxide into glucose and oxygen.",
  "tokens_used": 58,
  "balance": 49942
}
FieldTypeDescription
replystringTrex's response.
tokens_usedintegerTokens charged for this request, input and output combined.
balanceintegerTokens left in your account after this request.

Tokens and balance

Usage is measured in tokens. Each request is charged for the text sent to the model, including system and history, plus the text of the reply.

Free accounts receive 50,000 tokens each day, up to a balance of 50,000. Pro accounts receive 1,000,000 each day, up to 3,000,000.

If the balance is lower than the request needs, the API returns 402 without charging.

Rate limits

Each key may send a limited number of requests per minute. When you exceed it, the API returns 429. Wait for the period in the Retry-After header and try again.

Errors

Errors return a JSON body with a detail field that describes the problem.

StatusMeaning
400The request body is missing a field or a value is too long.
401The key is missing, wrong, or revoked.
402The token balance is too low for this request.
429Too many requests. Retry after the time in Retry-After.
500Something failed on Trex's side. Retry with backoff.