Migrating from Sora or DALL-E? Use promo code DALLE1000 for $10 in free API credits!
Tutorials/Node.js SDK
TypeScript / Node.js2 min setup

Node.js & TypeScript SDK

Building React, Next.js, or Express apps? Install our official Node.js SDK with full TypeScript support to generate multi-model images and videos with automatic routing and failover out of the box.

1. Installation

Install the package via npm, yarn, or pnpm.

bash
npm install @creativeai/node-sdk
# or yarn add @creativeai/node-sdk
# or pnpm add @creativeai/node-sdk

2. Core Usage Example

Import the class, initialize it with your API key, and handle synchronous images or asynchronous video generation with built-in polling.

typescript
import CreativeAI from '@creativeai/node-sdk';

// Automatically loads process.env.CREATIVEAI_API_KEY
const client = new CreativeAI();

async function main() {
  try {
    // 1. Generate an Image (Synchronous)
    const imageResult = await client.images.generate({
      prompt: 'A neon cyberpunk city at night',
      model: 'gpt-image-1',
      size: '1024x1024',
    });
    console.log('Image URL:', imageResult.data[0].url);

    // 2. Generate a Video with Automatic Failover (Asynchronous)
    console.log('Starting video generation (with auto failover)...');
    
    // Generate and wait for completion in one call
    const completedVideo = await client.videos.generateAndPoll({
      prompt: 'A cinematic drone shot of a misty mountain range',
      model: 'auto',       // Auto-routes to Kling or Seedance
      failover: true,      // Automatically falls back if primary provider fails
      duration: 5,
    });
    
    console.log('Video completed successfully!');
    console.log('Output URL:', completedVideo.output_url);

  } catch (error) {
    console.error('API Error:', error);
  }
}

main();

3. Unified Proxy Support

The SDK also exposes a unified proxy interface, allowing you to easily swap between image and video generation under a single endpoint format.

typescript
import CreativeAI from '@creativeai/node-sdk';

const client = new CreativeAI({ apiKey: 'YOUR_API_KEY' });

async function proxyExample() {
  // Use the unified proxy endpoint to seamlessly switch between image and video
  const vid = await client.proxy.generateAndPoll({
    type: 'video',
    prompt: 'A bird taking flight',
    model: 'kling-o3-pro',
  });
  
  console.log('Proxy Output:', vid.output_url);
}

Enterprise Reliability

Our SDK features automatic failover and load balancing. When an underlying provider goes down, we seamlessly route your request to an alternative model with zero latency.