Skip to content

JavaScript Examples

Chat completion

const response = await fetch("https://api.studiolm.dev/v1/chat/completions", {
  method: "POST",
  headers: {
    "Authorization": "Bearer sk-...",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "gemma-3-12b-it-qat",
    messages: [
      { role: "system", content: "You are a helpful assistant." },
      { role: "user", content: "What is the capital of France?" },
    ],
  }),
});

const data = await response.json();
console.log(data.choices[0].message.content);

Streaming

const response = await fetch("https://api.studiolm.dev/v1/chat/completions", {
  method: "POST",
  headers: {
    "Authorization": "Bearer sk-...",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "gemma-3-12b-it-qat",
    messages: [{ role: "user", content: "Write a haiku about the ocean." }],
    stream: true,
  }),
});

const reader = response.body.getReader();
const decoder = new TextDecoder();

while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  for (const line of decoder.decode(value).split("\n")) {
    if (!line.startsWith("data: ") || line.includes("[DONE]")) continue;
    const chunk = JSON.parse(line.slice(6));
    process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
  }
}

Image generation

const response = await fetch("https://api.studiolm.dev/v1/images/generations", {
  method: "POST",
  headers: {
    "Authorization": "Bearer sk-...",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    prompt: "A futuristic cityscape at night, neon lights",
    style: "vivid",
    aspect_ratio: "landscape",
    response_format: "url",
  }),
});

const data = await response.json();
console.log(data.data[0].url);
const response = await fetch("https://api.studiolm.dev/v1/chat/completions", {
  method: "POST",
  headers: {
    "Authorization": "Bearer sk-...",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "gemma-3-27b-it-qat",
    messages: [{ role: "user", content: "What's the latest in AI this week?" }],
    web_search: "auto",
  }),
});

const data = await response.json();
console.log(data.choices[0].message.content);

Multimodal (image in chat)

async function imageToBase64(url) {
  const res = await fetch(url);
  const buffer = await res.arrayBuffer();
  return btoa(String.fromCharCode(...new Uint8Array(buffer)));
}

const b64 = await imageToBase64("https://example.com/photo.jpg");

const response = await fetch("https://api.studiolm.dev/v1/chat/completions", {
  method: "POST",
  headers: {
    "Authorization": "Bearer sk-...",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "gemma-3-12b-it-qat",
    messages: [{
      role: "user",
      content: [
        { type: "text", text: "Describe this image." },
        { type: "image_url", image_url: { url: `data:image/jpeg;base64,${b64}` } },
      ],
    }],
  }),
});

const data = await response.json();
console.log(data.choices[0].message.content);