Skip to main content

Overview

Henry’s Universal Cart helpers let you manage a shopping cart for each shopper. Pass results from Product Discovery or bring your own product links, and Henry persists carts, quantities, variant checks, and totals for you.

Prerequisites

  • Henry API key (sandbox or production)
  • @henrylabs/sdk installed on your server
  • Stable x-user-id value that uniquely identifies the shopper in your system
import HenrySDK from "@henrylabs/sdk";

const client = new HenrySDK({
  apiKey: process.env.HENRY_API_KEY!,
});
1

Add or update items

Use client.cart.items.add to append new products or adjust quantities. Pass the shopper identifier via the x-user-id request option.
const cart = await client.cart.items.add({
  'x-user-id': 'user_123',
  checkVariantAvailability: true,
  productsDetails: [
    {
      productId: 'P01145AC2',
      name: 'Cloud 6 Versa',
      price: '149.00',
      quantity: 1,
      productLink: 'https://merchant.com/products/cloud-6-versa',
      productImageLink: 'https://merchant.com/images/cloud-6-versa.png',
      metadata: { size: '9', color: 'Black' },
    },
  ],
});

console.log(cart.data.cart_summary);
The response returns added_products, updated_products, and a cart_summary with totals. When checkVariantAvailability is true, Henry starts variant checks and surfaces their IDs in data.variant_checks.
2

List cart contents

Fetch the current cart to render a mini-cart or checkout summary.
const items = await client.cart.items.list({
  'x-user-id': 'user_123',
});

console.log(items.data.products);
Each product echoes the metadata you supplied plus computed totals.
3

Remove individual products

When a shopper deletes a line item, call client.cart.items.remove. The productId must match the identifier you originally supplied.
await client.cart.items.remove('P01145AC2', {
  'x-user-id': 'user_123',
});
4

Clear the cart

Reset the cart entirely (for example, on logout) with client.cart.items.clear.
await client.cart.items.clear({
  'x-user-id': 'user_123',
});
The response confirms the cart is empty. Henry also clears carts automatically after a successful checkout.
Reuse the same x-user-id in downstream checkout calls so Henry can hydrate the cart automatically when you initiate a hosted or headless session.

Troubleshooting

  • 400 Bad Request – validate price formatting (strings) and ensure the quantity is greater than zero.
  • 404 Cart already empty – surfaced when clearing an empty cart; you can safely ignore it for idempotent flows.
  • Variant checks return IDs in data.variant_checks; poll them with client.products.variantCheck.retrieveStatus for a definitive in-stock answer.
Continue to Cart Checkout once you are ready to convert carts into orders.