Skip to main content

๐Ÿงฑ Standalone Cart API

Every useCart method has a standalone equivalent exposed on the client returned by createShopifyClient.
Ideal for API routes, server actions, or custom logic without React context.


๐Ÿ”Œ Create Your Shopify Clientโ€‹

To use any standalone method, instantiate your client once:

import { createShopifyClient } from "@nextshopkit/sdk/server";

const client = createShopifyClient({
shop: process.env.NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN!,
token: process.env.NEXT_PUBLIC_SHOPIFY_ACCESS_TOKEN!,
apiVersion: "2025-01",
enableMemoryCache: true,
defaultCacheTtl: 300,
enableVercelCache: true,
defaultRevalidate: 60,
});

๐Ÿงฐ Available Methodsโ€‹

Each method is accessible directly via the client:

MethodDescription
client.createCart()Create a new cart
client.getCart(cartId)Fetch cart by ID
client.addToCart(cartId, lines)Add product(s) to cart
client.removeFromCart(cartId, lineId)Remove a line item
client.updateCartItem(cartId, lineId, qty)Change line quantity
client.applyDiscount(cartId, code)Apply discount
client.removeDiscount(cartId)Remove discount
client.emptyCart(cartId)Remove all items
client.mergeCarts(source, destination)Merge two carts
client.updateBuyerIdentity(cartId, data)Set user info
client.updateCartAttributes(cartId, attrs)Add custom fields

๐Ÿ” Examplesโ€‹

๐Ÿ†• Create Cartโ€‹

const cart = await client.createCart();

๐Ÿ“ฆ Add Productโ€‹

await client.addToCart(cart.id, [
{ merchandiseId: "gid://shopify/ProductVariant/123", quantity: 2 },
]);

โŒ Remove Productโ€‹

await client.removeFromCart(cart.id, "gid://shopify/CartLine/456");

๐Ÿ”„ Update Quantityโ€‹

await client.updateCartItem(cart.id, "gid://shopify/CartLine/456", 5);

๐ŸŽŸ๏ธ Apply Discountโ€‹

await client.applyDiscount(cart.id, "SUMMER10");

๐Ÿงผ Empty Cartโ€‹

await client.emptyCart(cart.id);

๐Ÿ”€ Merge Cartsโ€‹

await client.mergeCarts("source-id", "destination-id");

๐Ÿ‘ค Set Buyer Infoโ€‹

await client.updateBuyerIdentity(cart.id, {
email: "user@example.com",
countryCode: "FR",
});

๐Ÿท๏ธ Add Cart Attributesโ€‹

await client.updateCartAttributes(cart.id, [{ key: "gift", value: "๐ŸŽ" }]);

โœ… Next: Types Reference โ†’