Installation
npm install @commerce-blocks/sdk
Configuration
Required Configuration
| Option | Type | Required | Description |
|---|
token | string | Yes | Layers API public token |
sorts | Sort[] | Yes | Sort options ({ name, code }) |
facets | Facet[] | Yes | Facet fields ({ name, code }) |
Optional Configuration
| Option | Type | Description |
|---|
attributes | string[] | Product attributes to fetch |
baseUrl | string | Custom API URL |
fetch | CustomFetch | Custom fetch implementation (SSR, testing) |
Product Configuration
| Option | Type | Description |
|---|
currency | string | Currency for price formatting |
formatPrice | (amount, currency) => string | Custom price formatter |
swatches | Swatch[] | Color swatch definitions |
options | string[] | Product options to expose |
productMetafields | { namespace, key }[] | Product metafields to fetch |
variantMetafields | { namespace, key }[] | Variant metafields to fetch |
includeMeta | boolean | Include _meta in results (experiments, applied rules, variant breakouts) |
Transforms post-process results before caching. Filter aliases map URL-friendly keys to API property names.
| Option | Type | Description |
|---|
transforms.product | ({ base, raw }) => object | Extend products with custom fields |
transforms.collection | (result, raw) => result | Transform collection results |
transforms.search | (result, raw) => result | Transform search results |
transforms.block | (result, raw) => result | Transform block results |
transforms.filters | (filters) => FilterGroup | Custom filter transformation |
filterAliases | FilterAliases | URL-friendly filter key mapping |
Once configured, transforms and aliases are applied automatically:
import { createClient } from '@commerce-blocks/sdk'
const { data: client } = createClient({
token: 'your-token',
sorts: [{ name: 'Featured', code: 'featured' }],
facets: [{ name: 'Color', code: 'options.color' }],
attributes: ['body_html'],
transforms: {
product: ({ raw }) => ({
description: raw.body_html ?? '',
rating: raw.calculated?.average_rating ?? 0,
}),
},
filterAliases: {
color: 'options.color',
size: 'options.size',
brand: { property: 'vendor', values: { nike: 'Nike', adidas: 'Adidas' } },
},
})
// Aliases resolve automatically
const collection = client.collection({ handle: 'shirts' })
await collection.execute({ filters: { color: 'Red', brand: 'nike' } })
// Products now include description and rating from the product transform
Cache Configuration
| Option | Type | Description |
|---|
cacheLimit | number | Max entries in cache |
cacheLifetime | number | TTL in milliseconds |
storage | StorageAdapter | Custom storage adapter for cache persistence (defaults to localStorage in browser) |
initialData | CacheData | Pre-populate cache at initialization |
restoreCache | boolean | Auto-restore from storage on init (default: true) |
Singleton Access
After initialization, access the client anywhere:
import { getClient, isInitialized } from '@commerce-blocks/sdk'
if (isInitialized()) {
const { data: client } = getClient()
if (client) {
// Use client
}
}
Important Notes
All SDK methods return a Result type instead of throwing exceptions. Always check for result.error before accessing result.data.