integrations/sdk/AIgateway Python SDK
PY

AIgateway Python SDK + AIgateway

Typed Python SDK for the aggregator-native surface — async jobs, sub-accounts, evals, replays, webhooks.

For chat / embeddings / images / STT / TTS, use the `openai` package with `base_url='https://api.aigateway.sh/v1'` — AIgateway is drop-in. The `aigateway` package is the typed wrapper for the parts OpenAI doesn't model: long-running jobs, sub-account billing, evals, replays, signed file URLs, and webhook verification.

AIgateway Python SDK homepage →
Setup

Three steps or fewer.

STEP 01

Install

Python 3.9+. Pure-Python, single dependency on httpx. PyPI distribution name is aigateway-py; the import path is aigateway.

pip install aigateway-py
STEP 02

Submit an async job

Long-running modalities (video, music, 3D) return a Job. Wait inline or attach a webhook_url and forget about it.

import os
from aigateway import AIgateway

client = AIgateway(api_key=os.environ["AIGATEWAY_API_KEY"])

job = client.jobs.create_video(
    prompt="a sunset over mountains, cinematic",
    model="runwayml/gen-4",
    duration=5,
)
done = client.jobs.wait(job.id, timeout_seconds=600)
print(done.result_url)
STEP 03

Mint a sub-account per customer

One call returns a scoped key with its own spend cap and analytics.

acct = client.sub_accounts.create(
    name="acme-corp",
    spend_cap_cents=50_000,
    rate_limit_rpm=300,
    default_tag="acme",
)
print(acct["key"])  # hand to the customer
STEP 04

Verify a webhook

HMAC-SHA256 over `t.body`. Helper raises if the signature is bad or stale.

from aigateway import verify_webhook

ok = verify_webhook(
    secret=os.environ["AIGATEWAY_WEBHOOK_SECRET"],
    body=raw_request_body,
    header=request.headers["x-gateway-signature"],
)
STEP 05

Async variant

Same surface, async/await flavored.

import asyncio
from aigateway import AsyncAIgateway

async def main():
    async with AsyncAIgateway(api_key=os.environ["AIGATEWAY_API_KEY"]) as c:
        job = await c.jobs.create_video(prompt="a cat")
        done = await c.jobs.wait(job.id)
        print(done.result_url)

asyncio.run(main())
Notes
  • Drop-in OpenAI calls (chat / embeddings / images / TTS / STT) belong in the `openai` package — point its base_url at AIgateway and you're done.
  • Working examples: github.com/aigateway-sh/examples · bug reports: github.com/aigateway-sh or support@aigateway.sh.
More integrations

Same key. Every other tool.