Joy Subscription SDK

Joy Subscription SDK – Headless Integration Guide

A complete guide for integrating Joy Subscription into your headless storefront to enable subscription features on your store.


Introduction

If you're using a headless storefront instead of Shopify's default theme, you'll need to integrate the Joy Subscription SDK to enable subscription features on your store.

This guide uses Next.js (App Router) for code examples, but the SDK works with any JavaScript framework. Adapt the code patterns to your specific framework as needed.

circle-info

This feature available for Custom plan. See all pricing optionsarrow-up-right

SDK Features Overview

Feature
Description
For

Product Widget

Displays subscription plan selector on product pages

All merchants

Product Bundle

Allows customers to purchase bundles with subscription

Merchants with bundles

Subscription Box

Dedicated page for customers to build custom subscription boxes

Merchants with subscription box

Customer Portal

Subscription management page for customers

All merchants

Choose Your Integration Scope

Depending on your needs, you can choose one of the following integration levels:

Need
Sections to Complete
Estimated Time

Widget only (display plans on product page)

Part 1 → 4

~2 hours

Widget + Customer Portal

Part 1 → 5

~3 hours

All features

Part 1 → 7

~5 hours


Table of Contents

Part 1: Prerequisites — System requirements and SDK installation

Part 2: Basic Configuration — Environment variables and TypeScript

Part 3: Product Subscription Widget — Display plan selector on product pages

Part 4: Connect Widget to Cart — Handle Add to Cart for subscriptions

Part 5: Customer Portal — Subscription management page for customers

Part 6: Product Bundle — Integrate bundles with subscription

Part 7: Subscription Box — Build subscription box page

Part 8: Shopify Admin Configuration — Set up redirects and customer accounts

Part 9: Deployment — Deploy to your hosting platform

Take a view our demo site: https://headless-demo-five.vercel.app/searcharrow-up-right


Part 1: Prerequisites

System Requirements

Before you begin, make sure you have:

  • A Shopify store with the Joy Subscriptionarrow-up-right app installed

  • Products with selling plans configured in Shopify Admin

  • A headless storefront built with a JavaScript framework (Next.js, Gatsby, Nuxt, Remix, etc.)

  • Storefront API access token from Shopify Admin

  • Node.js version 18 or higher

Get Your Storefront API Access Token

  1. Go to Shopify AdminSettingsApps and sales channels

  2. Click Develop appsCreate an app

  3. Name your app (e.g., "Headless Storefront")

  4. Go to ConfigurationStorefront API → Select required permissions

  5. Click Install app and copy the Storefront API access token

Install the SDK

Run the following command in your project directory:

Or if you're using npm/yarn:


Part 2: Basic Configuration

2.1. Environment Variables

Create a .env.local file (or equivalent for your framework) at your project root:

Important: The SDK runs on the client side and needs access to your Shopify domain and Storefront token. Make sure these variables are exposed to the browser (in Next.js, use the NEXT_PUBLIC_ prefix; other frameworks may have different conventions).

2.2. TypeScript Declarations

The SDK doesn't include TypeScript definitions. Create a declaration file to avoid type errors:

Create file: types/joy-subscription-sdk.d.ts


Part 3: Product Subscription Widget

The widget allows customers to select subscription plans directly on the product page.

3.1. Create Subscription Context

This context shares state between the Widget, Add to Cart button, and Variant Selector.

Create file: components/subscription/subscription-context.tsx

3.2. Create Widget Component

Create file: components/subscription/subscription-widget.tsx

Important: You must pass shopDomain and storefrontAccessToken directly to the constructor. Standalone bundles (joy-subscription-sdk/widget, etc.) do not read config from SubscriptionSDK.configure().

3.3. Add Widget to Product Page

Wrap your product description with SubscriptionProvider and place the widget:

Update file: components/product/product-description.tsx


Part 4: Connect Widget to Cart

4.1. Update Add to Cart Button

Modify your Add to Cart button to support subscription purchases:

Update file: components/cart/add-to-cart.tsx

4.2. Update Server Action

Update file: components/cart/actions.ts

4.3. Connect Variant Selector to Widget

When a customer selects a different variant, notify the SDK:

Update file: components/product/variant-selector.tsx

4.4. Add Discount Code Support

Create server actions for SDK events:

Create file: components/subscription/cart-actions.ts


Part 5: Customer Portal

A page where logged-in customers can manage their subscriptions.

5.1. Create Customer Portal Component

Create file: components/subscription/customer-portal.tsx

5.2. Create Customer Portal Page

Create file: app/pages/joy-subscription/[[...slug]]/page.tsx

Note: The [[...slug]] catch-all route allows the portal to handle sub-pages like /pages/joy-subscription/orders.


Part 6: Product Bundle (Optional)

For stores using the Product Bundle feature.

6.1. Create Product Bundle Component

Create file: components/subscription/product-bundle-widget.tsx

6.2. Add to Product Page


