Skip to content

Latest commit

 

History

History
231 lines (173 loc) · 5.25 KB

File metadata and controls

231 lines (173 loc) · 5.25 KB

Apiframe Python SDK - Quick Start Guide

This guide will help you get started with the Apiframe Python SDK in minutes.

Installation

pip install apiframe-sdk

Get Your API Key

  1. Sign up at apiframe.ai
  2. Navigate to your dashboard
  3. Copy your API key

Basic Usage

1. Initialize the Client

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'))

2. Generate Your First Image

# 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']}")

3. With Progress Tracking

# Wait with progress updates
result = client.tasks.wait_for(
    task['id'],
    on_progress=lambda p: print(f'Progress: {p}%')
)

Common Workflows

Image Generation and Upscaling

# 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']}")

Video Generation

# 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']}")

Music Generation

# 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']}")

Image Generation with Flux

# 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']}")

Using Context Manager

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 here

Error Handling

from 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}')

Checking Task Status

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']}")

Account Information

# 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 Media

# 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'
    ]
})

Next Steps

Tips

  1. Use wait_for for long-running tasks: Don't poll manually, use wait_for() with a callback
  2. Set appropriate timeouts: Video and music generation can take 5-10 minutes
  3. Handle errors gracefully: Always wrap API calls in try-except blocks
  4. Use context managers: Ensures proper cleanup of resources
  5. Check your credits: Use get_account_info() to monitor your usage

Getting Help

Happy generating! 🎨🎬🎵