Skip to main content
This guide will help you get started with the Kelly AI Python SDK. You’ll install the package, set up authentication, and generate your first AI image.

Prerequisites

  • Python 3.7 or higher
  • An API key from @KellyAIBot on Telegram

Installation

1

Install the SDK

Install the Kelly AI Python SDK using pip:
pip install kellyapi
The SDK will automatically install its dependencies:
  • aiohttp - For asynchronous HTTP requests
  • dotmap - For convenient response object handling
2

Get your API key

If you don’t have an API key yet, message @KellyAIBot on Telegram to get one. See the authentication guide for detailed instructions.
3

Create your first script

Create a new Python file and import the SDK:
import asyncio
from kellyapi import KellyAPI

async def main():
    # Initialize the client with your API key
    client = KellyAPI(api_key="your_api_key_here")
    
    # Generate an image
    image_data = await client.generate(
        prompt="a beautiful sunset over mountains, photorealistic",
        model="PhotoPerfect",
        width=1024,
        height=1024
    )
    
    # Save the image
    with open("sunset.png", "wb") as f:
        f.write(image_data)
    
    print("Image saved as sunset.png")

# Run the async function
asyncio.run(main())
The generate() method returns raw binary image data (bytes). You can save it directly to a file or process it with image libraries like PIL.
4

Run your script

Execute your script:
python your_script.py
You should see a new file sunset.png with your generated image.

Complete example

Here’s a more complete example showing multiple SDK features:
import asyncio
from kellyapi import KellyAPI

async def main():
    # Initialize the client
    client = KellyAPI(api_key="your_api_key_here")
    
    # List available Stable Diffusion models
    sd_models = await client.sd_models()
    print(f"Available SD models: {sd_models}")
    
    # Generate an image
    image_data = await client.generate(
        prompt="a cute robot reading a book, digital art",
        negative_prompt="blurry, low quality, distorted",
        model="PhotoPerfect",
        width=1024,
        height=1024
    )
    
    # Save the generated image
    with open("robot.png", "wb") as f:
        f.write(image_data)
    
    # Upscale the image
    with open("robot.png", "rb") as f:
        original_image = f.read()
    
    import base64
    image_base64 = base64.b64encode(original_image).decode('utf-8')
    
    upscaled_data = await client.upscale(image_data=image_base64)
    
    # Save the upscaled image
    with open("robot_upscaled.png", "wb") as f:
        f.write(upscaled_data)
    
    print("Images saved successfully!")

asyncio.run(main())

Next steps

Authentication

Learn more about API keys and authentication options

Image generation

Explore all image generation capabilities and models

Language models

Use ChatGPT, Gemini, and other LLMs

API reference

Browse the complete API documentation

Common issues

If you get an InvalidApiKey error, make sure you’ve correctly obtained your API key from @KellyAIBot and passed it to the KellyAPI constructor.

Timeout errors

If you’re generating large images or using complex prompts, you may need to increase the timeout. The default timeout is 60 seconds.

Async/await syntax

All SDK methods are asynchronous and must be called with await inside an async function. If you’re new to async Python, check out the Python asyncio documentation.

Build docs developers (and LLMs) love