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.

CallsStorefront tokenUser tokenAPI key
channel, product, productsYesNoYes
cart, createCart, addItemsToCart, setItemQuantityForCart, removeItemsFromCart, addCouponToCart, removeCouponFromCart, addCustomerToCartYesNoYes
order (after the checkout)YesNoYes
logInCustomer, registerCustomer, requestBusinessAccount, password reset and verificationYes——
account, updateAccount, setPasswordForAccount, logInAsOrganisation, logOutAsOrganisationNoYesNo
wishlist, productViewingHistoryYesYesNo

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:

query Product($sku: String!) {
  product(sku: $sku) {
    sku
    label
    brand
    slug
    images
    categories
    filters {
      key
      value
    }
  }
}

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:

  1. Read the channel's lists once, for example when the page loads:

    query ChannelPriceLists {
      channel {
        pricing {
          currency
          lists {
            id
            currency
            is_included_vat
          }
        }
      }
    }
    
  2. Per product, take the price whose price_list.id is the channel list for the currency you show:

    query ProductPrices($sku: String!) {
      product(sku: $sku) {
        prices {
          amount
          original_amount
          price_list {
            id
          }
          vat {
            rate
            country_code
          }
          tiers {
            min_quantity
            amount
            is_percentage
          }
        }
      }
    }
    

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.

·
Note:

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, addItemsToCart fails with the error cannot add to cart, item out of stock.
  • When a SKU in the cart no longer has enough stock, the cart lists it in validation_errors with type: MISSING_STOCK. reference is the SKU, quantity how many are short and items the 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 productaddItemsToCart with sku and quantity.
Change a quantitysetItemQuantityForCart with sku and the new quantity.
Remove a productremoveItemsFromCart with the ids of the cart item (cart.items[].ids).
Show the cartThe cart query, or the cart that every cart mutation returns.
Apply a discount codeaddCouponToCart; 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.

{
  "errors": [
    {
      "message": "invalid permissions",
      "path": ["cart"]
    }
  ],
  "data": { "cart": null }
}
MessageMeans
invalid permissionsThe token can't make this call, for example a user token on a cart mutation. See the tokens.
unauthorized accessThe user token expired or isn't valid. Log the customer in again.
unauthorizedNo token, or a header that isn't a token.
extensions.status: 404The cart, order or product doesn't exist.
cannot add to cart, item out of stockThe 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.

Query Runnerhttps://afosto.app/graphql

No query loaded

Click play on any code block in the docs to load a query here.