Below is a minimal JavaScript snippet to demonstrate how to:
  1. POST to the /try-on/run endpoint with your input data.
  2. Poll the status endpoint using the pollingUrl returned from the run request until the generation is completed.
  3. Retrieve the final results from the “output” field.
For detailed documentation (including advanced parameters and usage), please refer to:

Minimal JavaScript Example

This snippet demonstrates a basic request using model and garment image URLs. You can also adapt the code to send local images in Base64 format.
// 1. Set up the API key and base URL
const API_KEY = process.env.AYNA_API_KEY;
if (!API_KEY) {
    throw new Error("Please set the AYNA_API_KEY environment variable.");
}
const BASE_URL = "https://api.getayna.com/v1";

// Helper function to sleep
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));

async function runTryOn() {
    try {
        // 2. POST request to /try-on/run
        const inputData = {
            model: "ayna-1.5",
            input: {
                model_image: "YOUR_MODEL_IMAGE_URL_OR_BASE64_HERE",
                garment_image: "YOUR_GARMENT_IMAGE_URL_OR_BASE64_HERE"
            }
        };

        const headers = {
            "Content-Type": "application/json",
            "Authorization": `Bearer ${API_KEY}`
        };

        const runResponse = await fetch(`${BASE_URL}/try-on/run`, {
            method: "POST",
            headers: headers,
            body: JSON.stringify(inputData)
        });

        const runData = await runResponse.json();

        // Check if the request was successful
        if (runData.status === "failed") {
            console.log("Request failed:", runData.error);
            return;
        }

        const pollingUrl = runData.pollingUrl;
        console.log("Generation started, polling URL:", pollingUrl);

        // 3. Poll the status endpoint using the polling URL
        while (true) {
            const statusResponse = await fetch(pollingUrl, {
                headers: headers
            });
            const statusData = await statusResponse.json();

            if (statusData.status === "completed") {
                console.log("Generation completed.");
                // 4. The "output" field contains the final image URL
                console.log("Result image:", statusData.output.image);
                break;
            } else if (["queued", "processing"].includes(statusData.status)) {
                console.log("Generation status:", statusData.status);
                await sleep(3000); // Wait 3 seconds
            } else if (statusData.status === "failed") {
                console.log("Generation failed:", statusData.error);
                break;
            } else {
                console.log("Unknown status:", statusData.status);
                break;
            }
        }
    } catch (error) {
        console.error("Error:", error);
    }
}

// Run the function
runTryOn();