Shop
Show a workspace's products, keep a cart in the visitor's browser, price it live and send orders with bf.shop.
bf.shop is the SDK surface of the Shop plugin. Once the shop is
on for a workspace (Settings → Plugins → Shop), your site can show its
products, keep a cart and send orders. Orders land in the team's Inbox
and in Shop → Orders.
The site key (brandfine_pk_…) does all of it, from a server, a build
step or the browser. Prices are integers in minor units (kuruş,
cents): 125000 in TRY is ₺1.250. formatPrice writes them.
import { createBrandfineClient, createShopCart, formatPrice } from '@brandfine/client'
const bf = createBrandfineClient({ apiKey: import.meta.env.PUBLIC_BRANDFINE_KEY })
// Build time: the catalog, a category's products, one product.
const { items, currency } = await bf.shop.products.list({ category: 'vitamins', locale: 'tr' })
const product = await bf.shop.products.get('mix-plus', { locale: 'tr' }) // null when not on sale
// The browser: a cart of variant ids and quantities.
const cart = createShopCart()
cart.add(product!.variants[0].id, 2, product!.maxPerOrder)
// The cart page: live names, prices and availability.
const quote = await bf.shop.quote(cart.lines(), { locale: 'tr' })
quote.subtotal === null // a line has no price: this is an order request
// Checkout: the server prices the order from its own catalog.
const result = await bf.shop.checkout({
lines: cart.lines(),
customer: { name: 'Ayşe Yılmaz', phone: '+90 532 000 00 00' },
shipping: { address: 'Çavuşlar Mh. 3330 Sk. No:13, Düzce' },
consents: { terms: true },
locale: 'tr',
sourceUrl: location.href,
})
cart.clear()
if (result.next === 'whatsapp') location.href = result.urlThe catalog
| Method | Returns |
|---|---|
shop.config() | { enabled: false }, or the currency, whether prices include VAT, the checkout mode, which fields checkout asks (required / optional / hidden), the legal links the buyer must accept and whether order requests go on to WhatsApp |
shop.products.list(opts) | On-sale products. category (a slug: its products and its subcategories'), post (a post slug: the products whose page it is), slugs, locale, limit (≤ 500), offset |
shop.products.get(slug, { locale }) | One product, or null |
shop.categories.list({ locale }) | The category tree: each category names its parent slug |
A product carries its variants (each with id, options, label,
price, compareAtPrice, available), its options and
attributes, images (the first is the cover), kind (PHYSICAL
products need a delivery address), maxPerOrder, categories, its
page (the published post that is its page) and customConfig, the
site's own fields. price is { min, max } across its variants, or
null when every price is on request.
Static pages stay current: a site Brandfine builds rebuilds when the catalog
changes, and the workspace's publish webhook gets
shop.catalog.changed. The cart and checkout read live prices, so a
page built before a price change never charges the old price.
Cart and checkout
createShopCart({ key? }) keeps { variantId, quantity } lines in
localStorage (in memory in a private window): lines(), count(),
add(id, qty?, max?), set(id, qty), remove(id), clear() and
subscribe(fn), which also hears other tabs.
shop.quote(lines) answers each line's name, variant, image, price and
a problem when it can't be ordered (gone, unavailable,
too_many), plus subtotal (null when a price is on request),
needsAddress and ready.
shop.checkout(input) sends the order. Show the fields config()
lists; a field to fix comes back as a 400 with code: 'shop.invalid'
and params.field (customer.phone, shipping.address,
consents.terms…). A cart line no longer for sale is a 409
shop.line_unavailable with params.variantId. The answer is
{ number, token, next }: next: 'whatsapp' comes with the url that
opens WhatsApp with the order written out (the shop's setting). Keep
token for the thank-you page: shop.order(token) reads the order
back.
Sending orders is limited per visitor and per shop; over it, a 429.