Supported callbacks
| Callback | When it fires | Key payload |
|---|---|---|
onTryOnButtonClick | On button click, before the dialog opens. | name |
onAddToCartClicked | When the experience posts an add-to-cart message; enable add-to-cart first. | name, garments, setIsLoading, closePopUp, onError |
Handle a cart request
TypeScript
import { init } from "@spreeai/web-sdk";
import "@spreeai/web-sdk/dist/web-sdk.css";
const sdk = await init({ clientId: "YOUR_ISSUED_CLIENT_ID" });
if (sdk) {
await sdk.renderTryOnButton({
elementId: "spreeai-try-on",
garments: [{ garmentId: "YOUR_READY_GARMENT_ID" }],
enableAddToCart: true,
events: {
onTryOnButtonClick: ({ name }) => {
// Forward this non-sensitive event to your analytics layer.
console.info(name);
},
onAddToCartClicked: async ({
garments, setIsLoading, closePopUp, onError,
}) => {
setIsLoading(true);
try {
// Implement this endpoint in YOUR storefront backend.
// Validate and map these garment IDs to your own cart variants.
const response = await fetch("/api/cart/from-spreeai", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ garments }),
});
if (!response.ok) throw new Error("Cart request failed");
closePopUp();
} catch {
onError("Could not add to bag. Please try again.");
} finally {
setIsLoading(false);
}
},
},
});
}Measure the right action
A button-click callback measures intent to open try-on; it is not proof that a try-on completed or a purchase occurred. Use your approved analytics integration for downstream results. Do not invent completion callbacks or server webhooks that are absent from the published API.
Acceptance checks
- A successful cart operation adds the intended product, variant and quantity.
- A failed request keeps the shopper informed and clears loading state.
- Duplicate clicks do not create unintended duplicate mutations in your own handler.
- Analytics contain event metadata only, without customer images, tokens or unnecessary personal data.