Expand AI logo
DocsDocs
Login

Documentation

Introduction

WelcomeQuickstartConcepts

Product

FetchMCPCLI

SDKs

SDKsTypeScript SDKPython SDK

SDKs

Python SDK

Official Python client for expand.ai

The Python SDK provides a clean, Pythonic interface for interacting with expand.ai's API.

PyPI

View on PyPI

GitHub

Source code

Installation

pip install expandai
uv add expandai
poetry add expandai

Usage















PreviousTypeScript SDK

On This Page

InstallationUsageConfigurationEnvironment VariablesAsync SupportError Handling
from expandai import Expand
client = Expand(api_key="{{API_KEY}}")
result = client.fetch(
url="https://news.ycombinator.com",
select={
"markdown": True,
"screenshot": True,
"summary": True
}
)
print(result.data.markdown)
print(result.data.summary)

Configuration

from expandai import Expand

client = Expand(
    # Your API key (required, or set EXPAND_API_KEY env var)
    api_key="{{API_KEY}}",
    
    # Custom base URL (optional)
    base_url="https://api.expand.ai/v1",
    
    # Additional headers (optional)
    headers={
        "Custom-Header": "value"
    }
)

Environment Variables

Instead of passing the API key directly, you can set the EXPAND_API_KEY environment variable:

export EXPAND_API_KEY={{API_KEY}}
# API key will be read from EXPAND_API_KEY
client = Expand()

Async Support

The SDK supports async/await for concurrent requests using AsyncExpand:

import asyncio
from expandai import AsyncExpand

async def main():
    client = AsyncExpand(api_key="{{API_KEY}}")
    
    result = await client.fetch(
        url="https://example.com",
        select={"markdown": True}
    )
    


Error Handling

from expandai import Expand, ExpandError, APIError, RateLimitError

client = Expand(api_key="{{API_KEY}}")

try:
    result = client.fetch(
        url="https://example.com",
        select={"markdown": True}
    )
    print(result.data.markdown)
except RateLimitError as e:
    print(





print(result.data.markdown)
asyncio.run(main())
f
"Rate limited, retry after:
{e.retry_after}
"
)
except APIError as e:
print(f"API error: {e.status_code} {e.message}")
except ExpandError as e:
print(f"Expand error: {e.message}")
except Exception as e:
print(f"Unexpected error: {e}")