Skip to main content

API

init

Description

Authenticates with the SPREEAI API and returns an SDK object used to render try-on and sizing buttons. The auth handshake happens once per page, regardless of how many buttons you render. Returns null if authentication fails.

const sdk = await init({
clientId: string,
partnerId: string,
baseURL?: string, // default: "https://protea-bridge.spreeai.com"
baseApiURL?: string, // default: "https://api.spreeai.com"
});

Parameters

InitOptions

PropertyTypeDefaultDescription
clientIdstring-Required. Your client id.
partnerIdstring-Required. The partner id provided to you.
baseURLstring"https://protea-bridge.spreeai.com"Optional base URL for the try-on experience.
baseApiURLstring"https://api.spreeai.com"Optional base API URL.

Returns

The returned SDK exposes:

interface SpreeAISDK {
renderTryOnButton(options: RenderTryOnButtonOptions): Promise<void>;
renderSizingButton(
options: RenderSizingButtonOptions,
): Promise<SizingButtonResult | undefined>;
}

sdk.renderTryOnButton

Description

Renders a try-on button into the element identified by elementId. Clicking the button launches SPREEAI's try-on experience as an overlay for the specified garment(s).

Parameters

RenderTryOnButtonOptions

PropertyTypeDefaultDescription
elementIdstring-Required. The id of the host element used to render the try-on button.
garmentsGarmentInput[]-Required (one of). Array of garments to try on. Multi-garment requests are validated as an outfit on the server; if no matching outfit is found, the SDK falls back to the first garment.
garmentIdstring-Required (one of). Deprecated. Single garment id. Use garments instead.
classNamestring-Optional extra CSS class on the button element.
button{ text?: string }-Optional button label. Default: "Try It On" for a single garment, "Try this look" for multiple. Overridden by features.tryOnButton.text.
enableAddToCartbooleanfalseOptional flag to enable add-to-cart functionality inside the iframe.
featuresTryOnFeatures-Optional features configuration for loading screen and button styling.
eventsTryOnButtonEvents-Optional event handlers for button interactions.
variantVariantOptions-Deprecated. Legacy single-garment variant filter. Use garments[i].variant instead.

You must provide exactly one of garments (recommended) or garmentId (deprecated).

GarmentInput

PropertyTypeDefaultDescription
garmentIdstring-Required. The id of the garment.
variantVariantOptions-Optional variant filter to pin a specific variant.
interface GarmentInput {
garmentId: string;
variant?: {
color?: string;
fit?: string;
gender?: string;
style?: string;
};
}

If variant is supplied, the matching variant on the garment must exist and be available, or the call exits with an error.

VariantOptions

PropertyTypeDefaultDescription
colorstring-Pin the garment variant by color.
fitstring-Pin the garment variant by fit.
genderstring-Pin the garment variant by gender.
stylestring-Pin the garment variant by style.

TryOnFeatures

PropertyTypeDefaultDescription
loadingScreenLoadingScreenConfig-Configuration for the loading screen.
tryOnButtonTryOnButtonConfig-Configuration for the try-on button styling.

LoadingScreenConfig

PropertyTypeDefaultDescription
videoUrlstring-URL to a video to display on the loading screen.
loadingTextstring[]-Array of text messages to cycle through while loading. Must contain 2–5 strings, each with a maximum of 42 characters. Invalid values are dropped with a console warning and the default loading screen text is shown instead.

TryOnButtonConfig

PropertyTypeDefaultDescription
textstring-Custom text to display on the button.
iconUrlstring-URL to a custom icon to display on the button.
iconHeightstring-Height of the icon (e.g., "20px").
borderRadiusstring-Border radius of the button (e.g., "8px").
backgroundColorstring-Background color of the button (e.g., "#000000").
textColorstring-Text color of the button (e.g., "#FFFFFF").
widthstring-Width of the button (e.g., "100%").
heightstring-Height of the button.
fontSizestring-Font size of the button text (e.g., "16px").
fontWeightstring-Font weight of the button text (e.g., "600").
paddingstring-Padding inside the button (e.g., "12px 24px").
borderstring-Border style of the button (e.g., "none").
boxShadowstring-Box shadow of the button.

TryOnButtonEvents

PropertyTypeDefaultDescription
onTryOnButtonClick(event: TryOnButtonClickEvent) => void-Callback fired when the try-on button is clicked, before the dialog opens.
onAddToCartClicked(event: AddToCartClickedEvent) => void-Callback fired when the iframe posts an add-to-cart message. Requires enableAddToCart: true.

TryOnButtonClickEvent

PropertyTypeDefaultDescription
namestring"spreeai-try-on-button-clicked"The name of the event.

AddToCartClickedEvent

