AI chat API
An OpenAI-compatible endpoint for chat completions, with streaming and tool use.
2 min read
On this page›
The AI chat API is OpenAI-compatible: point an OpenAI SDK at AiTM's base URL with one of your API keys, and it works. Each call uses the same model and guardrails as the AI assistant in the app.
- Base URL:
https://api.aitm.io/api/v1/ai - Scope:
ai_assistant:writefor chat,ai_assistant:readfor models.
List the models you can use#
Request
curl https://api.aitm.io/api/v1/ai/models \
-H "Authorization: Bearer pk_YOUR_KEY_HERE"Response
{
"object": "list",
"data": [
{ "id": "default", "object": "model", "owned_by": "AiTM" }
]
}Use any id from the list as the model field in a chat request.
Send a message#
POST /api/v1/ai/chat/completions
POST /api/v1/ai/chat/completions
Authorization: Bearer pk_YOUR_KEY_HERE
Content-Type: application/json
{
"model": "default",
"messages": [
{ "role": "user", "content": "Summarise what AiTM does." }
]
}The response is in the familiar chat.completion shape:
JSON
{
"id": "chatcmpl-...",
"object": "chat.completion",
"created": 1730000000,
"model": "default",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "AiTM is..." },
"finish_reason": "stop"
}
]
}Stream the answer#
Set stream: true and read Server-Sent Events:
Bash
curl -N https://api.aitm.io/api/v1/ai/chat/completions \
-H "Authorization: Bearer pk_YOUR_KEY_HERE" \
-H "Content-Type: application/json" \
-d '{
"model": "default",
"stream": true,
"messages": [{ "role": "user", "content": "Hello" }]
}'Each line starts with data: and carries an incremental chat.completion.chunk.
The stream ends with data: [DONE].
Use with the OpenAI SDK#
Python
from openai import OpenAI
client = OpenAI(
api_key="pk_YOUR_KEY_HERE",
base_url="https://api.aitm.io/api/v1/ai",
)
stream = client.chat.completions.create(
model="default",
messages=[{"role": "user", "content": "Hello"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")JavaScript
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "pk_YOUR_KEY_HERE",
baseURL: "https://api.aitm.io/api/v1/ai",
});
const stream = await client.chat.completions.create({
model: "default",
messages: [{ role: "user", content: "Hello" }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0].delta.content ?? "");
}Tools and attachments#
- Tools — pass
toolsin the OpenAI function-tools shape to let the model call your own functions. You gettool_callsback and reply withrole: "tool"messages. - Images — attach images as base64 data URLs in message content parts
(
image_urlwith adata:image/png;base64,...URL). - PDFs — attach PDFs as base64
fileparts withdata:application/pdf;base64,....
Limits and errors#
- Requests are rate-limited; busy periods return
429with aRetry-Afterheader. - Errors use OpenAI's error shape —
{"error": {"message", "type", "code"}}.insufficient_quotameans your plan's AI allowance is used up.
Model and provider#
The underlying model and provider are not exposed — not in answers,
headers, errors, or the model field. The API only ever names the public
id you asked for.
See also#
- API keys Create a key with the scopes this endpoint needs.
- Bring your own key Route your usage through your own AI provider key.
- AI usage How much of your allowance you have left.