This guide will help you get started with the Apiframe Python SDK in minutes.
pip install apiframe-sdk- Sign up at apiframe.ai
- Navigate to your dashboard
- Copy your API key
from apiframe import Apiframe
client = Apiframe(api_key='your_api_key_here')Or use environment variables:
import os
from apiframe import Apiframe
client = Apiframe(api_key=os.environ.get('APIFRAME_API_KEY'))# Create an image generation task
task = client.midjourney.imagine({
'prompt': 'a beautiful sunset over mountains, photorealistic'
})
print(f"Task ID: {task['id']}")
# Wait for completion
result = client.tasks.wait_for(task['id'])
# Get the image URLs (imagine returns 4 images)
print(f"Images: {result['image_urls']}")# Wait with progress updates
result = client.tasks.wait_for(
task['id'],
on_progress=lambda p: print(f'Progress: {p}%')
)# 1. Generate images
imagine_task = client.midjourney.imagine({
'prompt': 'a futuristic city skyline',
'aspect_ratio': '16:9'
})
imagine_result = client.tasks.wait_for(imagine_task['id'])
# 2. Upscale one of the images
upscale_task = client.midjourney.upscale_1x({
'parent_task_id': imagine_task['id'],
'index': '1' # Choose from '1', '2', '3', or '4'
})
upscale_result = client.tasks.wait_for(upscale_task['id'])
print(f"Upscaled image: {upscale_result['image_url']}")# Generate a video with Luma AI
task = client.luma.generate({
'prompt': 'waves crashing on a beach at sunset',
'aspect_ratio': '16:9'
})
result = client.tasks.wait_for(task['id'], timeout=600) # 10 minutes
print(f"Video: {result['video_url']}")# Generate music with Suno AI
task = client.suno.generate({
'prompt': 'upbeat electronic dance music',
'tags': 'electronic, dance, energetic',
'make_instrumental': True
})
result = client.tasks.wait_for(task['id'], timeout=600)
print(f"Songs: {result['songs']}")# Fast generation with Flux Schnell
task = client.flux.generate_schnell({
'prompt': 'a portrait of a woman in a garden',
'width': 512,
'height': 512
})
result = client.tasks.wait_for(task['id'])
print(f"Image: {result['image_url']}")For automatic cleanup, use the context manager:
with Apiframe(api_key='your_api_key') as client:
task = client.midjourney.imagine({
'prompt': 'a beautiful landscape'
})
result = client.tasks.wait_for(task['id'])
print(result['image_urls'])
# Client automatically closed herefrom apiframe import (
Apiframe,
AuthenticationError,
RateLimitError,
TimeoutError,
ApiframeError
)
try:
client = Apiframe(api_key='your_api_key')
task = client.midjourney.imagine({'prompt': 'test'})
result = client.tasks.wait_for(task['id'])
except AuthenticationError:
print('Invalid API key')
except RateLimitError:
print('Rate limit exceeded, please wait')
except TimeoutError:
print('Request timed out')
except ApiframeError as e:
print(f'API error: {e.message}')You can check task status manually without waiting:
# Create a task
task = client.midjourney.imagine({'prompt': 'test'})
# Check status manually
status = client.tasks.get(task['id'])
print(f"Status: {status['status']}") # pending, processing, completed, failed
# Check multiple tasks
results = client.tasks.get_many(['task_id_1', 'task_id_2'])
for task in results['tasks']:
print(f"Task {task['id']}: {task['status']}")# Get your account info
account = client.tasks.get_account_info()
print(f"Email: {account['email']}")
print(f"Credits: {account['credits']}")
print(f"Plan: {account['plan']}")# Upload an image
upload = client.media.upload(file='./path/to/image.jpg')
print(f"Uploaded: {upload['imageURL']}")
# Use in API call
task = client.midjourney.blend({
'image_urls': [
upload['imageURL'],
'https://another-image.jpg'
]
})- Check out the examples directory for more detailed examples
- Read the README.md for complete API reference
- Visit docs.apiframe.ai for full documentation
- Use wait_for for long-running tasks: Don't poll manually, use
wait_for()with a callback - Set appropriate timeouts: Video and music generation can take 5-10 minutes
- Handle errors gracefully: Always wrap API calls in try-except blocks
- Use context managers: Ensures proper cleanup of resources
- Check your credits: Use
get_account_info()to monitor your usage
- Documentation: docs.apiframe.ai
- GitHub Issues: github.com/apiframe-ai/apiframe-python-sdk/issues
- Email: support@apiframe.ai
Happy generating! 🎨🎬🎵