Skip to main content
This guide provides the essential concepts and patterns for integrating Magic Hour APIs into your production application. You’ll learn the API workflow, project structure, and production-ready patterns.

What you’ll accomplish

By the end of this guide, you’ll have:
  • ✅ Secure API integration setup
  • ✅ Complete create → poll → download workflow
  • ✅ Robust error handling
  • ✅ Production-ready patterns

Before you start

Complete the Quick Start Guide first to get familiar with basic API calls.
Prerequisites:
  • API key from Developer Hub
  • Your preferred SDK installed or direct HTTP client ready
Security: Never expose your API key in client-side code. Always keep it secure on your server to prevent unauthorized usage.

Integration overview

Choose the workflow that matches what you are building:
  • Start with generate() for quick scripts and prototypes. It creates the job, polls for completion, and can download the output for you.
  • Use create() with webhooks or background polling for production apps. It returns immediately so your server can track multiple jobs without holding requests open.
This guide uses the production create() workflow. If you are still learning the API, complete the Quick Start with generate() first. Magic Hour APIs follow a simple 3-step pattern that applies to all content generation:

Step 1: Create Job

Purpose: Submit your generation request to the API Relevant APIs:
  • Video APIs: POST /v1/face-swap, POST /v1/lip-sync, POST /v1/animation, POST /v1/text-to-video, etc.
  • Image APIs: POST /v1/ai-image-generator, POST /v1/face-swap-photo, POST /v1/ai-headshot-generator, etc.
  • Audio APIs: POST /v1/ai-voice-generator
Returns: Project ID and initial status (queued)
Python SDK

Production application pattern

Best for: Web apps, APIs, multiple concurrent jobs

Step-by-step integration guide

Let’s build a complete face swap integration step by step:

Step 1: Set up the API client

What this does: Creates a configured Magic Hour client for making API calls

Step 2: Create the job

What this does: Submits a face swap request and gets a project ID to track progress

Step 3: Monitor job status

What this does: Checks if the job is complete and handles different status states

Step 4: Download results

What this does: Downloads the generated video when the job completes successfully

Step 5: Put it all together

What this does: Combines all steps into a complete workflow function

Working with input files

Magic Hour accepts input files in two ways: The simplest approach is to pass file URLs:
Supported formats:
  • Images: PNG, JPG, JPEG, HEIC, HEIF, WEBP, AVIF, JP2, TIFF, BMP
  • Videos: MP4, M4V, MOV, WEBM
  • Audio: MP3, WAV, AAC, FLAC, WEBM, M4A, OPUS, OGG, AIFF, AMR
See Handling Inputs and Outputs for the canonical format list and special-case support.

Option 2: Upload to Magic Hour

For secure or temporary files, upload directly to Magic Hour storage:

Inputs and Outputs Guide

Complete guide to file uploads, formats, and storage options

Understanding job status

Every job goes through these states: Recommended polling intervals:
  • Images: Check every 2-3 seconds (usually complete within 30-60 seconds)
  • Videos: Check every 5-10 seconds (can take 2-5 minutes depending on length)

Error handling

When a job fails (status: "error"), the response includes detailed error information:

Error handling example

For unknown_error codes, contact support@magichour.ai with your project ID for investigation.

Status monitoring strategies

Choose the right approach based on your application’s needs: Get real-time notifications when jobs complete. Best for:
  • ✅ Production applications
  • ✅ Video processing (longer render times)
  • ✅ Multiple concurrent jobs
  • ✅ Better server resource usage

Webhook Setup Guide

Complete webhook implementation guide with examples

Option 2: Polling (Good for simple use cases)

Periodically check job status. Best for:
  • ✅ Simple integrations
  • ✅ Single job processing
  • ✅ Image generation (quick results)
Smart polling example:

Downloading results

When a job completes, the downloads array is populated with secure, time-limited URLs:
Download URLs expire after 24 hours. Download files immediately after job completion.

Download outputs

Multiple outputs handling

Some tools generate multiple files (e.g., multiple images):

File management

Cleaning up storage

Generated files are stored indefinitely. Clean up completed jobs to manage storage:
Deletion is permanent and cannot be undone. Only delete after confirming successful download.

Development and testing

Free testing with mock server

Avoid credit charges during development by using the mock API server:
The mock server returns realistic sample data without processing jobs or charging credits.

Job cancellation

Cancel video jobs (with full refund) via the web dashboard:
1

Open project details

Visit the project in your library: - Videos: https://magichour.ai/my-library?videoId= {project_id} - Images: https://magichour.ai/my-library?imageId={project_id} - Audio: https://magichour.ai/my-library?audioId={project_id}
2

Click cancel render

Cancel Render Button
3

Confirm cancellation

Confirm Cancel Button
4

Cancellation complete

Cancel Success
Notes:
  • Image jobs cannot be cancelled (they complete too quickly)
  • API-based cancellation is not currently available
  • Full credit refund is provided for cancelled video jobs

Next steps

Webhook Integration

Set up real-time notifications for production apps

API Reference

Explore all available endpoints and parameters

Google Colab Cookbook

Try all 22 APIs with ready-to-run examples

File Management

Advanced file upload and management techniques

Need help? Join our Discord community or email support@magichour.ai