Skip to main content

Controller state

All controllers expose reactive state through a plain state getter:
Read controller.state synchronously or subscribe to changes with controller.subscribe(callback) or the standalone subscribe(controller, callback) utility.

Query result

Collection, search, blocks, image search, similar products, and facets controllers all return a paginated QueryResult shape in data:

Blocks result

Blocks results extend QueryResult with block metadata:
anchor_type can be product, collection, cart, home, search, not_found, landing, other, or none.

Autocomplete result

The autocomplete controller returns a different shape:
SDK 3.0.0 ships a MatchedQuery object type for this field, but the API returns plain strings. Treat the values as strings. The type will be corrected in a following SDK release.

Sort orders result

Facets result

Content search result

The searchContent controller returns articles:

Product types

Product

The Product type represents a fully built product returned by the SDK. It extends ProductBase and can be further extended via the transforms.product configuration.
CommerceBlocksID is a string in the format layers-{numericId}. ProductGID is a Shopify Global ID string like gid://shopify/Product/123.

ProductVariant

VariantGID is a Shopify Global ID string like gid://shopify/ProductVariant/456.

Price and PriceRange

The SDK generates the formatted value using the formatPrice function you provide at SDK init, or a default formatter based on Intl.NumberFormat.

Image and FeaturedMedia

Options

RichProductOption is used for the product’s top-level options array. It includes the attribute code (the internal Layers identifier) and an optional swatch with color or image data if defined for that option.

ProductCategory

Error handling

All SDK methods and controllers return { data, error } instead of throwing. Errors are plain Error instances or ApiError instances for HTTP failures.

ApiError

Raised when the Layers API returns a non-2xx response. A 401 usually means the storefrontAccessToken is missing or invalid. A 402 with the message This store has been suspended. means the store is suspended; every storefront API request returns 402 until the suspension is lifted. Messages like "The selected sort order code is invalid." or "The selected facet is invalid." mean a sort or filterGroup references an attribute that is not configured in Layers. See Troubleshooting for fixes.

ConfigError

Raised during createSDK() when the SDK is misconfigured or used in an unsupported environment. A storage-related ConfigError occurs only when you pass a storage value in an environment without localStorage; the SDK does not access localStorage on its own. This error is not exported, so match on error.name === 'ConfigError' or handle error directly rather than using instanceof.

Other errors

Network failures, timeouts, aborted requests, and unexpected thrown values are normalized to Error instances. The original cause is preserved on error.cause when available.

Error handling with reactive state

Handle errors reactively using the controller’s subscribe method. This approach works with any framework:

Exported types

The SDK exports all types from @commerce-blocks/sdk:

Next steps