Getting started

Code samples

There is no SDK to install. The API is small enough that a thin wrapper covers it; copy one of these into your project.

Each sample reads the key from AIO_KEY, raises a typed error with code and hint, and includes a run() helper that creates a generation and polls it to completion.

Clients#

// pixwellai.js — minimal client, no dependencies (Node 18+, Deno, Bun, browsers)
const BASE = "https://<your-host>/api/v1";

export class PixwellAIError extends Error {
  constructor(status, body) {
    super(body.error?.message ?? `HTTP ${status}`);
    this.status = status;
    this.code = body.error?.code;
    this.hint = body.error?.hint;
  }
}

export function client(key = process.env.AIO_KEY) {
  async function call(method, path, body, extraHeaders = {}) {
    const res = await fetch(BASE + path, {
      method,
      headers: {
        Authorization: `Bearer ${key}`,
        ...extraHeaders,
        ...(body && !(body instanceof FormData) ? { "Content-Type": "application/json" } : {}),
      },
      body: body instanceof FormData ? body : body ? JSON.stringify(body) : undefined,
    });
    const data = await res.json();
    if (!res.ok) throw new PixwellAIError(res.status, data);
    return data;
  }

  return {
    me: () => call("GET", "/me"),
    models: (q = {}) => call("GET", "/models?" + new URLSearchParams(q)), // { kind, family, task, q, limit, cursor }
    templates: (q = {}) => call("GET", "/templates?" + new URLSearchParams(q)), // { kind, category, q, sort, limit, cursor }
    async *all(path, q = {}) {
      // walk every page of a list endpoint
      let cursor;
      do {
        const page = await call("GET", `/${path}?` + new URLSearchParams({ ...q, limit: "100", ...(cursor ? { cursor } : {}) }));
        yield* page.data;
        cursor = page.next_cursor;
      } while (cursor);
    },
    estimate: (req) => call("POST", "/estimate", req),
    create: (req, idempotencyKey = crypto.randomUUID()) => call("POST", "/generations", req, { "Idempotency-Key": idempotencyKey }),
    get: (id) => call("GET", `/generations/${id}`),
    upload: (file) => { const f = new FormData(); f.append("file", file); return call("POST", "/uploads", f); },
    async run(req, { interval = 3000 } = {}) {
      let gen = await this.create(req);
      while (["queued", "processing"].includes(gen.status)) {
        await new Promise((r) => setTimeout(r, interval));
        gen = await this.get(gen.id);
      }
      return gen;
    },
  };
}

// usage
const aio = client();
const gen = await aio.run({ model: "flux-2-pro", prompt: "isometric tiny greenhouse, soft light" });
console.log(gen.status, gen.outputs.map((o) => o.url));

Image to video, end to end#

Upload a local photo, price the job, then animate it.

import { readFile } from "node:fs/promises";

const aio = client();
const photo = new Blob([await readFile("coffee.jpg")], { type: "image/jpeg" });
const asset = await aio.upload(photo);

const req = {
  model: "kling-2-5-turbo",
  prompt: "slow push-in, steam rising",
  params: { duration: 5 },
  inputs: { images: [asset.url] },
};
const { credits, sufficient } = await aio.estimate(req);
if (!sufficient) throw new Error(`needs ${credits} credits`);

const gen = await aio.run(req, { interval: 8000 });
console.log(gen.outputs[0]?.url ?? gen.error);