Search docs

Search the TryOnCloud API docs

Platforms

Python

Django, Flask or FastAPI backends.

Works with any Python backend. The examples use requests; httpx works the same way.

Plain function

python
import os
import requests

API = "https://www.tryoncloud.com/api/v1/generate"

def try_on(person_bytes: bytes, garment_url: str) -> bytes:
    r = requests.post(
        API,
        headers={"X-API-KEY": os.environ["TRYONCLOUD_API_KEY"]},
        files={"person_image": ("person.jpg", person_bytes)},
        data={"garment_url": garment_url},
        timeout=120,
    )
    if r.status_code != 200:
        err = r.json()
        raise RuntimeError(f"{err['code']}: {err['error']}")
    return r.content  # the PNG

FastAPI

python
from fastapi import FastAPI, File, Form, UploadFile, HTTPException
from fastapi.responses import Response

app = FastAPI()

@app.post("/try-on")
async def try_on_endpoint(person: UploadFile = File(...), garment_url: str = Form(...)):
    try:
        png = try_on(await person.read(), garment_url)
    except RuntimeError as e:
        raise HTTPException(status_code=502, detail=str(e))
    return Response(content=png, media_type="image/png")

Django

python
from django.http import HttpResponse, JsonResponse
from django.views.decorators.http import require_POST

@require_POST
def try_on_view(request):
    person = request.FILES.get("person")
    garment_url = request.POST.get("garment_url")
    if not person or not garment_url:
        return JsonResponse({"error": "person and garment_url are required"}, status=400)
    try:
        png = try_on(person.read(), garment_url)
    except RuntimeError as e:
        return JsonResponse({"error": str(e)}, status=502)
    return HttpResponse(png, content_type="image/png")

Flask is the same: read request.files["person"], call try_on, return the bytes with mimetype="image/png".