Skip to main content
The Kelly AI Python SDK uses API keys to authenticate requests. You’ll need an API key to use any of the SDK’s features.

Getting your API key

1

Message the bot

Contact @KellyAIBot on Telegram to request your API key.
2

Receive your key

The bot will provide you with a unique API key. Keep this key secure—don’t share it publicly or commit it to version control.
3

Use your key

Pass your API key to the KellyAPI constructor when initializing the client.

Initializing the client

Basic initialization

The simplest way to initialize the Kelly AI client is by passing your API key directly:
from kellyapi import KellyAPI

client = KellyAPI(api_key="your_api_key_here")

Using environment variables

For better security, store your API key in an environment variable:
import os
from kellyapi import KellyAPI

# Read API key from environment variable
api_key = os.getenv("KELLY_API_KEY")
client = KellyAPI(api_key=api_key)
Never hardcode your API key in production code. Always use environment variables or a secure secrets management system.

Configuration options

The KellyAPI class accepts several configuration parameters:
from kellyapi import KellyAPI
import aiohttp

client = KellyAPI(
    api_key="your_api_key_here",      # Required: Your API key
    api="https://api.kellyai.pro/",    # Optional: Custom API endpoint
    session=aiohttp.ClientSession      # Optional: Custom aiohttp session
)

Parameters

api_key
string
required
Your Kelly AI API key obtained from @KellyAIBot
api
string
default:"https://api.kellyai.pro/"
The base URL for the Kelly AI API. You typically don’t need to change this unless you’re using a custom endpoint.
session
aiohttp.ClientSession
default:"aiohttp.ClientSession"
A custom aiohttp.ClientSession class for advanced use cases. Useful if you need to configure custom timeouts, proxies, or connection pooling.

Using a custom session

For advanced scenarios, you can provide your own aiohttp.ClientSession configuration:
import aiohttp
from kellyapi import KellyAPI

# Create a custom session class with specific settings
class CustomSession(aiohttp.ClientSession):
    def __init__(self, *args, **kwargs):
        kwargs['timeout'] = aiohttp.ClientTimeout(total=120)  # 2 minute timeout
        kwargs['connector'] = aiohttp.TCPConnector(limit=100)
        super().__init__(*args, **kwargs)

client = KellyAPI(
    api_key="your_api_key_here",
    session=CustomSession
)

Authentication errors

The SDK will raise an InvalidApiKey exception if your API key is invalid or unauthorized:
import asyncio
from kellyapi import KellyAPI
from kellyapi.errors import InvalidApiKey

async def main():
    client = KellyAPI(api_key="invalid_key")
    
    try:
        await client.generate(prompt="test")
    except InvalidApiKey as e:
        print(f"Authentication failed: {e}")
        print("Get an API key from @KellyAIBot")

asyncio.run(main())
The error message will be:
Invalid API key, Get an api key from @KellyAIBot
Authentication is checked on every API request. If you receive a 401 or 403 status code, the SDK will automatically raise an InvalidApiKey exception.

Best practices

Secure storage

Store API keys in environment variables or secrets management systems, never in code.

Separate keys

Use different API keys for development, staging, and production environments.

Key rotation

Regularly rotate your API keys and update them across your applications.

Access control

Limit who has access to your API keys within your organization.

Next steps

Now that you understand authentication, you’re ready to explore the SDK’s features:

Image generation

Generate images with Stable Diffusion and SDXL models

Language models

Use ChatGPT, Gemini, and other LLMs for text generation

Image tools

Upscale images, remove backgrounds, and more

Voice synthesis

Convert text to speech with multiple voice models

Build docs developers (and LLMs) love