Products, filters and sorting
products.list (GET /v1/storefront/products)
answers one page of public products for a selection, with the filters a shop page shows beside them. Shop,
category, collection, search and home pages are all this one call.
Parameters
products.list(query, options) takes the selection first and the per-call
options (signal, timeoutMs, currency, locale) second.
| SDK option | Query parameter | Meaning |
|---|---|---|
q | q | Full-text search, up to 200 characters. See Search and suggestions. |
category | category | A category handle. Products in its subcategories are included. |
collection | collection | A collection handle. |
tags | tag (repeated) | Products carrying any of the tags. |
featured | featured | true for featured products only, false for the others. |
filters | filter.<key> (repeated) | Selected filter values, keyed by the filter key. A range is one a..b value. |
handles / skus | handles / skus (repeated) | A batch of up to 100 products by handle or SKU, in the order asked. |
vehicle | year, make, model, submodel, engine | Parts that fit a vehicle; make and model are required. |
sort | sort | The order. See Sorting. |
limit | limit | Products per page, 1 to 100. Default 24. |
page / cursor | page / cursor | A page number, or the previous page’s nextCursor. Not both. |
facets | facets | true for every filter, or a list of filter keys. |
fields | fields | lite (the default), full, or a list of product keys. |
include | include | Heavy parts to add: fitment, oemNumbers, supersessions, jsonLd, descriptionHtml. |
An unknown parameter, or a value it does not accept, answers 400 INVALID_REQUEST with an issue naming it. A category or collection that does not
exist answers 404 NOT_FOUND. Batch lookups silently leave out anything that
is not public: a draft, an archived product and an unknown SKU look the same.
Filters
The merchant sets up filters in Storefront › Filters: each has a key, a label, a source
(brand, product type, tag, category, price, availability, rating, an attribute or a product option) and a
display (checkbox, swatch, range, boolean or dropdown). The examples here assume filters keyed brand, color and price.
- Values within a filter are OR, filters are AND.
brand: ['Bosch', 'Brembo']withcolor: ['Red']is Bosch or Brembo products that are also red. - Ranges are one
a..bvalue:5000..12000, or open-ended5000..and..12000. A price range is in minor units, like every price. A malformed range answers400. - Variant filters match one variant. Filters on variant values, such as options and price, must all hold for the same variant: “red” with “under $50” finds products with a red variant under $50, not a product whose red variant costs more and whose blue one is cheaper.
-
An unknown filter key answers
400 INVALID_REQUEST. A filter the merchant disabled, or whose source is no longer public, is skipped and named in the answer'sapplied.ignored.
import { createCatalogClient } from '@usethrottle/catalog';
const catalog = createCatalogClient({
publishableKey: 'pc_pk_your_browser_key',
});
// Brakes by Bosch or Brembo (OR within a filter), that are also red (AND
// across filters), priced between $50.00 and $120.00 (a range, in cents).
const list = await catalog.products.list({
category: 'brakes',
filters: {
brand: ['Bosch', 'Brembo'],
color: ['Red'],
price: '5000..12000',
},
facets: true,
sort: 'price_asc',
limit: 24,
page: 1,
});
for (const filter of list.filters ?? []) {
// Each value's count is what the shopper gets by adding that value.
for (const value of filter.values) {
console.log(filter.label, value.label, value.count, value.selected);
}
if (filter.range) console.log(filter.label, filter.range.min, filter.range.max);
if (filter.more) console.log(`${filter.label}: more values than shown`);
}
console.log(list.sort, list.page.total, list.applied.ignored);
On the wire, each value is its own filter.<key> parameter; commas and .. are literal:
curl -G "https://catalog.usethrottle.dev/v1/storefront/products" \
-H "X-Catalog-Key: $CATALOG_SERVER_KEY" \
--data-urlencode "category=brakes" \
--data-urlencode "filter.brand=Bosch" \
--data-urlencode "filter.brand=Brembo" \
--data-urlencode "filter.color=Red" \
--data-urlencode "filter.price=5000..12000" \
--data-urlencode "facets=true" \
--data-urlencode "sort=price_asc" \
--data-urlencode "limit=24"
Facets and filter values
With facets, the answer carries filters: one
entry per filter with its key, label, display, its values (each value, label, count, selected and an optional swatch), a range for range filters, and more.
-
A value's
countis the number of products that match every other selected filter plus that value, so the numbers next to a checkbox are what the shopper gets by ticking it. -
Values with no products are left out, unless selected: a selected value that matches nothing comes back with
count: 0, so the shopper can clear it. -
A list answer carries up to 50 values per filter;
more: truemeans there are others.filters.values(key, query)(GET /v1/storefront/filters/:key/values) answers every value of one filter, up to 1,000, counted for the same selection.
import { createCatalogClient, type ProductsQuery } from '@usethrottle/catalog';
const catalog = createCatalogClient({
publishableKey: 'pc_pk_your_browser_key',
});
const query: ProductsQuery = {
category: 'brakes',
filters: { color: ['Red'] },
facets: ['brand', 'color'],
};
const list = await catalog.products.list(query);
const brand = list.filters?.find((filter) => filter.key === 'brand');
// A list answer carries at most 50 values per filter. For "Show all brands",
// ask for every value of that one filter, counted for the same selection.
if (brand?.more) {
const all = await catalog.filters.values('brand', query);
console.log(all.values.length, all.values.map((value) => value.label));
}
Sorting
| Sort | Order |
|---|---|
relevance | Best match first. The default with q, and the request order for handles and skus. |
featured | Featured products first, then a manual collection’s order, else newest. The default otherwise. |
manual | A manual collection’s own order. |
title_asc / title_desc | By title. |
price_asc / price_desc | By the regular price (before any customer-group price), in the answer’s currency. |
newest | Most recently created first. |
updated | Most recently updated first. |
best_rated | Highest rated first. Only while reviews and ratings on product lists are on. |
Without a sort, the list uses relevance with q (or a handles/skus
batch), a collection's own default sort with collection, and featured otherwise. The answer's sort
names the one used, and config().sorts lists the ones the store offers. An
unknown sort, or best_rated while ratings on lists are off, answers 400 INVALID_REQUEST. Ties break by product, so pages never overlap.
Paging
Every list answer carries page: number, limit, total (the exact count of matching products), pageCount and nextCursor. Page by number with page, or follow nextCursor with cursor for infinite scroll; sending both answers 400.
- Offsets are capped. A page number, and a cursor for
relevanceormanual, can reach 10,000 products deep (1,000 forrelevance). Past that the page is empty andpage.cappedistrue. - Cursors on a sort with a stable key are not capped. For
featured, title, price,newest,updatedandbest_rated, the cursor is keyset based: it walks the whole list and stays exact while products change. - A cursor is opaque. Send it back as it came, with the same selection and sort.
import { createCatalogClient } from '@usethrottle/catalog';
const catalog = createCatalogClient({
publishableKey: 'pc_pk_your_browser_key',
});
// Numbered pages: page 3 of 24 products, with the total for the pager.
const third = await catalog.products.list({ category: 'brakes', page: 3 });
console.log(third.page.number, third.page.pageCount, third.page.total);
if (third.page.capped) {
console.log('Past the offset cap: narrow the selection instead.');
}
// Infinite scroll: follow nextCursor until it is null. With a sort that has
// a stable key (title, price, newest, updated, featured, best_rated) the
// cursor is never capped.
let cursor: string | undefined;
do {
const page = await catalog.products.list({
sort: 'newest',
limit: 48,
...(cursor === undefined ? {} : { cursor }),
});
console.log(page.products.map((product) => product.handle));
cursor = page.page.nextCursor ?? undefined;
} while (cursor !== undefined);
Fields and include
Lists answer the lite projection by default, sized for a product grid (a 24-product page is
at most about 30 KB): handle, title, brand, productType, images (the first two, with sizes), priceRange, availability, lowStock, featured, badges (sale and new), variantCount, optionSwatches, seo.title and, while ratings show on lists, rating.
-
fields: 'full'answers the whole product, as a product page gets it: variants with their option values, options, breadcrumbs, descriptions,purchasableand the rest. -
fields: [...keys]answers only those top-level keys (handleis always kept). An unknown key answers400 INVALID_REQUESTnaming it. -
includeadds the heavy parts no projection carries by default:fitment,oemNumbers,supersessions,jsonLdanddescriptionHtml. -
products.get(handle)always answers the full product and takesinclude. A renamed product answers{ redirectTo }with its new handle; see Collections, categories and tags.
import { createCatalogClient } from '@usethrottle/catalog';
const catalog = createCatalogClient({
publishableKey: 'pc_pk_your_browser_key',
});
// The default: the lite projection, sized for a product grid.
const grid = await catalog.products.list({ limit: 24 });
// Only the keys you name (`handle` is always kept): a small sitemap page.
const handles = await catalog.products.list({
fields: ['handle', 'title', 'seo'],
limit: 100,
});
// Full products in a list, with the fitment table they leave out by default.
const parts = await catalog.products.list({
skus: ['BRK-PAD-01', 'BRK-ROT-07'],
fields: 'full',
include: ['fitment'],
});
// A product page is always the full product; `include` adds the heavy parts.
const answer = await catalog.products.get('ceramic-brake-pads', {
include: ['jsonLd', 'descriptionHtml'],
});
if (!('redirectTo' in answer)) {
console.log(answer.product.options, answer.product.breadcrumbs);
}
console.log(grid.products.length, handles.products.length, parts.products.length);
Keep the shop page in its URL
@usethrottle/catalog/url turns a shop page's query string into a
selection and back. serializeShopQuery writes one canonical string per
selection (a stable parameter order, so browser and CDN caches see one URL), parseShopQuery(serializeShopQuery(q)) gives q back, and parameters it does not know (utm_source,
your own) are kept untouched, in their order. toProductsQuery maps the
selection onto products.list, and parseRange
/ formatRange convert range values.
import { createCatalogClient } from '@usethrottle/catalog';
import {
formatRange,
parseRange,
parseShopQuery,
serializeShopQuery,
toProductsQuery,
type ShopQuery,
} from '@usethrottle/catalog/url';
const catalog = createCatalogClient({
publishableKey: 'pc_pk_your_browser_key',
});
// /shop?category=brakes&filter.brand=Bosch&sort=price_asc&utm_source=mail
export async function loadShop(search: string) {
const shop = parseShopQuery(search);
const answer = await catalog.products.list(
toProductsQuery(shop, { limit: 24, facets: true }),
);
return { shop, answer };
}
// The link behind one facet value: it toggles the value and goes back to
// the first page. utm_source and any other parameter the helpers do not know
// stay in the URL, in their original order.
export function facetHref(shop: ShopQuery, key: string, value: string): string {
const selected = shop.filters?.[key] ?? [];
const values = selected.includes(value)
? selected.filter((item) => item !== value)
: [...selected, value];
const next: ShopQuery = {
...shop,
filters: { ...shop.filters, [key]: values },
};
delete next.page;
delete next.cursor;
return `/shop?${serializeShopQuery(next)}`;
}
// A price slider: ranges stay strings in the URL (`5000..12000`). With
// neither bound the price filter is dropped (`..` alone is refused).
export function priceHref(shop: ShopQuery, min?: number, max?: number): string {
const filters = { ...shop.filters };
if (min === undefined && max === undefined) {
delete filters.price;
} else {
filters.price = [
formatRange({
...(min === undefined ? {} : { min }),
...(max === undefined ? {} : { max }),
}),
];
}
return `/shop?${serializeShopQuery({ ...shop, filters })}`;
}
export function selectedPrice(shop: ShopQuery) {
const value = shop.filters?.price?.[0];
return value === undefined ? null : parseRange(value);
}
While the catalog builds its index
Right after the extension is installed, the first storefront read starts a one-time build of the storefront index. Until it finishes:
-
Plain browsing (
q,category,featured, a vehicle, a sort, page numbers) answers200withindexing: true, nofiltersandpage.total: null. -
Anything that needs the index (
filters,tags,collection,handles/skus,currency) and the other read routes answer503 STOREFRONT_INDEXINGwithRetry-After: 30. That answer is never cached. -
A keyset cursor from before the build answers
400 INVALID_REQUEST; start again from the first page.
What the SDK does about it:
- The client retries a read that answers
503 STOREFRONT_INDEXINGonce, afterRetry-After, when that is 30 seconds or less (indexingRetrychanges the limit;falseturns it off). Otherwise, or if the retry answers the same, it throwsCatalogErrorSTOREFRONT_INDEXINGwithretryAfterSeconds. Anindexing: truelist is returned as it is. - The React hooks never wait inside a call: they show the error and fetch once more at the
retry time, and a list with
indexing: trueis fetched once more after 30 seconds.
import { createCatalogClient, isCatalogError } from '@usethrottle/catalog';
const catalog = createCatalogClient({
publishableKey: process.env.CATALOG_SERVER_KEY ?? '',
// The default: wait out one indexing answer when it asks for 30 s or less.
// `false` turns the wait off.
indexingRetry: { maxWaitSeconds: 30 },
});
// Plain browsing still answers during the first index build...
const list = await catalog.products.list({ category: 'brakes' });
if (list.indexing) {
// ...without filters and with page.total null. Show the products, and
// fetch again later for the filters.
console.log('Indexing: filters appear when the catalog finishes.');
}
// ...while a request that needs the index answers 503 STOREFRONT_INDEXING.
try {
await catalog.products.list({ filters: { brand: ['Bosch'] } });
} catch (error) {
if (isCatalogError(error) && error.code === 'STOREFRONT_INDEXING') {
console.log(`Try again in ${error.retryAfterSeconds ?? 30} s`);
} else {
throw error;
}
}