Storefront#
The storefront calls the GraphQL API with the storefront token, as described under Tokens.
Which token for which call#
The storefront token does everything for the shop and the cart. The user token only works for the customer's own account.
| Calls | Storefront token | User token | API key |
|---|---|---|---|
channel, product, products | Yes | No | Yes |
cart, createCart, addItemsToCart, setItemQuantityForCart, removeItemsFromCart, addCouponToCart, removeCouponFromCart, addCustomerToCart | Yes | No | Yes |
order (after the checkout) | Yes | No | Yes |
logInCustomer, registerCustomer, requestBusinessAccount, password reset and verification | Yes | — | — |
account, updateAccount, setPasswordForAccount, logInAsOrganisation, logOutAsOrganisation | No | Yes | No |
wishlist, productViewingHistory | Yes | Yes | No |
A call with a token that may not make it fails with the error invalid permissions. See Errors.
Products#
Listing and searching#
The GraphQL API reads products by SKU; it has no query that lists, searches or filters the catalogue. Build your product and category pages from a list of SKUs you keep yourself, for example from your own catalogue or CMS, and read the details for those SKUs:
products(skus: [...]) reads several SKUs in one call and returns them in nodes.
Which price to show#
product.prices lists the prices of the SKU on every price list in the account, also lists of other channels and of business customers. Pick the price on the price list of your channel:
-
Read the channel's lists once, for example when the page loads:
-
Per product, take the price whose
price_list.idis the channel list for the currency you show:
Amounts are in cents. price_list.is_included_vat tells whether amount includes VAT; vat gives the rate per country to show the other one. When original_amount is higher than amount, show it as the price before discount.
For a logged-in customer, show their own prices with account.prices. See Showing the prices on product pages.
The cart always calculates the price itself, from the channel list and the lists of the customer.
The price you show is for display; the cart's total and items[].total are what the customer
pays.
Stock#
The storefront token can't read stock levels. Whether a SKU can be ordered shows when you add it to the cart:
- When the channel checks stock and the SKU is out of stock,
addItemsToCartfails with the errorcannot add to cart, item out of stock. - When a SKU in the cart no longer has enough stock, the cart lists it in
validation_errorswithtype: MISSING_STOCK.referenceis the SKU,quantityhow many are short anditemsthe cart items to remove. The hosted checkout doesn't continue while the cart has validation errors.
The cart#
Create the cart with createCart when the customer adds the first product. Keep its id between page loads, for example in localStorage or a cookie, and pass it as cart_id to the cart mutations. The Create a cart guide shows the mutations step by step.
| To… | Use |
|---|---|
| Add a product | addItemsToCart with sku and quantity. |
| Change a quantity | setItemQuantityForCart with sku and the new quantity. |
| Remove a product | removeItemsFromCart with the ids of the cart item (cart.items[].ids). |
| Show the cart | The cart query, or the cart that every cart mutation returns. |
| Apply a discount code | addCouponToCart; removeCouponFromCart takes it off. |
Each cart item groups the units of one SKU. ids holds one id per unit, so removing all of them removes the item and removing some lowers its quantity.
When the cart id no longer works#
The hosted checkout turns the cart into an order, and the cart is deleted once the order is paid. A deleted or unknown cart id gives cart: null and an error with extensions.status: 404. Forget the stored id and create a new cart when the customer adds a product. Also create a new cart after the customer comes back from the checkout.
Errors#
The API answers with HTTP 200 and lists what went wrong in errors. A field that failed is null in data.
| Message | Means |
|---|---|
invalid permissions | The token can't make this call, for example a user token on a cart mutation. See the tokens. |
unauthorized access | The user token expired or isn't valid. Log the customer in again. |
unauthorized | No token, or a header that isn't a token. |
extensions.status: 404 | The cart, order or product doesn't exist. |
cannot add to cart, item out of stock | The SKU is out of stock. |
A storefront token or API key that doesn't exist is refused before GraphQL, with HTTP 401. The errors for logging in are listed under Logging in. See Errors for the general shape.