Python
This guide helps you install and set up the Youzu Python API client.
InstallationCopied!
You can install the Youzu Python client using pip:
pip install youzu-api
For a specific version:
pip install youzu-api==1.2.0
AuthenticationCopied!
Configure your secret key in your code:
import youzu
# Configure the API client with your secret key
youzu.secret_key = "YOUR_SECRET_KEY"
# Optional: Set a custom base URL
# youzu.base_url = "https://platform-api.youzu.ai"
Alternatively, you can set the secret key via environment variables:
export YOUZU_SECRET_KEY="YOUR_SECRET_KEY"
Basic UsageCopied!
Image GenerationCopied!
import youzu
from youzu.api import generation
youzu.secret_key = "YOUR_SECRET_KEY"
# Generate an image
try:
response = generation.create(
prompt="a beautiful sunset over mountains",
# Optional parameters
width=1024,
height=1024,
num_outputs=1
)
# Access the generated image URL
image_url = response['url']
print(f"Generated image available at: {image_url}")
except youzu.error.APIError as e:
print(f"API Error: {e}")
except Exception as e:
print(f"Error: {e}")
Product SearchCopied!
import youzu
from youzu.api import products
youzu.secret_key = "YOUR_SECRET_KEY"
# Search for products using an image
try:
# Open the image file
with open("path/to/image.jpg", "rb") as image_file:
# Search for products
results = products.search(file=image_file)
# Display search results
for product in results['products']:
print(f"Product: {product['name']}")
print(f"Price: {product['price']}")
print(f"URL: {product['url']}")
print("-------------------")
except youzu.error.APIError as e:
print(f"API Error: {e}")
except Exception as e:
print(f"Error: {e}")
Asynchronous UsageCopied!
The Youzu Python client also provides asynchronous support:
import asyncio
import youzu
from youzu.async_api import generation
youzu.secret_key = "YOUR_SECRET_KEY"
async def generate_image():
try:
response = await generation.create(
prompt="a beautiful sunset over mountains"
)
return response['url']
except youzu.error.APIError as e:
print(f"API Error: {e}")
return None
# Run the async function
image_url = asyncio.run(generate_image())
print(f"Generated image available at: {image_url}")
Error HandlingCopied!
The Youzu Python client provides structured error handling:
import youzu
from youzu.error import APIError, RateLimitError, AuthenticationError
try:
# Your API call
response = youzu.api.generation.create(prompt="a beautiful sunset")
except AuthenticationError as e:
print(f"Authentication failed: {e}")
except RateLimitError as e:
print(f"Rate limit exceeded: {e}")
print(f"Reset at: {e.reset_at}")
except APIError as e:
print(f"API error: {e}")
print(f"Error code: {e.code}")
print(f"Status: {e.status}")
except Exception as e:
print(f"Unexpected error: {e}")
Configuration OptionsCopied!
The Youzu Python client provides several configuration options:
import youzu
# Secret Key (required)
youzu.secret_key = "YOUR_SECRET_KEY"
# Base URL (optional, defaults to https://platform-api.youzu.ai)
youzu.base_url = "https://custom-api.youzu.ai"
# Timeout in seconds (optional, defaults to 60)
youzu.timeout = 120
# Maximum retries (optional, defaults to 3)
youzu.max_retries = 5
# Enable debug logging (optional, defaults to False)
youzu.debug = True
For more examples and detailed documentation, refer to the Image Generation and Product Search guides.