One call, finished media
run submits the generation, polls with the backoff the API recommends, and resolves to the completed request with result.url.
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.
20.19+
Node.js
0
Runtime dependencies
28
Typed models
MIT
Open source
Setup
Add @mage-space/sdk to your project.
npm install @mage-space/sdkCreate a key in API → API Keys on mage.space and export it. The client reads MAGE_API_KEY.
export MAGE_API_KEY="mage_sk_..."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
Each example starts from a client that reads MAGE_API_KEY.
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),
});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],
});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' });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
run submits the generation, polls with the backoff the API recommends, and resolves to the completed request with result.url.
Every submission carries an idempotency key. Network errors and server errors are retried with backoff, and a retried submission is never charged again.
Files too large for a data URL go through a signed upload, with the ticket, the PUT, and the size check handled for you.
API refusals carry their HTTP status, error code, and request id; failed or cancelled generations and timeouts have their own error types.
MangoConfig, CherryConfig, and the rest are generated from the API reference, so your editor completes fields and catches invalid options.
ESM with no runtime dependencies. It uses the fetch built into Node.js, and every call takes an AbortSignal.
Reference
The same methods cover every model; the model is the first argument.
| Method | What 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 / wait | Read, 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.references | List, 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
Image · from 37 Gems
Photorealism that passes for a photograph
Image · from 135 Gems
Our flagship image family
Image · from 68 Gems
Crisp, readable text, up to 4K
Video · from 618 Gems
Our flagship video family
Video · from 245 Gems
Every video feature, at a friendlier price
Audio · from 19 Gems
Voices, music, and sound effects from one prompt
FAQ
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.
The package is ESM-only. Node.js 20.19 and later can require() it from CommonJS too.
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.
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.
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.
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.
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.
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.
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.
Yes. Content you create with Mage, including through the API and MCP server, can be used commercially. Attribution is appreciated but not required.
One account, one Gems balance, every model. Pay only for what you generate.