Skip to content
main-compute

Integration

Beta gaps

SDK 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

.env (server-side only)
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

Install
npm install openai
Chat, non-streaming
import 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);
Streaming
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 ?? "");
}
Typed error handling
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

Install
pip install openai
Chat, non-streaming
import 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)
Embeddings
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))
Async streaming
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

Responses passthrough
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 /v1 sends 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 maxRetries low and add your own queue backoff on 429.
  • Observability: wrap calls and log x-request-id from error.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.