Python Quickstart

Get up and running with the ShortGenius Python SDK in minutes. This guide will walk you through installation, authentication, and making your first API calls using Python.

Prerequisites

Installation

Install the ShortGenius Python SDK using pip:

pip install shortgenius

For development or to get the latest features:

pip install --upgrade shortgenius

Quick Start

1

Initialize the Client

Create a new ShortGenius client instance with your API key:

from shortgenius import Shortgenius

# Initialize with API key
client = Shortgenius(
    api_key="YOUR_API_KEY"  # or use os.environ.get("SHORTGENIUS_API_KEY")
)

The SDK will automatically look for the SHORTGENIUS_API_KEY environment variable if you don't provide the API key explicitly.

2

Test the Connection

Verify your setup by checking the API health status:

from shortgenius import Shortgenius

client = Shortgenius(api_key="YOUR_API_KEY")

# Check API health
status = client.health.check()
print(f"API Status: {status.status}")  # Should print: API Status: ok
3

List Available Resources

Get available voices and check your credits:

# List available voices
voices = client.audio.voices.list_voices(locale="en-US")
print(f"Found {len(voices)} voices")

for voice in voices[:3]:  # Show first 3 voices
    print(f"  - {voice.name} ({voice.source})")
    if voice.tags:
        print(f"    Gender: {voice.tags.gender}, Accent: {voice.tags.accent}")

# Check your credits
credits = client.credits.list()
print(f"Credits: {credits.balance.credits}")
4

Create Images for Your Video

Generate images that you can use in video creation:

# List available image styles
styles = client.images.list_styles()
print(f"Found {len(styles)} image styles")

# Show first few styles
for style in styles[:3]:
    print(f"  - {style.name}: {style.prompt}")

# Create an image
try:
    image = client.images.create(
        prompt="A serene mountain landscape at sunset",
        aspect_ratio="9:16",
        wait_for_generation=True
    )
    print(f"\nImage created: {image.id}")
    print(f"URL: {image.url}")
except Exception as e:
    print(f"Image creation failed: {e}")
    print("This might be due to insufficient credits")
5

Generate Topics and Create a Video

Generate video topics and create a video:

# Get necessary resources
connections = client.connections.list()
voices = client.audio.voices.list_voices(locale="en-US")

if not connections:
    print("Please set up a publishing connection first!")
    exit()

# Generate video topics
topics = client.videos.generate_topics(
    parent_topic="space exploration",
    locale="en-US",
    number_of_topics=3
)

print(f"Generated {len(topics)} video topics:")
for i, topic in enumerate(topics[:3], 1):
    print(f"  {i}. {topic}")

# Create a video with a topic
if topics:
    video = client.videos.create(
        topic=topics[0],
        locale="en-US",
        connection_ids=[connections[0].id],
        voice_id=voices[0].id if voices else None,
        aspect_ratio="9:16"
    )

    print(f"\nVideo created!")
    print(f"  ID: {video.id}")
    print(f"  Status: {video.publishing_state}")

Complete Example

Here's a complete example that ties everything together:

Error Handling

The SDK provides comprehensive error handling with typed exceptions:

Async Support

The SDK also provides an async client for better performance in async applications:

Working with Different Content Types

Working with Images

Working with Audio

Working with Video Series

Configuration Options

Custom Timeout

Custom Base URL

Retry Configuration

Using Proxies

Working with Responses

All SDK methods return typed response objects:

Best Practices

  1. Environment Variables: Store your API key in environment variables:

  2. Error Handling: Always wrap API calls in try-except blocks

  3. Rate Limiting: Implement exponential backoff for rate limit errors

  4. Resource Cleanup: Use context managers with async client

  5. Logging: Enable debug logging for troubleshooting:

Next Steps

Now that you're up and running with the Python SDK:


Need Help? Join our Discord community or check out more examples in our GitHub repository.

Last updated