Migrating from Sora or DALL-E? Use promo code DALLE1000 for $10 in free API credits!
Tutorials/Python SDK
Python / Data Science2 min setup

Python Drop-in API Wrapper

Building automated scripts, AI agents, or data pipelines? Drop this lightweight Python class into your project to generate multi-model images and videos securely and efficiently.

1. The Drop-in Wrapper

Save this code as creativeai.py. It only depends on the standard requests package.

python
import requests

class CreativeAI:
    """
    CreativeAI Drop-in Python SDK
    Requires 'requests' library. Built-in failover routing and sync/async endpoints.
    """
    def __init__(self, api_key: str):
        self.api_key = api_key
        self.base_url = "https://api.creativeai.run/v1"
        self.headers = {
            "Authorization": f"Bearer {self.api_key}",
            "Content-Type": "application/json"
        }

    def _request(self, method: str, endpoint: str, payload: dict = None):
        url = f"{self.base_url}{endpoint}"
        response = requests.request(method, url, headers=self.headers, json=payload)
        response.raise_for_status()
        return response.json()

    def generate_image(self, prompt: str, model: str = "seedream-3.0", size: str = "1024x1024"):
        return self._request("POST", "/images/generations", {
            "model": model,
            "prompt": prompt,
            "size": size,
            "n": 1
        })

    def generate_video(self, prompt: str, model: str = "auto", webhook_url: str = None):
        payload = {
            "model": model,
            "prompt": prompt,
            "failover": True
        }
        if webhook_url:
            payload["webhook_url"] = webhook_url
            
        return self._request("POST", "/video/generations", payload)
        
    def get_generation_status(self, task_id: str):
        return self._request("GET", f"/generations/{task_id}")

2. Usage Example

Import the class, initialize it with your API key, and handle synchronous or asynchronous API routing effortlessly.

python
from creativeai import CreativeAI

client = CreativeAI("YOUR_API_KEY")

# 1. Generate an Image (Synchronous)
try:
    image_response = client.generate_image("A cinematic sunset over a futuristic city")
    if "data" in image_response and image_response["data"]:
        print(f"Image URL: {image_response['data'][0]['url']}")
except Exception as e:
    print(f"Image generation failed: {e}")

# 2. Generate a Video (Asynchronous)
try:
    video_response = client.generate_video(
        prompt="Drone shot of a coastal highway",
        model="auto", # Auto-routes to Kling or Seedance
        webhook_url="https://your-app.com/webhook/video-complete"
    )
    print(f"Video Task ID: {video_response['id']}")
    print(f"Check status at your webhook or via get_generation_status('{video_response['id']}')")
except Exception as e:
    print(f"Video generation failed: {e}")

Automate Your Content Generation

Empower your Python workflows with the industry's most robust multi-model image and video API. Get started with zero vendor lock-in.