Skip to main content
A complete, runnable Python script that walks through every step of the Try-On API: creating a product, uploading a customer photo, generating a try-on, polling for the result, and displaying it.

Prerequisites

pip install requests

The Script

import os, sys, time, requests

BASE_URL = os.environ.get("GENLOOK_BASE_URL", "https://api.genlook.app/tryon/v1")
API_KEY  = os.environ["GENLOOK_API_KEY"]  # set this env var

session = requests.Session()
session.headers["x-api-key"] = API_KEY


# ── Step 1: Create a product ────────────────────────────────

def create_product(image_url: str, external_id: str = "demo-product-001") -> str:
    r = session.post(f"{BASE_URL}/products", json={
        "externalId": external_id,
        "title": "Classic Blue T-Shirt",
        "description": "A comfortable cotton crew-neck t-shirt in navy blue",
        "imageUrls": [image_url],
    })
    r.raise_for_status()
    data = r.json()
    print(f"Product created: {data['productId']} (externalId={data['externalId']})")
    return data["externalId"]


# ── Step 2: Upload a customer photo ─────────────────────────

def upload_photo(file_path: str) -> str:
    with open(file_path, "rb") as f:
        r = session.post(
            f"{BASE_URL}/images/upload",
            files={"file": (os.path.basename(file_path), f, "image/jpeg")},
        )
    r.raise_for_status()
    data = r.json()
    print(f"Photo uploaded: {data['imageId']}")
    return data["imageId"]


# ── Step 3: Generate a try-on ────────────────────────────────

def generate_tryon(product_id: str, image_id: str) -> str:
    r = session.post(f"{BASE_URL}/try-on", json={
        "productId": product_id,
        "customerImageId": image_id,
    })
    r.raise_for_status()
    data = r.json()
    if not data.get("success"):
        print(f"Error: {data.get('error')} ({data.get('code')})")
        sys.exit(1)
    print(f"Generation started: {data['generationId']} (status={data['status']})")
    return data["generationId"]


# ── Step 4: Poll until complete ──────────────────────────────

def poll(generation_id: str, timeout: int = 120) -> dict:
    start = time.time()
    while time.time() - start < timeout:
        r = session.get(f"{BASE_URL}/generations/{generation_id}")
        r.raise_for_status()
        data = r.json()
        status = data["status"]
        elapsed = int(time.time() - start)
        print(f"  [{elapsed:3d}s] {status}")
        if status == "COMPLETED":
            return data
        if status == "FAILED":
            print(f"  Error: {data.get('errorMessage')}")
            sys.exit(1)
        time.sleep(2)
    print("Timed out waiting for generation")
    sys.exit(1)


# ── Run it ───────────────────────────────────────────────────

if __name__ == "__main__":
    PRODUCT_IMAGE = "https://example.com/images/tshirt.jpg"  # replace with a real URL
    CUSTOMER_PHOTO = "./customer-photo.jpg"                   # replace with a real file

    product_id = create_product(PRODUCT_IMAGE)
    image_id   = upload_photo(CUSTOMER_PHOTO)
    gen_id     = generate_tryon(product_id, image_id)
    result     = poll(gen_id)

    print(f"\nResult image: {result['resultImageUrl']}")

Running It

Set your API key and run:
export GENLOOK_API_KEY=gk_your_api_key
python tryon_demo.py
Expected output:
Product created: cm8xyz789 (externalId=demo-product-001)
Photo uploaded: retention-7d/abc123/customer/20260330-xyz.jpeg
Generation started: cm8gen456 (status=PENDING)
  [  0s] PENDING
  [  2s] PROCESSING
  [ 14s] COMPLETED

Result image: https://storage.googleapis.com/...

Key Points

  • Authentication: Every request includes the x-api-key header via session.headers.
  • Product images: Passed as public URLs — the API fetches and processes them.
  • Customer photos: Uploaded as multipart form data (files={"file": ...}).
  • Polling: Generations are async. Poll GET /generations/{id} every 2 seconds until COMPLETED or FAILED.
  • Credits: Each POST /try-on call consumes 1 credit. Check your balance with GET /account/credits.

CLI Version

A more complete CLI version with argument parsing and error handling is available in the repo at test-tryon-api/demo.py.