MarsAI
Sign in Get Started

MarsAI developer guide

Call MarsAI with Python or JavaScript

Use the OpenAI SDK with the MarsAI base URL while keeping credentials out of source code.

Reviewed . Checked against the current MarsAI /v1 behavior and published catalog.

Before running the examples

Create a MarsAI key, store it as MARSAI_API_KEY, and copy an exact model ID from the model catalog. The examples use the OpenAI compatible chat completions endpoint.

shell
# macOS or Linux
export MARSAI_API_KEY='sk-mars-YOUR_KEY'

# PowerShell
$env:MARSAI_API_KEY = 'sk-mars-YOUR_KEY'

Never paste a live key into browser code, source control, screenshots or support messages.

Python

shell
python -m pip install openai
python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://marsai.click/v1",
    api_key=os.environ["MARSAI_API_KEY"],
)

response = client.chat.completions.create(
    model="YOUR_PUBLIC_MODEL_ID",
    messages=[{"role": "user", "content": "Explain what HTTP 429 means."}],
)

print(response.choices[0].message.content)
print(response.usage)

JavaScript

shell
npm install openai
javascript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://marsai.click/v1",
  apiKey: process.env.MARSAI_API_KEY,
});

const response = await client.chat.completions.create({
  model: "YOUR_PUBLIC_MODEL_ID",
  messages: [{ role: "user", content: "Explain what HTTP 429 means." }],
});

console.log(response.choices[0].message.content);
console.log(response.usage);

Inspect the response before indexing into it

A normal chat completion contains text under the first choice and a usage object. Wording and token counts differ by model and request. If the body contains error, or no choice exists, keep the status and body for error handling rather than reading choices[0] unconditionally.

Streaming and retries

Set stream=True in Python or stream: true in JavaScript, then consume the SDK's stream. A connection can fail after text has arrived. Mark that answer incomplete; do not replay it automatically if your application has already performed a side effect.

For nonstreaming requests, correct invalid credentials, model IDs and credit errors. Apply a small retry budget only to temporary rate limits or server errors. The API docs contain more request shapes.