Official SDK

Python SDK

Full-featured Python SDK with sync and async support, type hints, and automatic retries.

Installation

pip install butterflyapi

Quick Start

from butterflyapi import ButterflyAPI

client = ButterflyAPI(api_key="your_api_key_here")

# Cartoonify an image
result = client.cartoon(
    image_url="https://your-bucket.s3.amazonaws.com/photo.jpg",
    style="pixar"
)

print(f"Result: {result.output_url}")

Async Support

import asyncio
from butterflyapi import AsyncButterflyAPI

async def main():
    client = AsyncButterflyAPI(api_key="your_api_key")
    
    result = await client.cartoon(
        image_url="https://your-bucket.s3.amazonaws.com/photo.jpg",
        style="anime"
    )
    
    print(f"Result: {result.output_url}")

asyncio.run(main())

Available Methods

Image Processing

  • client.cartoon() - Convert images to cartoon styles
  • client.image_to_image() - Transform images with AI
  • client.background_remove() - Remove backgrounds
  • client.upscale() - Upscale images
  • client.enhance() - Enhance quality
  • client.face_restore() - Restore faces
  • client.watermark() - Add watermarks
  • client.text_to_image() - Generate images
  • client.image_caption() - Generate captions

Video Processing

  • client.video_generate() - Generate videos
  • client.cineflow_director() - Advanced video generation

Document Processing

  • client.html_to_pdf() - Convert HTML to PDF

Error Handling

from butterflyapi import ButterflyAPI, ButterflyAPIError, InsufficientCreditsError

client = ButterflyAPI(api_key="your_api_key")

try:
    result = client.cartoon(
        image_url="https://your-bucket.s3.amazonaws.com/photo.jpg",
        style="anime"
    )
except InsufficientCreditsError:
    print("Not enough credits")
except ButterflyAPIError as e:
    print(f"API error: {e.message}")

Batch Processing

# replaced example.com with placeholder URLs
images = [
    "https://your-bucket.s3.amazonaws.com/photo1.jpg",
    "https://your-bucket.s3.amazonaws.com/photo2.jpg",
    "https://your-bucket.s3.amazonaws.com/photo3.jpg"
]

results = client.batch_cartoon(
    images=images,
    style="pixar",
    max_concurrent=3
)

for result in results:
    print(f"Processed: {result.output_url}")

Job Polling

# Submit video generation job
job = client.video_generate(
    prompt="A cat playing piano",
    duration="5s",
    tier="standard"
)

# Wait for completion
result = client.wait_for_job(
    job_id=job.job_id,
    timeout=300,  # 5 minutes
    poll_interval=5  # Check every 5 seconds
)

print(f"Video ready: {result.output_url}")

Configuration

client = ButterflyAPI(
    api_key="your_api_key",
    base_url="https://butterflyapi.com/api/v1",
    timeout=30,
    max_retries=3,
    webhook_url="https://yoursite.com/webhook"
)

Package Information

  • Package: butterflyapi
  • Version: 1.0.0
  • License: MIT
  • Python: 3.8+
  • Dependencies: requests, aiohttp, pydantic