Integration
Beta gapsSDK compatibility
Use the official OpenAI packages against the MainAI base URL.
Updated 2026-10-25 · MainAI Compute docs
There is no separate MainAI SDK and no renamed package. Use the official OpenAI SDK for TypeScript or Python, point it at OPENAI_BASE_URL, and authenticate with an sk-mai_ key.
EnvironmentLink to this section
OPENAI_BASE_URL=https://cloud.mainai.dk/v1
OPENAI_API_KEY=sk-mai_…The official packages read both variables by default. The familiar OPENAI_API_KEY name keeps local scripts and framework integrations unchanged. A MainAI key works because only the prefix and bearer shape matter.
TypeScriptLink to this section
npm install openaiimport OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: process.env.OPENAI_BASE_URL,
maxRetries: 2,
});
const completion = await client.chat.completions.create({
model: "mainai-coder",
messages: [
{ role: "system", content: "Answer in one short paragraph." },
{ role: "user", content: "Explain what an API gateway allow-list does." },
],
max_tokens: 220,
});
console.log(completion.choices[0]?.message.content);
console.log(completion.usage?.prompt_tokens);import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: process.env.OPENAI_BASE_URL,
});
const stream = await client.chat.completions.create({
model: "mainai-coder",
messages: [{ role: "user", content: "Write a short changelog entry." }],
stream: true,
max_tokens: 300,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: process.env.OPENAI_BASE_URL,
});
try {
await client.chat.completions.create({
model: "mainai-coder",
messages: [{ role: "user", content: "Say ready" }],
max_tokens: 8,
});
} catch (error) {
if (error instanceof OpenAI.APIError) {
console.error({
status: error.status,
code: error.code,
requestId: error.headers?.get("x-request-id"),
});
} else {
throw error;
}
}PythonLink to this section
pip install openaiimport os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["OPENAI_API_KEY"],
base_url=os.environ["OPENAI_BASE_URL"],
max_retries=2,
)
completion = client.chat.completions.create(
model="mainai-coder",
messages=[
{"role": "system", "content": "Answer in one short paragraph."},
{"role": "user", "content": "Explain what an API gateway allow-list does."},
],
max_tokens=220,
)
print(completion.choices[0].message.content)
print(completion.usage.prompt_tokens)import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["OPENAI_API_KEY"],
base_url=os.environ["OPENAI_BASE_URL"],
)
batch = client.embeddings.create(
model="mainai-coder",
input=["first document", "second document"],
)
for item in batch.data:
print(item.index, len(item.embedding))import asyncio
import os
from openai import AsyncOpenAI
client = AsyncOpenAI(
api_key=os.environ["OPENAI_API_KEY"],
base_url=os.environ["OPENAI_BASE_URL"],
)
async def main() -> None:
stream = await client.chat.completions.create(
model="mainai-coder",
messages=[{"role": "user", "content": "Write a short changelog entry."}],
stream=True,
max_tokens=300,
)
async for chunk in stream:
print(chunk.choices[0].delta.content or "", end="", flush=True)
asyncio.run(main())Responses APILink to this section
response = client.responses.create(
model="mainai-cyber",
input="List three checks before a production restore.",
)
print(response.output_text)Responses is passthrough. If a typed property is missing from the SDK version, read the raw response or use chat completions.
Framework notesLink to this section
- Next.js / serverless: keep the key in server-only env. Do not import the client into a
"use client"module. - Browser: the SDK works because
/v1sends CORS headers, but a key in the bundle is public. Use a thin server proxy for anything shared. - Workers / queues: create one client per process, not per job. Keep
maxRetrieslow and add your own queue backoff on429. - Observability: wrap calls and log
x-request-idfromerror.headers. Do not log prompts by default.
Retry behaviorLink to this section
The SDKs retry connection errors, 408, 409, 429 and 5xx by default. Two retries is a sane production cap. Set maxRetries: 0 where your queue owns retries, so a job is not retried twice.
Raw HTTPLink to this section
If you do not want a dependency, every operation has a curl example in the API reference. Use plain fetch in TypeScript or urllib/httpx in Python.