Skip to content
Open app

JavaScript & Python SDKs

Use the official OpenAI SDKs with a custom base URL and your Moonez.ai key. The same client calls Claude and GPT: change the model, not the integration. These examples run on your server, not in the browser.

Complete the authentication setup, then list available models. Choose an ID whose endpoints includes /v1/chat/completions.

For JavaScript or TypeScript:

Terminal window
pnpm add openai

For Python:

Terminal window
pip install --upgrade openai

Set server-side environment variables. Replace the example model if it is not listed for your key; a GPT ID such as gpt-4o uses the same code when available.

Terminal window
export MOONEZ_API_KEY="YOUR_MOONEZ_API_KEY"
export MOONEZ_MODEL="claude-haiku-4-5"
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.MOONEZ_API_KEY,
baseURL: "https://api.moonez.ai/v1",
maxRetries: 0,
});
const models = await client.models.list();
for (const model of models.data) console.log(model.id);
const response = await client.chat.completions.create({
model: process.env.MOONEZ_MODEL,
messages: [{ role: "user", content: "Explain idempotency in one sentence." }],
});
console.log(response.choices[0]?.message?.content ?? "");

Use an ES module, for example example.mjs, and run it with node example.mjs. TypeScript uses the same client and methods; validate that the environment variables are defined before passing them to the SDK.

import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["MOONEZ_API_KEY"],
base_url="https://api.moonez.ai/v1",
max_retries=0,
)
for model in client.models.list().data:
print(model.id)
response = client.chat.completions.create(
model=os.environ["MOONEZ_MODEL"],
messages=[{"role": "user", "content": "Explain idempotency in one sentence."}],
)
if response.choices:
print(response.choices[0].message.content or "")

The SDK adds Authorization: Bearer automatically. Keep /v1 in baseURL (JavaScript) or base_url (Python); do not append /chat/completions there. maxRetries: 0 / max_retries=0 disables the SDK’s automatic retries so an ambiguous generation failure does not silently submit another request. Decide how to recover based on the actual error and whether the request was accepted.

Reuse the configured client and selected model from above.

const stream = await client.chat.completions.create({
model: process.env.MOONEZ_MODEL,
messages: [{ role: "user", content: "Write a short welcome message." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
process.stdout.write("\n");
stream = client.chat.completions.create(
model=os.environ["MOONEZ_MODEL"],
messages=[{"role": "user", "content": "Write a short welcome message."}],
stream=True,
)
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="", flush=True)
print()

These minimal examples print text deltas. Tool calls and other model-specific stream fields need their own handling. Not every chunk contains text, and the choices array may be empty. Usage is available only when returned by the model; do not infer final usage from the first chunk.

Choose a GPT Image ID whose endpoints includes /v1/images/generations, then set it separately from your chat model:

Terminal window
export MOONEZ_IMAGE_MODEL="gpt-image-2"
const image = await client.images.generate({
model: process.env.MOONEZ_IMAGE_MODEL,
prompt: "A ceramic cup in soft studio lighting.",
});
console.log(image.data);
image = client.images.generate(
model=os.environ["MOONEZ_IMAGE_MODEL"],
prompt="A ceramic cup in soft studio lighting.",
)
print(image.data)

The image response may contain base64 data or URLs, depending on the selected model. Handle the response actually returned instead of assuming a specific format. The SDK does not make unsupported operations available: check the compatibility notes before adding Responses, image edits, or model-specific parameters.

The client configuration and methods follow the official OpenAI SDKs: JavaScript Chat Completions, Python model discovery, JavaScript Images, and Python Images. OpenAI’s own model availability and pricing do not define your Moonez.ai catalog or account rates.