Mage SDK · TypeScript

The Mage TypeScript SDK

Generate images, video, and audio from Node.js with typed configs for every Mage model. One call submits, waits, and returns the result.

Get an API keyRead the TypeScript guideSource on GitHub
npm install @mage-space/sdk

At a glance

The Mage TypeScript SDK is the official, open-source client for the Mage API, published as @mage-space/sdk. Install it with "npm install @mage-space/sdk", set MAGE_API_KEY, and call mage.run with a model id and a config to get finished images, video, or audio from any of 28 models. It runs on Node.js 20.19 or later, or any server runtime with fetch, and every generation is paid in Gems.

Setup

Install the TypeScript SDK and run a model

  1. Install the package

    Add @mage-space/sdk to your project.

    npm install @mage-space/sdk
  2. Set your API key

    Create a key in API → API Keys on mage.space and export it. The client reads MAGE_API_KEY.

    export MAGE_API_KEY="mage_sk_..."
  3. Generate

    Call run with a model id and its config. It waits for the result and returns it; the output is at result.url.

    import Mage from '@mage-space/sdk';
    
    const mage = new Mage(); // reads MAGE_API_KEY
    
    const request = await mage.run('mango', {
      prompt: 'Editorial portrait in soft daylight, 35mm film look',
      aspect_ratio: '4:5',
      model_id: 'mango-v3',
    });
    
    console.log(request.result.url);

Examples

The TypeScript SDK in practice

Each example starts from a client that reads MAGE_API_KEY.

Submit now, wait later
import Mage from '@mage-space/sdk';

const mage = new Mage(); // reads MAGE_API_KEY

const submitted = await mage.generate('cherry', {
  prompt: 'Waves rolling onto a black sand beach at sunset',
  resolution: '720p',
  duration: '5',
});

const final = await mage.requests.wait(submitted.request_id, {
  timeout: 15 * 60_000,
  onUpdate: (request) => console.log(request.status),
});
Upload a file
import { readFile } from 'node:fs/promises';
import Mage from '@mage-space/sdk';

const mage = new Mage(); // reads MAGE_API_KEY

const clip = await mage.uploads.upload(await readFile('clip.mp4'), {
  contentType: 'video/mp4',
});

await mage.run('cherry', {
  prompt: 'Restyle this clip as a watercolor painting',
  videos: [clip.url],
});
Use a saved character
import Mage from '@mage-space/sdk';

const mage = new Mage(); // reads MAGE_API_KEY

await mage.characters.create({
  name: 'Ana',
  handle: 'ana',
  image: 'https://example.com/ana.png',
});

await mage.run('mango', { prompt: '@ana walking through a night market' });
Handle errors
import Mage, { MageAPIError, MageGenerationError } from '@mage-space/sdk';

const mage = new Mage(); // reads MAGE_API_KEY

try {
  await mage.run('mango', { prompt: 'A lighthouse at night' });
} catch (error) {
  if (error instanceof MageAPIError && error.code === 'insufficient_gems') {
    // Top up Gems, then try again.
  } else if (error instanceof MageGenerationError) {
    console.log('No output:', error.code);
  } else {
    throw error;
  }
}

Why the SDK

What the SDK handles for you

One call, finished media

run submits the generation, polls with the backoff the API recommends, and resolves to the completed request with result.url.

Retries that never pay twice

Every submission carries an idempotency key. Network errors and server errors are retried with backoff, and a retried submission is never charged again.

Uploads in one call

Files too large for a data URL go through a signed upload, with the ticket, the PUT, and the size check handled for you.

Clear errors

API refusals carry their HTTP status, error code, and request id; failed or cancelled generations and timeouts have their own error types.

Typed for every model

MangoConfig, CherryConfig, and the rest are generated from the API reference, so your editor completes fields and catches invalid options.

Zero dependencies

ESM with no runtime dependencies. It uses the fetch built into Node.js, and every call takes an AbortSignal.

Reference

The TypeScript SDK at a glance

The same methods cover every model; the model is the first argument.

MethodWhat it does
mage.run(model, config)Submit a generation, wait for it, and return the completed request.
mage.generate(model, config)Submit a generation and return the request at once.
mage.requests.get / cancel / waitRead, stop, or wait for a request by id.
mage.uploads.upload(data)Upload a file of up to 100 MB and return its URL.
mage.characters / mage.referencesList, create, and delete saved characters and references.
mage.account.get()Your Gems balance.
mage.architectures.list()The live model catalog with options and prices.

Models

Start with these models

Image · from 37 Gems

Guava API

Photorealism that passes for a photograph

Image · from 135 Gems

Mango API

Our flagship image family

Image · from 68 Gems

Nano Banana 2 API

Crisp, readable text, up to 4K

Video · from 618 Gems

Cherry API

Our flagship video family

Video · from 245 Gems

Lemon API

Every video feature, at a friendlier price

Audio · from 19 Gems

Seed Audio API

Voices, music, and sound effects from one prompt

FAQ

Frequently asked questions

Can I use the Mage TypeScript SDK in the browser?

No. An API key spends your Gems, so keep it on your servers. The API sends no CORS headers, so browsers cannot call it directly; call your own backend instead, and let it use the SDK.

Does the TypeScript SDK work with CommonJS?

The package is ESM-only. Node.js 20.19 and later can require() it from CommonJS too.

Does the TypeScript SDK work with Next.js?

Yes, in server code such as route handlers and server actions. The SDK is built on fetch and standard Web APIs such as AbortSignal, so other server runtimes with fetch should work too, though CI tests it on Node.js.

Are the Mage SDKs free and open source?

Yes. The SDKs and the ComfyUI nodes are MIT-licensed on GitHub, and installing them is free. You pay only for the generations you run, in Gems, at each model’s listed price.

Do new Mage models work with an older SDK?

Yes. Pass the new model’s id as a string and it runs; the SDK checks its config against the fields every model shares. A bot updates the types whenever the API reference changes, so new models arrive with their own types in the next release.

How do the SDKs avoid paying twice for a retried request?

Every submission carries an Idempotency-Key. The SDK makes a new key per call and reuses it when it retries that call itself, so a retried submission returns the original request and charges nothing.

Are the SDKs stable?

They are in beta at 0.x, so a minor version may still change them. The API itself is versioned separately and stays stable within v1.

How much does the Mage API cost?

There is no subscription or monthly fee. Each generation is paid in Gems at its model's listed price, and 1,000 Gems cost $1 on the standard pack. Every model page lists its default-settings price, and each response reports the exact charge. A generation that fails is refunded; one Mage's content policy blocks is not. A request your balance cannot cover is refused before anything is charged.

Do I need a Mage membership to use the API?

No. The API runs on Gems alone. Membership plans and their unlimited in-app generation do not apply to API requests, so any Mage account with Gems can call it.

Can I use API outputs commercially?

Yes. Content you create with Mage, including through the API and MCP server, can be used commercially. Attribution is appreciated but not required.

Start building with Mage

One account, one Gems balance, every model. Pay only for what you generate.

Get an API keyRead the TypeScript guide

Related