Documentation menu

DocsStorefront features

Notify Me When Back in Stock

Shopify's Storefront API has no native "back in stock" subscription, so <NotifyWhenAvailable> and its resource route ship as a documented stub. The form, the validation and the route are fully built and tested, but the subscribe function returns {ok: true} without storing or sending anything until you wire it to something real. This guide covers three drop-in ways to do that: a Klaviyo list, a custom serverless endpoint tied to your own inventory webhook, or a metafield-backed waitlist app. None of them require touching the component, the route or the existing tests.

It's for a developer who needs sold-out variants to actually notify someone on a real client project, where a form that looks finished and sends nothing won't do. Afterwards you'll know which of the three backends fits the store's existing tooling, and which three files (notifyBackInStock.ts, its test file, .env.example) change when you wire it up.

Related: Architecture for why this ships as a documented stub instead of a built-in integration, and B2B-Ready Data Layer for the same documented-but-unwired pattern applied to a different feature.

Source: docs/guides/notifyBackInStock.md

<NotifyWhenAvailable> renders in place of a disabled "Add to cart" state whenever the selected variant on a product page isn't available for sale (app/components/ProductForm.tsx). It's a small email-capture form that posts to the /notify-back-in-stock resource route (app/routes/notify-back-in-stock.tsx), which validates the input and hands it off to subscribeToBackInStock in app/lib/notifyBackInStock.ts.

Why this ships as a stub

Shopify's Storefront API has no native "back in stock" subscription primitive; merchants handle this with a third-party app or their ESP. This template documents integration points for commodity features like this one instead of building in an app (see the architecture doc's note), so subscribeToBackInStock ships as a stub: it validates the email and product info and returns {ok: true}, but doesn't persist or send anything. You need to wire it to something real before relying on it in production.

The stub keeps the rest of the stack (the route, the component, the form UX, the tests) fully usable and testable without an external dependency, so you can build against it immediately and swap in a real backend later without touching anything above app/lib/notifyBackInStock.ts.

Wiring it to a real backend

All three options below are drop-in replacements for the body of subscribeToBackInStock. The function's signature and the route/component that call it don't need to change.

Option 1: Klaviyo list

Add the visitor to a dedicated Klaviyo list (or a list segmented by variantId) via Klaviyo's Profiles API. This is the natural choice if you're already using Klaviyo for the email-capture component in the analytics/growth slice of this template, since it keeps everything on one ESP with one API key, and you manage the resulting campaign in the same place.

export async function subscribeToBackInStock(input: NotifyBackInStockInput) {
  // ...existing validation...

  const response = await fetch(
    'https://a.klaviyo.com/client/subscriptions/?company_id=YOUR_PUBLIC_KEY',
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        revision: '2024-10-15',
      },
      body: JSON.stringify({
        data: {
          type: 'subscription',
          attributes: {
            profile: {
              data: {type: 'profile', attributes: {email: input.email}},
            },
            custom_source: 'back-in-stock',
          },
          relationships: {
            list: {data: {type: 'list', id: 'YOUR_LIST_ID'}},
          },
        },
      }),
    },
  );

  return response.ok
    ? {ok: true}
    : {ok: false, error: 'Something went wrong. Try again shortly.'};
}

Option 2: A custom serverless endpoint

If you'd rather own the data (e.g. to trigger a restock email through your own transactional email provider once inventory updates), have the stub call your own endpoint, and drive the actual restock notification from a Shopify inventory webhook on the backend:

export async function subscribeToBackInStock(input: NotifyBackInStockInput) {
  // ...existing validation...

  const response = await fetch(process.env.BACK_IN_STOCK_WEBHOOK_URL!, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(input),
  });

  return response.ok
    ? {ok: true}
    : {ok: false, error: 'Something went wrong. Try again shortly.'};
}

Option 3: A metafield-backed waitlist app

Some Shopify apps (and custom builds) store back-in-stock signups as a variant or product metafield instead of in an external system. If you're integrating with one of those, this usually means writing the metafield through the Admin API from a server-side context. The Storefront API is read-only for this purpose. Typically you'd do it from the same resource route action, using an authenticated Admin API client in place of the storefront client already available in Hydrogen's app context.

What to change if you swap the backend

  • app/lib/notifyBackInStock.ts: replace the stub body, keep the NotifyBackInStockInput/NotifyBackInStockResult shape so the route and component don't need changes.
  • app/lib/notifyBackInStock.test.ts: update or mock the new network call; the existing validation tests (empty/malformed email, missing product info) should still pass unchanged.
  • .env.example: document whatever new environment variable the integration needs (API key, list ID, webhook URL).