Skip to main content
The hosted loader exposes a command API through window.virtualfit. Define the queue before loading the script so early commands replay in order.

init

Initialize one storefront configuration.
siteKey is required. Calling init starts product matching and keeps it current as the page URL changes. See Loader configuration for all fields.

setContext

Patch multiple parts of the active configuration.
Use setContext when a single application state update changes both product and customer context.

setProduct

Set or replace the current product and optional exact variant context.
Provide id or handle. The value must resolve to a synced, enabled product with usable garment images.
string
Stable product ID used during catalog sync.
string
Synced product handle. Provide id or handle; providing both is recommended.
string
Current shopper-facing product title.
string
Display fallback for the current product. It does not override prepared catalog media.
string
Exact source_variant_id synced under this product. Use it when the shopper has selected a specific variant.
string
Optional display hint for the selected variant. virtual.fit verifies the variant against the synced product and uses canonical prepared variant media for try-on.
Call setProduct after an SPA commits a product route when URL detection cannot identify the product. Call it again when the product or selected variant changes. Keep id stable across all variants of one product. Use variantId for the exact color, style, fit, and size combination. A size-only variant can reuse the product’s shared prepared media. A color or pattern variant uses its own prepared media when you synced it.
A variant ID must belong to the supplied product and must be available when availability is known. An unknown, mismatched, or unavailable variant fails with an actionable error instead of silently using the wrong garment.

setCustomer

Update shopper identity after login, logout, or account switching.
Clear identity on logout:
Set customer context as early as possible so try-on and commerce events join to the same shopper journey.

setConsentMode

Choose whether analytics may include shopper identity.
Supported values: Call this when your consent manager initializes and whenever the shopper changes analytics consent.
See Privacy and consent.

recommend

Request hydrated products for a storefront placement:
Use similar for same-category alternatives, complete_the_look for the next category in an outfit, and likely_purchases for general “You may also like” or cart recommendations. If intent is omitted, the browser API defaults to likely_purchases. The hosted browser API supplies shopper identity automatically and keeps the existing merchant order if a recommendation request is rate-limited. See the Recommendations API rate limits for backend integrations. virtual.fit automatically uses the active product and products selected in the fitting room. likely_purchases can return products from categories already represented in the context. Pass the latest cart contents as cartProductIds. This augments the automatically resolved current product and fitting-room selection:
Send the complete current cart, including an empty array after the shopper removes its last item. An explicit cartProductIds value is authoritative for that call. On a single-page storefront, you may call recommend immediately after setProduct or a product-bearing setContext call. The loader waits for the newest product resolution, so the request cannot fall back to the previously mounted product. To replace all automatically resolved context instead, pass productIds:
productIds must contain one to five outfit IDs for complete_the_look and exactly one ID for similar. For likely_purchases, it may contain zero to five IDs: productIds: [] deliberately suppresses automatic page and fitting-room context so a supplied cartProductIds array can be the entire request context. A non-array value is rejected. selectedProductIds, cartProductIds, and excludeProductIds must also be arrays when supplied. excludeProductIds is a hard per-request override and accepts up to 500 IDs. Context products are always excluded automatically.

Copy-paste recommendation placements

Call recommend after the hosted loader is ready. Giving the loader script an ID makes that easy on a server-rendered storefront:
Keep the queued virtualfit('init', ...) block before the loader, as shown in the Custom JavaScript quickstart.

Product page: similar products

The current synced product is supplied automatically. This example renders a basic accessible row and leaves the placement hidden if recommendations are unavailable:

Cart drawer: likely purchases

Always read the full live cart after an add, removal, quantity change, or checkout. productIds: [] makes this a cart-only request instead of also using the product page behind the drawer:
Omit productIds: [] when you want the current product or fitting-room selection to augment the cart instead. The request counter prevents a slower response for an older cart from replacing recommendations for the latest cart.

Custom outfit builder: complete the look

Send the complete current outfit whenever a shopper adds or removes an item. The first product is treated as the primary outfit item and all supplied products are excluded from results automatically:
An empty items array is a valid result when there is no compatible open outfit category. Hide the row instead of replacing it with unrelated products.

Load another page

Pass the returned cursor with the same context and intent:
The promise resolves to:
Items are in display order, and rank is authoritative. score is optional and meaningful only within that response. Do not re-sort items by score or compare scores across requests. A recommendation may target the whole product, a visual choice, or one exact variant. Render one card per product. When variant_id is present, use it to preselect the recommended variant or pass it as variantId after navigation. virtual.fit does not return several cards for different variants of the same product. Keep your existing merchandising order as the placement fallback when a request fails. An empty successful complete_the_look response is valid when the outfit has no compatible open slot or no eligible product is available for that slot. Hide the empty row instead of filling it with unrelated products.

track

Send a commerce event.
virtual.fit adds the current product context when it is available. Explicit payload fields take precedence. Only call track after storefront analytics has initialized. Events sent before analytics is ready are not queued and may be dropped with a console warning. See the commerce event reference →

Object-style alias

The loader also exposes window.VirtualFit:
Available methods are init, setContext, setProduct, setCustomer, setConsentMode, recommend, and track. Prefer the queued virtualfit(...) form for commands that do not return a value. Call recommend after the hosted loader has initialized because it returns a promise with the ranked products.

SPA integration pattern

Keep one loader and one initialization per page. Update context instead of injecting another script after each route or variant change.