PropertyTypeDefaultDescription
namestring"spreeai-add-to-cart-clicked"The name of the event.
garmentsGarment[]-The garments the user wants to add to cart.
setIsLoading(loading: boolean) => void-Function to set the loading state of the button.
closePopUp() => void-Function to close the try-on popup.
onError(message: string) => void-Function to display an error message.

Garment

PropertyTypeDefaultDescription
idstring-Required. The unique identifier of the garment.
partner_idstring-Required. The partner ID associated with the garment.
availableboolean-Required. Whether the garment is available.
namestring-Required. The name of the garment.
descriptionstring-Optional description of the garment.

sdk.renderSizingButton

Description

Renders a sizing-recommendation button into the element identified by elementId. Clicking the button opens a size-recommendation flow. The recommended size is cached in localStorage per garment and is returned (populated from cache) on subsequent loads.

const result = await sdk.renderSizingButton({
elementId: string,
garmentId: string,
});

Parameters

RenderSizingButtonOptions

PropertyTypeDefaultDescription
elementIdstring-Required. The id of the host element used to render the sizing button.
garmentIdstring-Required. The id of the garment.

Returns

interface SizingButtonResult {
recommendedSize: string | undefined;
}

Returns { recommendedSize } — populated from cache on subsequent loads, or undefined if no recommendation has been made yet.

Events

onTryOnButtonClick

Fired when the user clicks the try-on button, before the dialog opens.

{
name: "spreeai-try-on-button-clicked";
}

onAddToCartClicked

Fired when the iframe posts an add-to-cart message. Requires enableAddToCart: true.

{
name: "spreeai-add-to-cart-clicked",
garments: Garment[], // garments the user wants to add
setIsLoading: (loading: boolean) => void,
closePopUp: () => void,
onError: (message: string) => void,
}
events: {
onAddToCartClicked: async ({ garments, setIsLoading, closePopUp, onError }) => {
setIsLoading(true);
try {
await fetch("/api/cart", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ids: garments.map((g) => g.id) }),
});
setIsLoading(false);
closePopUp();
} catch (err) {
setIsLoading(false);
onError("Failed to add to cart. Please try again.");
}
},
}

Basic Usage

<html>
<body>
<script type="module" src="https://unpkg.com/@spreeai/web-sdk@2.1.0"></script>
<link
rel="stylesheet"
href="https://unpkg.com/@spreeai/web-sdk@2.1.0/dist/web-sdk.css"
/>
<div id="try-on-button"></div>
<script type="module">
import { init } from "@spreeai/web-sdk";

const sdk = await init({
clientId: "your-client-id",
partnerId: "your-partner-id",
});

if (sdk) {
await sdk.renderTryOnButton({
elementId: "try-on-button",
garmentId: "your-garment-id",
});
}
</script>
</body>
</html>

Complete Example

import { init } from "@spreeai/web-sdk";

const sdk = await init({
clientId: "client_id",
partnerId: "my-brand",
});

if (sdk) {
await sdk.renderTryOnButton({
elementId: "try-on-button",
garments: [
{ garmentId: "shirt-123", variant: { color: "blue", fit: "regular" } },
{ garmentId: "pants-456", variant: { color: "indigo" } },
],
enableAddToCart: true,
events: {
onTryOnButtonClick: (event) => analytics.track("try_on_started", event),
onAddToCartClicked: async ({
garments,
setIsLoading,
closePopUp,
onError,
}) => {
setIsLoading(true);
try {
await fetch("/api/cart/add", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ids: garments.map((g) => g.id) }),
});
setIsLoading(false);
closePopUp();
} catch (err) {
setIsLoading(false);
onError(err.message);
}
},
},
features: {
loadingScreen: {
videoUrl: "https://cdn.example.com/loading.mp4",
loadingText: [
"Preparing your fitting room…",
"Loading models…",
"Almost ready!",
],
},
tryOnButton: {
backgroundColor: "#000",
textColor: "#fff",
borderRadius: "4px",
fontSize: "14px",
padding: "10px 20px",
},
},
});
}

TypeScript Support

The SDK is written in TypeScript and ships full type definitions:

import {
init,
type SpreeAISDK,
type RenderTryOnButtonOptions,
type GarmentInput,
type AddToCartClickedEvent,
} from "@spreeai/web-sdk";

tryOnButton (deprecated)

The v1 entry point still works in 2.x but is deprecated and will be removed in v3.0.0. The first call logs a deprecation warning. New integrations should use init plus sdk.renderTryOnButton instead.

import { tryOnButton } from "@spreeai/web-sdk";

await tryOnButton({
clientId: "your-client-id",
partnerId: "your-partner-id",
elementId: "try-on-button",
garmentId: "garment-123",
});