Skip to main content
Every generation follows the same lifecycle:

Prefer an interactive browser notebook?

Open the Google Colab cookbook and try every API without local setup.

What You’ll Build

In this tutorial, you’ll create a working application that generates an AI image using the Magic Hour API. By the end, you’ll have:
  • A complete project structure ready for development
  • Code that creates a job, monitors its progress, and downloads the result
  • Proper file handling for inputs and outputs
  • Error handling for production use
  • A downloadable GitHub repository to reference
Estimated time: 15-20 minutes Prerequisites: API key from Developer Hub, Python 3.8+ or Node.js 16+ installed

Choose Your Language

Step 1: Set Up Your Project

Create a new directory and set up your project structure:
Your project structure should now look like this:

Step 2: Install Dependencies

Install the Magic Hour Python SDK and python-dotenv for managing API keys:
SDK Documentation: Full Python SDK docs available at github.com/magichourhq/magic-hour-python

Step 3: Configure Your API Key

Open .env and add your API key:
Security: Never commit .env to version control. Add it to .gitignore immediately.

Step 4: Write the Integration Code

Open main.py and add the following code. We’ll build it section by section:

Step 5: Run Your Integration

Execute your script:
You should see output like this:

Understanding the Code

Let’s break down what each part does:

1. Job Creation

  • Sends a request to Magic Hour to start generating an image
  • Returns immediately with a job_id and credits_charged
  • The actual generation happens asynchronously on Magic Hour’s servers

2. Status Polling

  • Periodically checks if the job is complete
  • Polls every 3 seconds (appropriate for image generation)
  • Handles different statuses: queued, rendering, complete, error

3. File Download

  • Downloads the generated image from the provided URL
  • Uses streaming to handle large files efficiently
  • Saves to the outputs/ directory with a unique filename

4. Error Handling

  • Checks for API errors and displays helpful messages
  • Implements timeouts to prevent infinite loops
  • Validates API key exists before making requests

Using the Simpler generate() Function

The SDK also provides a generate() function that handles polling and downloading automatically:
When to use each approach:
  • Use create() + polling: Production apps, webhook integration, custom monitoring
  • Use generate(): Quick scripts, testing, simple integrations

Download the Complete Project

Get the full working example from GitHub:

Python Example

Complete Python tutorial project

Node.js Example

Complete Node.js tutorial project

Working with Video Generation

For video generation, the process is identical but uses different endpoints:
Video Processing Times: Videos take 2-10 minutes depending on length. Use longer poll intervals (5-10 seconds) for video jobs.

Next Steps

Now that you have a working integration:

Handling Files

Learn advanced file upload and download techniques

Development & Testing

Best practices for testing without using credits

Webhooks

Set up real-time notifications instead of polling

API Reference

Explore all available endpoints and parameters

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