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

TypeScript
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

input

CartLineInput

CartLineInput: the var_... variant id and how many units to add (1 to 999).

options

BaseRequestOptions

locale (defaults to the client locale, stored only when the cart is created) and fetchOptions.

Returns: Promise<CartWithToken>

TypeScript
const cart = await lynkow.cart.addLine({ variantId: 'var_9f2c...', quantity: 2 })
if (cart.cartToken) sessionStorage.setItem('lynkow_cart', cart.cartToken)

claim

TypeScript
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

cartToken

string

The guest token to claim. Defaults to the token the service holds.

options

BaseRequestOptions

fetchOptions passes through.

Returns: Promise<Cart>

TypeScript
await lynkow.cart.addLine({ variantId: 'var_9f2c...', quantity: 2 })
await lynkow.customers.login({ email, password })
const cart = await lynkow.cart.claim()

getCartToken

TypeScript
getCartToken(): string | null

Returns 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

TypeScript
await lynkow.cart.addLine({ variantId: 'var_...', quantity: 1 })

const token = lynkow.cart.getCartToken()
if (token) sessionStorage.setItem('lynkow_cart', token)

removeLine

TypeScript
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

variantId

string

The line's CartLineItem.variantId (var_...).

options

BaseRequestOptions

fetchOptions passes through.

Returns: Promise<Cart>

TypeScript
const line = cart.items[0]
if (line.variantId) {
  const updated = await lynkow.cart.removeLine(line.variantId)
  render(updated.items)
}

retrieve

TypeScript
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

options

BaseRequestOptions & { country?: string }

country (ISO 3166-1 alpha-2) sets the VAT country of this response while the cart has no delivery country; fetchOptions passes through.

Returns: Promise<Cart>

TypeScript
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

TypeScript
setCartToken(token: string | null): void

Restores 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

token

string | null

A token from a previous CartService.getCartToken, or null to clear it.

Returns: void

TypeScript
// 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

TypeScript
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

locale

string

The cart's new language tag (e.g. 'it', 'pt-BR').

options

BaseRequestOptions

fetchOptions passes through.

Returns: Promise<Cart>

TypeScript
const cart = await lynkow.cart.setLocale('it')
console.log(cart.locale) // 'it'

update

TypeScript
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

input

CartUpdateInput

CartUpdateInput: deliveryAddress and shippingOptionId, each optional and clearable with null.

options

BaseRequestOptions

fetchOptions passes through.

Returns: Promise<Cart>

TypeScript
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

TypeScript
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

input

CartLineInput

CartLineInput: the var_... variant id and the number of units to leave on the line.

options

BaseRequestOptions

fetchOptions passes through.

Returns: Promise<Cart>

TypeScript
// 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)