Magic Hour Python SDK
The Magic Hour Python Library provides convenient access to the Magic Hour API. This library offers both synchronous and asynchronous clients powered by httpx.
Documentation
For full documentation of all APIs, please visit https://docs.magichour.ai
If you have any questions, please reach out to us via discord.
Install
pip install magic_hour
Cookbook
For end-to-end examples demonstrating all available Magic Hour APIs, check out our interactive Google Colab cookbook:
- Interactive Notebook: Magic Hour API Cookbook
The cookbook includes:
- Setup instructions
- Examples for all available APIs (image generation, face swap, lip sync, video generation, and more)
- Display helpers for previewing outputs
- Production-ready patterns and best practices
Synchronous Client Usage
from magic_hour import Client
# generate your API Key at https://magichour.ai/developer
client = Client(token="my api key")
response = client.v1.face_swap_photo.generate(
assets={
"face_swap_mode": "all-faces",
"source_file_path": "/path/to/source/image.png",
"target_file_path": "/path/to/target/image.png",
},
name="Face Swap image",
wait_for_completion=True,
download_outputs=True,
download_directory=".",
)
print(f"Project ID: {response.id}")
print(f"Status: {response.status}")
print(f"Downloaded files: {response.downloaded_paths}")
Asynchronous Client Usage
from magic_hour import AsyncClient
# generate your API Key at https://magichour.ai/developer
client = AsyncClient(token="my api key")
response = await client.v1.face_swap_photo.generate(
assets={
"face_swap_mode": "all-faces",
"source_file_path": "/path/to/source/image.png",
"target_file_path": "/path/to/target/image.png",
},
name="Face Swap image",
wait_for_completion=True,
download_outputs=True,
download_directory=".",
)
print(f"Project ID: {response.id}")
print(f"Status: {response.status}")
print(f"Downloaded files: {response.downloaded_paths}")
Client Functions
Most resources that generate media content support two methods:
generate()- A high-level convenience method that handles the entire workflowcreate()- A low-level method that only initiates the generation process
Generate Function
The generate() function provides a complete end-to-end solution:
- Uploads local file to Magic Hour storage
- Calls the API to start generation
- Automatically polls for completion
- Downloads generated files to your local machine
- Returns both API response data and local file paths
Additional Parameters:
wait_for_completion(bool, default True): Whether to wait for the project to complete.download_outputs(bool, default True): Whether to download the generated filesdownload_directory(str, optional): Directory to save downloaded files (defaults to current directory)
# Generate function - handles everything automatically
response = client.v1.ai_image_generator.generate(
style={"prompt": "A beautiful sunset over mountains"},
name="Sunset Image",
wait_for_completion=True, # Wait for status to be complete/error/canceled
download_outputs=True, # Download files automatically
download_directory="./outputs/" # Where to save files
)
# You get both the API response AND downloaded file paths
print(f"Project ID: {response.id}")
print(f"Status: {response.status}")
print(f"Downloaded files: {response.downloaded_paths}")
Create Function
The create() function provides granular control:
- Only calls the API to start the generation process
- Returns immediately with a project ID and amount of credits used
- Requires manual status checking and file downloading
# Create function - only starts the process
create_response = client.v1.ai_image_generator.create(
style={"prompt": "A beautiful sunset over mountains"},
name="Sunset Image"
)
# You get just the project ID and initial response
project_id = create_response.id
print(f"Started project: {project_id}")
# You must handle the rest:
# 1. Poll for completion. We provide a helper function to handle polling for you
result = client.v1.image_projects.check_status(
wait_for_completion=True,
download_outputs=False,
)
# 2. Download files using the download URLs
download_urls = result.downloads
# download the files using your preferred way
Choosing Between Which Function to use
Use generate() when:
- You want a simple, one-call solution
- You're building a straightforward application
- You don't need custom polling or download logic
Use create() when:
- You need custom status checking logic
- You're integrating with existing job processing systems
- You want to separate generation initiation from completion handling
- You need fine-grained control over the entire workflow
Module Documentation and Snippets
v1.account
- list - Get account details
v1.ai_clothes_changer
v1.ai_face_editor
v1.ai_gif_generator
v1.ai_headshot_generator
v1.ai_image_editor
v1.ai_image_generator
v1.ai_image_upscaler
v1.ai_meme_generator
v1.ai_qr_code_generator
v1.ai_talking_photo
v1.ai_video_editor
v1.ai_voice_cloner
- create - AI Voice Cloner
v1.ai_voice_generator
v1.animation
v1.audio_projects
- check-result - Check results
- delete - Delete audio
- get - Get audio details
v1.audio_to_video
v1.auto_subtitle_generator
v1.body_swap
v1.character_replace
v1.face_detection
- create - Face Detection
- generate - Face Detection Generate Workflow
- get - Get face detection details
v1.face_swap
v1.face_swap_photo
v1.files
- upload-file - Upload File
v1.files.upload_urls
- create - Generate asset upload urls
v1.head_swap
v1.image_background_remover
v1.image_projects
- check-result - Check results
- delete - Delete image
- get - Get image details
v1.image_to_video
v1.lip_sync
v1.photo_colorizer
v1.saved_items
- list - List saved items
v1.text_to_video
v1.video_projects
- check-result - Check results
- delete - Delete video
- get - Get video details
v1.video_to_video
Release files for magic_hour 0.82.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| magic_hour-0.82.1.tar.gz | 173.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| magic_hour-0.82.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 515.3 kB
Release files / magic_hour-0.82.1.tar.gz
| Download URL | magic_hour-0.82.1.tar.gz |
|---|---|
| Size | 173.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f3e3e6625ed478f34644061b108173b53640ab3df87f4769f3078e25b149617f
|
|
BLAKE2b-256 checksum How to use checksums |
0a20519f9a3b8c0f83ab6e9a8af634f642d73eb0d1b4a735f79428aabd2c4f4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.8.18 Linux/6.6.141
|
Release files / magic_hour-0.82.1-py3-none-any.whl
| Download URL | magic_hour-0.82.1-py3-none-any.whl |
|---|---|
| Size | 341.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
509ff37e5f6b4932caa8a080aa4cce21b3a12e89560a9e7d8a543caf64d46bc1
|
|
BLAKE2b-256 checksum How to use checksums |
4a41a5d76157b25d23aa4536e58304d5d88a55b8e0313006893a9f273b44daad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.8.18 Linux/6.6.141
|