JavaScript SDK
Initialize the SDK, mount server-rendered recommendations, build a custom renderer, and manage page and cart-drawer lifecycle safely.
The JavaScript SDK owns the storefront network layer, valid-impression observation, event batching, retry identity, and cart-drawer remount controller. Your storefront owns the mount point and, when you use the facts API, the card markup.
Install
The SDK is published on the public npm registry as @tabcommerceio/cross-sell-sdk.
npm install @tabcommerceio/cross-sell-sdk
import { init } from '@tabcommerceio/cross-sell-sdk';
It is ESM only, has no runtime dependencies, and ships its own TypeScript declarations. It targets the browser and expects DOM APIs, so bundle it into your storefront build rather than importing it on a server.
Bundles use a separate client
This SDK handles individual Upsell recommendations. Use the independent Bundle SDK for bundle offers, exact variant quotes, and verified Add bundle. The two packages can coexist, but their roots, tokens, and events must remain separate.
The published @tabcommerceio/cross-sell-sdk version verified on September 9, 2026 is 0.1.5. Confirm the installed shop has API access and use its actual App Proxy path. These integrations run on Shopify Online Store pages, not on a cross-origin headless storefront.
Initialize one client
Create one client for the page and pass the installed shop’s App Proxy base. The examples use /apps/tab-cross-sell; use the actual configured path for the shop. Supplying document and window enables page-visibility and page-hide flush behavior.
const client = init({
proxyBase: '/apps/tab-cross-sell',
documentRef: document,
windowRef: window,
});
Do not initialize a new client for every card. One client shares the bounded event queue and flush lifecycle.
Mount authoritative HTML
client.mount() calls /recommendations/rendered, inserts the returned HTML, and automatically observes valid impressions from the server-owned data-* identity on each card.
const root = document.querySelector('[data-tab-cross-sell="pdp"]');
let unmountCrossSell = () => {};
if (root) {
unmountCrossSell = await client.mount({
root,
request: {
placement: 'pdp',
source_products: [
{ product_id: 'gid://shopify/Product/1234567890' },
],
cart: {
currency: 'USD',
lines: [],
},
context: {
country: 'US',
language: 'en',
},
render: {
layout: 'carousel',
heading: 'Complete your order',
add_to_cart_label: 'Add',
},
},
onEmpty(mountRoot) {
mountRoot.replaceChildren();
},
});
}
Keep the returned function and call it before replacing or removing that mount generation.
For a PDP, the SDK starts an advisory presence probe and the authoritative rendered request concurrently. Only an explicit positive presence result may show a skeleton. Cart page and cart drawer mounts skip the probe and remain empty until rendered HTML arrives.
The HTML returned by /recommendations/rendered is the render source. Do not rebuild those cards from result.candidates, and do not replace their Campaign, Rule, request, or tracking attributes.
client.mount() mounts HTML and observes impressions. A custom host still owns click handlers, Shopify cart writes, variant selection behavior, and add-to-cart tracking. If you do not want to reproduce those interactions, use the official theme block or app embed. The fully custom path below is the production-safe choice when an agency owns the UI.
Resolve facts for a custom UI
Use client.resolve() when you need complete control over markup:
async function renderRecommendations() {
const result = await client.resolve({
placement: 'cart_drawer',
source_products: cartLines.map((line) => ({
product_id: line.productGid,
variant_id: line.variantGid,
quantity: line.quantity,
})),
cart: {
token: cart.token,
currency: cart.currency,
subtotal: cart.subtotal,
lines: cartLines.map((line) => ({
product_id: line.productGid,
variant_id: line.variantGid,
quantity: line.quantity,
})),
},
context: {
country: market.country,
language: document.documentElement.lang || 'en',
},
});
if (!result || result.candidates.length === 0) {
root.replaceChildren();
return;
}
// Hydrate Shopify product and variant data, then render your cards.
}
The facts response contains decisions[] and candidates[]. Cart placements can combine several rules, so there is no truthful singular Campaign or Rule on the response root. Read these fields from each candidate:
campaign_idrule_idtracking_tokenproduct_id
Keep the identity together through render, click, add-to-cart, and event tracking. Do not take a Campaign ID from one candidate and a tracking token from another.
The facts API does not replace Shopify product and variant hydration for a custom UI. Resolve the sellable variant and Market price from the storefront’s Shopify data source before rendering or adding to cart.
Track a click
client.mount() observes impressions, but a custom interaction handler still tracks clicks and successful add-to-carts. A click event uses the selected card’s identity:
client.track({
event_type: 'click',
tracking_token: candidate.tracking_token,
candidate_product_id: candidate.product_id,
request_id: result.request_id,
campaign_id: candidate.campaign_id,
rule_id: candidate.rule_id,
placement: 'cart_drawer',
attrs: {
variant_id: selectedVariantGid,
},
});
See Tracking and attribution before implementing add-to-cart. A tracked event without the matching Shopify line properties is not enough for reliable order attribution.
Cart drawer controller
Prefer an explicit live mount point:
<div data-tab-cross-sell="cart_drawer"></div>
Then start one controller:
import { createCartDrawerController, init } from '@tabcommerceio/cross-sell-sdk';
const client = init({
proxyBase: '/apps/tab-cross-sell',
documentRef: document,
windowRef: window,
});
const drawer = createCartDrawerController({
documentRef: document,
async mount(root) {
return client.mount({
root,
request: {
placement: 'cart_drawer',
source_products: getCartSourceProducts(),
cart: getCartContext(),
context: getMarketContext(),
render: { layout: 'compact' },
},
});
},
});
drawer.start();
The controller coalesces drawer mutations and remounts when the live mount or native cart-line state changes. Presentation-only mutations should not trigger a recommendation request.
Cleanup
Stop controllers and the client when the storefront navigation system tears down the page:
unmountCrossSell();
drawer.stop();
await client.flush();
client.destroy();
client.destroy() disconnects impression observers, stops scheduled flushing, and makes one final best-effort flush. Delivery errors do not reject into the commerce experience.