Part 7: Subscription Box (Optional)

For stores using the Subscription Box feature.

7.1. Create Subscription Box Component

Create file: components/subscription/subscription-box.tsx

7.2. Create Subscription Box Page

Create file: app/pages/subscription-box/page.tsx


Part 8: Shopify Admin Configuration

8.1. Create Selling Plans

  1. Go to Shopify AdminAppsJoy Subscription

  2. Create a subscription plan (e.g., "Monthly Subscription - Every 1 month")

  3. Assign the selling plan to your products

8.2. Verify Product Has Selling Plan

Go to Products → Select a product → Check the Purchase options section. You should see your selling plan listed.

8.3. Redirect from Shopify Theme (Optional)

If customers might land on your Shopify theme (e.g., after checkout), you can redirect them to your headless storefront:

  1. Go to Online StoreThemesEdit code

  2. Open layout/theme.liquid

  3. Add the following code right after <link rel="preconnect" ...>:

Replace your-headless-store-domain.com with your actual headless storefront domain.

8.4. Customer Account (Optional)

If you want to use Shopify's New Customer Accounts:


Part 9: Deployment

9.1. Environment Variables

Before deploying, ensure all required environment variables are configured in your hosting platform:

Variable
Required
Description

SHOPIFY_STORE_DOMAIN

Yes

Your Shopify store domain

SHOPIFY_STOREFRONT_ACCESS_TOKEN

Yes

Storefront API token (server-side)

NEXT_PUBLIC_SHOPIFY_DOMAIN

Yes (SDK)

Your Shopify store domain (client-side)

NEXT_PUBLIC_STOREFRONT_TOKEN

Yes (SDK)

Storefront API token (client-side)

Important: Client-side environment variables (like NEXT_PUBLIC_* in Next.js) are typically injected at build time. If you add or change them after deployment, you'll need to rebuild and redeploy for changes to take effect. Refer to your hosting platform's documentation for specific instructions.

9.2. Build and Deploy

Build your project and deploy to your hosting platform of choice. Common options include:

  • Vercel — Optimized for Next.js

  • Netlify — Supports multiple frameworks

  • AWS Amplify — Full-stack deployment

  • Cloudflare Pages — Edge-first deployment

  • Self-hosted — Any Node.js-capable server

Follow your hosting platform's deployment guide for specific instructions.

9.3. Verify After Deployment

After deployment, verify the integration is working:

  1. Visit a product page → Subscription widget should appear

  2. Select a plan → Button changes to "Subscribe"

  3. Click "Subscribe" → Item is added to cart with selling plan

  4. Visit /pages/subscription-box → Subscription box loads (if configured)

  5. Visit /pages/joy-subscription → Customer portal loads (if configured)


Troubleshooting

Widget Not Showing on Product Page

Cause
Solution

Client-side environment variables not set

Add them in your hosting platform's dashboard → Rebuild and redeploy

Config not passed to SDK

Pass { shopDomain, storefrontAccessToken } directly to new WidgetSDK({...})

Product has no selling plan

Add a selling plan to the product in Shopify Admin

Wrong product handle

Verify the handle matches Shopify

"Subscribe" Button Not Appearing

  • Ensure SubscriptionProvider wraps both SubscriptionWidget and AddToCart

  • Use useSubscriptionOptional() in AddToCart (not useSubscription())

Console Error: "SDK requires configuration"

Config was not passed to the constructor. Do NOT use SubscriptionSDK.configure() — standalone bundles have separate module scopes.

Checkout Redirects to Shopify Theme

Add the JavaScript redirect in layout/theme.liquid as described in Part 8.3arrow-up-right.

Variant Change Not Reflected in Widget

Ensure your Variant Selector calls subscription?.notifyVariantChange(variantId) when the variant changes.

Environment Variables Not Taking Effect

Client-side environment variables are typically baked into the JavaScript bundle at build time. If you've added or changed them:

  1. Verify the variables are set correctly in your hosting platform

  2. Trigger a new build/deployment

  3. Clear your browser cache and refresh


Quick Reference

SDK Events

SDK
Event
Data
Purpose

WidgetSDK

plan:selected

{ productId, variantId, sellingPlanId, plan }

Update context when plan is selected

ProductBundleSDK

add-to-cart

{ items: [...], discountCodes: [...] }

Add bundle to cart

BoxSDK

add-to-cart

{ items: [...], discountCodes: [...] }

Add box to cart

PortalSDK

add-to-cart

{ items: [...], discountCodes: [...] }

Add items from portal

Container Elements

SDK
HTML Element

WidgetSDK

<div class="Avada-SubscriptionWidget-Block" />

ProductBundleSDK

<div class="Avada-ProductBundleData-Block" />

BoxSDK

<div class="Avada-SubscriptionBox__Wrapper" />

PortalSDK

<div id="Avada-SubscriptionManagement__Container" />


Need Help?

If you encounter any issues during integration, please contact the Joy Subscription support team:

Last updated