Working integration examples
Install the standard widget or build a browser flow with bounded job polling.
Reviewed by Closku documentation team ·
Before you start
Ask the owner to:
- authorize your exact HTTPS storefront origin
- prepare an active product with an image
- provide the installation line
A real try-on requires an eligible subscription and available capacity. Run these examples on that authorized storefront, not from a local file. The identifiers are public; never insert a secret API key.
Standard embed
Replace the UUID with the matching active Closku product ID and use the exact script line copied from Your Widget.
<div data-product-id="11111111-1111-4111-8111-111111111111"></div>
<script src="https://closku.com/widget/your-store-slug.js" defer></script>Check that the script and button load, compare the ID with the products response, and complete a real try-on. A button proves placement only. See installation for theme considerations.
Custom browser flow
This standalone HTML example:
- loads products
- creates one session
- submits one selected photo
- polls a known job
An overall two-minute deadline stops requests, photo reading, and waiting. Find the merchant UUID in the installation script and use the products endpoint to find the product UUID. Replace the merchant and product UUIDs with real values; they are different identifiers. Host it on the authorized test storefront. The processing notice is an example, not a recorded consent control; arrange appropriate shopper notice and permission for your store.
<p>Your photo will be processed to generate a virtual try-on preview.</p>
<input id="photo" type="file" accept="image/jpeg,image/png,image/webp">
<button id="start">Start try-on</button>
<button id="stop">Stop waiting</button>
<p id="message" role="status"></p>
<a id="result" hidden>Open preview</a>
<script>
const api = "https://closku.com";
const merchantId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
const productId = "11111111-1111-4111-8111-111111111111";
const message = document.querySelector("#message");
const start = document.querySelector("#start");
const result = document.querySelector("#result");
let controller;
let submitted = false;
async function request(url, options = {}) {
const response = await fetch(api + url, options);
const data = await response.json();
if (!response.ok) throw new Error(data.error || "request_failed");
return data;
}
function photoData(file, signal) {
return new Promise((resolve, reject) => {
if (signal.aborted) return reject(new Error("stopped"));
const reader = new FileReader();
const cancel = () => reader.abort();
const cleanup = () => signal.removeEventListener("abort", cancel);
reader.onload = () => { cleanup(); resolve(reader.result); };
reader.onerror = () => { cleanup(); reject(new Error("photo_read_failed")); };
reader.onabort = () => { cleanup(); reject(new Error("stopped")); };
signal.addEventListener("abort", cancel, { once: true });
reader.readAsDataURL(file);
});
}
function delay(ms, signal) {
return new Promise((resolve, reject) => {
if (signal.aborted) return reject(new Error("stopped"));
const timer = setTimeout(() => { signal.removeEventListener("abort", cancel); resolve(); }, ms);
function cancel() { clearTimeout(timer); reject(new Error("stopped")); }
signal.addEventListener("abort", cancel, { once: true });
});
}
document.querySelector("#stop").onclick = () => controller?.abort();
window.addEventListener("pagehide", () => controller?.abort());
start.onclick = async () => {
if (submitted) return;
const file = document.querySelector("#photo").files[0];
if (!file || !["image/jpeg", "image/png", "image/webp"].includes(file.type) || file.size > 10 * 1024 * 1024) {
message.textContent = "Choose a JPEG, PNG, or WebP photo up to 10 MiB.";
return;
}
start.disabled = true;
controller = new AbortController();
const deadline = setTimeout(() => controller.abort(), 120000);
let jobId;
try {
const query = "?merchantId=" + encodeURIComponent(merchantId);
const config = await request("/api/widget/config" + query, { signal: controller.signal });
if (!config.isActive) throw new Error("widget_inactive");
const catalog = await request("/api/widget/products" + query, { signal: controller.signal });
if (!catalog.products.some(product => product.id === productId)) throw new Error("product_not_active");
const imageUrl = await photoData(file, controller.signal);
const session = await request("/api/widget/session", {
method: "POST", headers: { "Content-Type": "application/json" },
body: JSON.stringify({ merchantId, productId }), signal: controller.signal
});
if (controller.signal.aborted) throw new Error("stopped");
submitted = true;
const job = await request("/api/widget/try-on", {
method: "POST", headers: { "Content-Type": "application/json" },
body: JSON.stringify({ merchantId, sessionId: session.sessionId, productId, imageUrl }),
signal: controller.signal
});
jobId = job.jobId;
message.textContent = "Job ID: " + jobId;
for (let attempt = 0; attempt < 60; attempt++) {
const status = await request("/api/widget/try-on/" + encodeURIComponent(jobId) + query, { signal: controller.signal });
if (status.status === "completed") {
if (!status.resultUrl) throw new Error("result_unavailable");
result.href = status.resultUrl; result.hidden = false;
message.textContent = "Completed. Job ID: " + jobId; return;
}
if (status.status === "failed") throw new Error(status.error || "try_on_failed");
await delay(2000, controller.signal);
}
message.textContent = "Still waiting. Check this job later: " + jobId;
} catch (error) {
message.textContent = jobId
? "Stopped or failed: " + error.message + ". Check job " + jobId + " before submitting again."
: submitted
? "Submission outcome unknown: " + error.message + ". Contact support before another submission."
: "Could not start: " + error.message;
} finally {
clearTimeout(deadline);
if (!submitted) start.disabled = false;
}
};
</script>The example deliberately prevents another submission after the POST begins, even if the response is lost. Stopping only ends client-side waiting; it does not cancel the server job. Keep the displayed job ID and use the status endpoint to resume checking. A temporary result URL can expire. Verify the product name and actual garment before launch.