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']}")