Builds and reads the buyer's cart, accessible via lynkow.cart. Requires publishableKey.
A guest's first CartService.addLine creates the cart and the service keeps its
token in memory; a signed-in buyer's cart comes from their session. Calls run one at a
time in invocation order, and a call whose buyer or token changes before it completes
rejects with UNAUTHORIZED. Never cache a cart response: if the client's fetchOptions
revalidate, pass { fetchOptions: { cache: 'no-store' } } on cart calls.
Access via: lynkow.cart
Methods
9 methods
addLine
addLine(input: CartLineInput, options?: BaseRequestOptions): Promise<CartWithToken>Adds units of a variant to the cart, creating the cart on first use. A variant
already in the cart has its line increased, capped at 999 units. Every line takes
the current catalogue price. On a guest's first add the response carries
cartToken, which the service keeps. The locale is stored as the cart's language
only when this call creates the cart. Throws LynkowError NOT_FOUND (apiCode
CART_NOT_FOUND) for a variant that cannot be bought, and VALIDATION_ERROR with
apiCode OUT_OF_STOCK, or CART_LINES_LIMIT_REACHED for a 101st line.
Parameter | Type | Description |
|---|---|---|
|
| CartLineInput: the |
|
|
|
Returns: Promise<CartWithToken>
const cart = await lynkow.cart.addLine({ variantId: 'var_9f2c...', quantity: 2 })
if (cart.cartToken) sessionStorage.setItem('lynkow_cart', cart.cartToken)claim
claim(cartToken?: string, options?: BaseRequestOptions): Promise<Cart>Moves the guest cart to the signed-in buyer; call it once after sign-in. A buyer
with no cart adopts the guest cart. Otherwise the guest lines merge into theirs
(quantities summed, capped at 999), so re-read cart.id. A wrong or used token
moves nothing and returns the buyer's own cart. Throws LynkowError BAD_REQUEST
locally when no token is passed or held, UNAUTHORIZED (apiCode
CUSTOMER_AUTH_REQUIRED) without a session, and VALIDATION_ERROR with apiCode
OUT_OF_STOCK or CART_LINES_LIMIT_REACHED when the merge would exceed them.
Parameter | Type | Description |
|---|---|---|
|
| The guest token to claim. Defaults to the token the service holds. |
|
|
|
Returns: Promise<Cart>
await lynkow.cart.addLine({ variantId: 'var_9f2c...', quantity: 2 })
await lynkow.customers.login({ email, password })
const cart = await lynkow.cart.claim()getCartToken
getCartToken(): string | nullReturns the guest cart token this client holds, without a request. It is null
before the first guest add, after CartService.claim retires it, and after
setCartToken(null). Persist it to keep a guest cart across a page reload. Whoever
holds it can read and change the cart, so store it in the visitor's own browser.
Returns: string | null
await lynkow.cart.addLine({ variantId: 'var_...', quantity: 1 })
const token = lynkow.cart.getCartToken()
if (token) sessionStorage.setItem('lynkow_cart', token)removeLine
removeLine(variantId: string, options?: BaseRequestOptions): Promise<Cart>Removes a variant's line and returns the recomputed cart, whose items is empty
after the last line. Throws LynkowError NOT_FOUND (apiCode CART_NOT_FOUND)
when there is no cart, no line for that variant, or a malformed id.
Parameter | Type | Description |
|---|---|---|
|
| The line's CartLineItem.variantId ( |
|
|
|
Returns: Promise<Cart>
const line = cart.items[0]
if (line.variantId) {
const updated = await lynkow.cart.removeLine(line.variantId)
render(updated.items)
}retrieve
retrieve(options?: BaseRequestOptions & { country?: string }): Promise<Cart>Fetches the buyer's cart: the signed-in buyer's own cart, else the cart of the held
guest token. The response is already healed: lines no longer sellable, or out of
stock on a site that refuses overselling, are removed and listed in
Cart.removedLines. Line names are translated into Cart.locale. Throws
LynkowError NOT_FOUND (apiCode CART_NOT_FOUND) when this buyer has no cart,
a wrong or retired token included, and VALIDATION_ERROR for an invalid country.
Parameter | Type | Description |
|---|---|---|
|
|
|
Returns: Promise<Cart>
try {
const cart = await lynkow.cart.retrieve()
render(cart.items, cart.totalCents)
} catch (error) {
if (isLynkowError(error) && error.code === 'NOT_FOUND') renderEmptyCart()
else throw error
}setCartToken
setCartToken(token: string | null): voidRestores a guest cart token your app saved from CartService.getCartToken, or
clears it with null, without a request. Clear it when the visitor signs out on a
shared browser; the client's destroy() does it too. The token is not checked here:
a wrong one makes the next cart call throw NOT_FOUND. Restore only a token stored
for this visitor, never one read from a URL or a message, since it grants full access
to the cart.
Parameter | Type | Description |
|---|---|---|
|
| A token from a previous CartService.getCartToken, or |
Returns: void
// On page load, hand back what the previous session stored.
const stored = sessionStorage.getItem('lynkow_cart')
if (stored) lynkow.cart.setCartToken(stored)
const cart = await lynkow.cart.retrieve().catch(() => null)setLocale
setLocale(locale: string, options?: BaseRequestOptions): Promise<Cart>Sets the cart's language and returns the cart. Line names are served in it when the
product is translated, and the order and its emails follow it. Any language tag is
accepted, stored normalized (at most 35 characters); a value holding no usable tag
changes nothing. Throws LynkowError NOT_FOUND (apiCode CART_NOT_FOUND) when
this buyer has no cart.
Parameter | Type | Description |
|---|---|---|
|
| The cart's new language tag (e.g. |
|
|
|
Returns: Promise<Cart>
const cart = await lynkow.cart.setLocale('it')
console.log(cart.locale) // 'it'update
update(input: CartUpdateInput, options?: BaseRequestOptions): Promise<Cart>Sets the delivery address and shipping option, and returns the cart with shipping
and totals recomputed. An absent key keeps the stored value, null clears it, and
an address replaces the stored one whole. An option that is not active in the
delivery country's market is cleared rather than refused, so read shippingOptionId
on the result. Throws LynkowError NOT_FOUND (apiCode CART_NOT_FOUND) when this
buyer has no cart, and VALIDATION_ERROR for a malformed option id or address.
Parameter | Type | Description |
|---|---|---|
|
| CartUpdateInput: |
|
|
|
Returns: Promise<Cart>
const { data: options } = await lynkow.shippingOptions.list({ market: 'mkt_...' })
const cart = await lynkow.cart.update({
deliveryAddress: { firstName: 'Alice', address1: '12 rue de la Paix', city: 'Paris', postalCode: '75002', countryCode: 'FR' },
shippingOptionId: options[0]?.id ?? null,
})
if (cart.shippingOptionId === null) {
// The choice was not kept: ask the buyer to pick again.
}updateLine
updateLine(input: CartLineInput, options?: BaseRequestOptions): Promise<Cart>Sets the exact quantity of one variant's line (1 to 999) and returns the recomputed
cart. Remove a line with CartService.removeLine: quantity 0 is refused. The
line takes the current catalogue price. A target line no longer sellable or out of
stock is removed instead and reported in Cart.removedLines. Throws
LynkowError NOT_FOUND (apiCode CART_NOT_FOUND) when there is no cart or no
line for that variant, and VALIDATION_ERROR with apiCode OUT_OF_STOCK when the
quantity exceeds tracked stock on a site that refuses overselling.
Parameter | Type | Description |
|---|---|---|
|
| CartLineInput: the |
|
|
|
Returns: Promise<Cart>
// The buyer typed 3 into the quantity box: set the line to exactly 3.
const cart = await lynkow.cart.updateLine({ variantId: 'var_9f2c...', quantity: 3 })
console.log(cart.subtotalCents)