The Magic Hour Python Library provides convenient access to the Magic Hour API. This library offers both synchronous and asynchronous clients powered by httpx.
For full documentation of all APIs, please visit https://docs.magichour.ai
If you have any questions, please reach out to us via discord.
pip install magic_hourfrom 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",
"source/image": "/path/to/target/image.png",
},
name="Face Swap image",
wait_for_completion=True,
download_outputs=True,
download_directory="./outputs/",
)
print(f"Project ID: {response.id}")
print(f"Status: {response.status}")
print(f"Downloaded files: {response.downloaded_paths}")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="./outputs/",
)
print(f"Project ID: {response.id}")
print(f"Status: {response.status}")
print(f"Downloaded files: {response.downloaded_paths}")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
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}")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 wayUse 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
- check-result - Check results
- delete - Delete audio
- get - Get audio details
- create - Face Detection
- generate - Face Detection Generate Workflow
- get - Get face detection details
- upload-file - Upload File
- create - Generate asset upload urls
- check-result - Check results
- delete - Delete image
- get - Get image details
- check-result - Check results
- delete - Delete video
- get - Get video details