Create job
→Receive project ID
→Wait
→Track completion
↗Poll project
↘Download result
↘Receive webhook
↗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
- Python
- Node.js
Step 1: Set Up Your Project
Create a new directory and set up your project structure: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.
The Python package is published as
magic_hour on PyPI; the Node.js SDK is magic-hour on npm.Step 3: Configure Your API Key
Open.env and add your API key:Step 4: Write the Integration Code
Openmain.py and add the following code. We’ll build it section by section:Step 5: Run Your Integration
Execute your script: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_idandcredits_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:
- 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: Video jobs run asynchronously. Review the recent typical processing
times for planning, then poll with exponential backoff or use
webhooks because duration still varies with the input, selected settings, and queue load.
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