CartWishlistService class (accessed via client.cartWishlist) manages shopping carts for both authenticated and anonymous users, along with customer wishlists.
Overview
The cart and wishlist APIs support full variant tracking and a unified media gallery system. This enables customers to save specific product selections (color, size, etc.) with accurate pricing, real-time stock levels, and variant-specific images.Sandbox isolation: Cart and wishlist items created with a
tybrite_pk_test_* or tybrite_sk_test_* key are stored in the sandbox environment and are never visible to production keys. Every response includes a Tybrite-Environment: sandbox | production header confirming which environment resolved.Security Model
Cart and wishlist endpoints accept publishable keys, so a leaked publishable key alone is not enough to access another customer’s cart. When the request claims acustomer_id (query or body), Galactic Core requires a customer identity — supply either xAuthToken (the customer’s session JWT from client.authentication.login or verifyOtp) or xExternalAuth (a bring-your-own-auth signed assertion, if your store runs its own identity provider); provide one, not both. The resolved customer must match the claimed customer_id, otherwise the request returns 403 forbidden. Anonymous (session-only) carts skip this check and rely on the secrecy of X-Session-Id.
All wishlist endpoints (and
mergeCart) always require a customer identity (xAuthToken or xExternalAuth) because they are inherently customer-scoped. Only cart reads/writes that omit customer_id and rely solely on xSessionId may proceed unauthenticated.Cart Management
getCart
Retrieve the current contents of a cart. This method supports both logged-in customers and anonymous guest sessions.
Response (200)
See Response Objects below for the full
CartItem shape, including the media[] gallery.addToCart
Add a specific product variant to the cart. If the same variant already exists, its quantity will be incremented.
variant_id is required to specify the exact product selection. For products without variants, use the default variant ID.updateCartItem
Update variables for a specific item already in the cart, such as changing the quantity.
clearCart
Remove all items from the cart in a single operation.
removeCartItem
Remove a specific item from the cart.
mergeCart
Combine an anonymous guest cart with a customer’s permanent cart. Carts are matched by both product_id and variant_id to prevent merging different variants of the same product.
mergeCart always requires xAuthToken — it links a guest session to a customer record, so Galactic Core must verify the caller actually owns that customer.Wishlist Management
All wishlist endpoints require
xAuthToken. Wishlists are inherently customer-scoped — there is no anonymous mode. The JWT must resolve to the same customer referenced by customer_id.getWishlist
Retrieve a customer’s saved items, including variant and media details.
Response (200)
addToWishlist
Save a specific product variant for later. Wishlists require an authenticated customer_id.
removeFromWishlist
Remove an item from the customer’s wishlist.
moveWishlistToCart
An atomic operation that removes an item from the wishlist and adds it to the active cart. This operation preserves the variant_id and performs stock validation.
Response (200)
Response Objects
CartItem & WishlistItem
Both cart and wishlist items include structured data for optimized frontend rendering.
Core Logic
Stock Validation
Stock checks are enforced against the specific variant during all cart operations. If the requested quantity exceedsstock_available, the API returns an insufficient_stock error code along with the current available count.
Pricing Logic
Prices are calculated at the variant level using theselling_price field. This ensures that different variations (e.g., larger sizes or premium colors) are priced accurately and that active promotions are automatically applied.
Frontend Patterns
Variant Selection & Add to Cart
When a user selects a variant and adds it to their cart, branch on whether you have a stored customer session:Pro Tip: Use
xSessionId for guest users. Tybrite will persist this guest cart until it is either merged via mergeCart (which requires xAuthToken) or cleared.
