Subi JavaScript SDK
Put the subscription widget where your theme does not, and follow what the shopper picks.
- Object
window.subifySdk - Version5
- Runs inthe shopper's browser
- Keynone needed
Overview
The Subi JavaScript SDK draws the Subi subscription widget on a storefront and tells your code what the shopper picks in it. Use it to show the widget where a theme does not put it by itself: a landing or collection page, a featured product, a quick-buy window.
On any page other than a product page, the widget appears only when Display subscription widget on home page and other pages is turned on in Subi's widget display settings. With it off, renderWidget draws nothing there. A headless mount is not affected.
It runs in the shopper's browser and needs no key. Once loaded it puts one object on the page, window.subifySdk, and sends its events on window.
subify, the app's earlier name. They stay as they are, so code already written against them keeps working.On a headless storefront such as Hydrogen or Next.js, start with Headless storefronts.
function whenSubiReady(run) {
if (window.subifySdk) return run(window.subifySdk);
window.addEventListener("subify:sdkLoaded",
() => run(window.subifySdk), { once: true });
}
whenSubiReady((sdk) => {
sdk.renderWidget("classic-blue-jeans", {
renderPosition: { wrapper: ".widget-wrapper" }
});
});
window.addEventListener("subify:sellingPlanChange", (event) => {
// null when the shopper picks a one-time purchase
console.log(event.detail.sellingPlanGid);
});Loading the SDK
The SDK loads by itself when Subi's app embed is turned on in your theme (Online Store, Themes, Customize, App embeds). You add nothing.
On a headless storefront, or with the app embed turned off, load it with the script tag in the first example.
The subify:sdkLoaded event is sent only once. If your code may run after the SDK has already loaded, check window.subifySdk first and listen for the event only when it is not there yet, as the second example does.
<script
src="https://cdn.subi.co/subify/v5/subify-sdk.js"
defer
></script>function whenSubiReady(run) {
if (window.subifySdk) return run(window.subifySdk);
window.addEventListener("subify:sdkLoaded",
() => run(window.subifySdk), { once: true });
}renderWidget
window.
Draws the subscription widget for one product inside an element of the page you choose. Outside a product page it draws only when Display subscription widget on home page and other pages is on in Subi's widget display settings.
Parameters
productstring | objectrequiredclassic-blue-jeans), or a Shopify product object with its details.configsobjectoptionalrenderPositionobjectoptionalwrapperstringposition"APPEND" | "PREPEND"default "APPEND"APPEND, first with PREPEND.sellingPlanInputobjectoptional<input type="hidden" name="selling_plan" value="692456128832">.wrapperstringidstringoptionaluseCartApibooleanoptionaltrue when your page adds to the cart with a script (a fetch or XMLHttpRequest call to Shopify's /cart/add) instead of submitting a form, and the SDK adds the chosen selling_plan to that call for you. It does nothing on a headless storefront, where your own code puts the plan on the cart line.fetch and XMLHttpRequest. Test that everything else on your site still works.window.subifySdk.renderWidget("classic-blue-jeans", {
renderPosition: {
wrapper: ".widget-wrapper",
position: "APPEND"
}
});{
renderPosition?: {
wrapper: string;
position?: "APPEND" | "PREPEND";
};
sellingPlanInput?: {
wrapper: string;
id?: string;
};
useCartApi?: boolean;
}changeVariant
window.
Tells the widget that a different variant is selected. Call it when your page picks variants in a way the SDK does not follow by itself, or when you change the variant from your own code.
Parameters
targetProductIdnumber | stringrequiredselectedVariantIdnumber | stringrequiredwindow.subifySdk.changeVariant(12345, 1234567);subify:sdkLoaded
Sent once on window, when the SDK has finished loading and window.subifySdk is ready.
Because it is sent only once, code that may run later should check window.subifySdk first (see Loading the SDK).
window.addEventListener("subify:sdkLoaded", () => {
// window.subifySdk is ready
});subify:variantChange
Sent on window when the SDK sees a new variant selected.
event.detail
targetProductIdnumberselectedVariantIdnumberwindow.addEventListener("subify:variantChange", (event) => {
const { targetProductId, selectedVariantId } = event.detail;
});subify:sellingPlanChange
Sent on window when the widget first shows its selected option, and again each time the shopper picks a different one, a subscription or a one-time purchase.
event.detail
targetProductIdnumberselectedSellingPlanobject | nullnull when the shopper picks the one-time purchase.sellingPlanGidstring | nullgid://shopify/SellingPlan/692456128832, or null for a one-time purchase.window.addEventListener("subify:sellingPlanChange", (event) => {
const {
targetProductId,
selectedSellingPlan,
sellingPlanGid,
} = event.detail;
});Headless storefronts
On a headless storefront such as Hydrogen or Next.js, the widget mounts with window.subifySdk.mountHeadlessWidget instead of renderWidget, and your own code puts the chosen plan on the cart line.
Follow Adding the Subi widget to a Hydrogen or headless storefront for the step-by-step recipe. changeVariant and the events on this page work there too.
Help
- Adding the Subi widget to a Hydrogen or headless storefront: the step-by-step recipe.
- The developers page: the Subi API and webhooks, for your store's own backend.
Stuck? Write to [email protected] with your store's address and the page where the widget should appear.