Try-On Javascript Quickstart

Below is a minimal JavaScript snippet to demonstrate how to:

  1. POST to the /v1/tryon/clothes endpoint with your input data.

  2. Poll the /v1/status/<ID> endpoint until the job is completed.

  3. Retrieve the final results from the "output" field.


Minimal JavaScript Example

This example shows a simple request using URLs for the model and garment images. You can also modify the code to upload local images by converting them to Base64 format.

// 1. Set up the API key and base URL
const API_KEY = process.env.TRYITON_API_KEY;
const BASE_URL = "https://tryiton.now/api/v1";
 
// 2. POST request to initialize the try-on
const inputData = {
    model_image: "http://example.com/path/to/model.jpg",
    garment_image: "http://example.com/path/to/garment.jpg"
};
 
const headers = {
    "Content-Type": "application/json",
    "Authorization": `Bearer ${API_KEY}`
};
 
async function runPrediction() {
    try {
        // Make the initial run request
        const runResponse = await fetch(`${BASE_URL}/tryon/clothes`, {
            method: 'POST',
            headers: headers,
            body: JSON.stringify(inputData)
        });
        const runData = await runResponse.json();
        const jobId = runData.id;
        console.log("Job started, ID:", jobId);
 
        // Poll for status
        while (true) {
            const statusResponse = await fetch(`${BASE_URL}/v1/status/${jobId}`, {
                headers: headers
            });
            const statusData = await statusResponse.json();
 
            if (statusData.status === "completed") {
                console.log("Job completed.");
                console.log(statusData.output);
                break;
            } else if (statusData.status === "processing") {
                console.log("Job status:", statusData.status);
                await new Promise(resolve => setTimeout(resolve, 3000));
            } else {
                console.log("Job failed:", statusData.error);
                break;
            }
        }
    } catch (error) {
        console.error("Error:", error.message);
    }
}
 
// Run the prediction
runPrediction();

Last updated