Installation
Loading from a CDN
The SDK ships as a pure ES module with no Node-only dependencies, so it works in the browser with no build step. Use this when you’re integrating directly into a Shopify theme, a static HTML page, or any environment where you don’t runnpm install.
unpkg (recommended)
unpkg serves the published npm package directly. Add the?module query string and unpkg rewrites the SDK’s imports into sibling unpkg URLs, so the browser resolves the entire module graph without a bundler.
esm.sh
esm.sh is an alternative ESM CDN that pre-bundles the SDK and its dependencies into a single file.jsDelivr
jsDelivr mirrors npm with its own ESM transform. Use the+esm suffix to get a browser-compatible bundle.
Import maps
If you load multiple modules from a CDN and want to keep imports short, declare an import map once and use bare specifiers everywhere else:Reusing the SDK across modules
createSDK registers the SDK in a singleton, so any later <script type="module"> block can pull it back with getSDK. This is the cleanest pattern for theme integrations where one snippet boots the SDK and many other sections consume it.
Configuration
Required configuration
Optional configuration
Context
Pass market and shopper context to personalize results across all controllers. Set it globally on the SDK config, per-query onget(), or both. Per-query context shallow-merges with and overrides the global context.
Context fields:
CartProduct:
CustomerContext:
Swatch configuration
Use swatches to map option values to colors or images. Swatches are matched by optionname and value.
Swatch:
Transforms
Transforms post-process response data before it is cached and returned.
Once configured, transforms are applied automatically:
Cache configuration
The SDK uses an in-memory LRU cache with TTL eviction. In the browser, it persists cache values to
localStorage automatically when a storage backend is available and supported. Pass a custom storage backend if you need a different store shape or prefix.
Singleton access
After initialization, access the SDK anywhere:Important notes
All SDK methods return an
ApiResult type ({ data, error }) instead of throwing exceptions. Always check for error before accessing data.Troubleshooting
401 Unauthorized on the first request
If the first get() returns an ApiError with status 401 (for example, POST /api/storefront/v1/browse/frontpage 401), the storefrontAccessToken passed to createSDK() is missing, invalid, or expired.
On Shopify, the storefront token is read from the shop metafield shop.metafields.layers.embed_settings.value.storefrontApiToken. If that metafield is empty or absent in your theme:
- Confirm the Layers app is installed and connected in Shopify admin.
- Trigger a resync from Settings → Integrations in the Layers dashboard so the metafield is written to the shop. See Collection & app metafields.
- Re-render the theme and confirm the metafield now returns a non-empty value.
"The selected sort order code is invalid."
The sort you passed to get() does not match a sort order configured in Layers for the current collection, or the sort order no longer exists in the Layers dashboard.
- Fetch available sort orders at runtime with
sdk.sortOrders({ collectionHandle: 'shirts' }). - Use a
codefrom the returned sort orders when callingcollection.get()or creating a collection controller withsort. - Omit
sortfromget()to fall back to the collection’s default sort order.
"The selected facet is invalid."
A filterGroup you passed references an attribute that is not configured as a facet in Layers.
- Fetch available facets at runtime with
sdk.facets({ collectionHandle: 'shirts' }). - Attribute codes are case-sensitive and must exactly match the attribute code shown in the Layers dashboard (for example,
your-attribute, notYour-Attribute). - Remove filters that reference attributes no longer configured as facets.