Overview
This is the SDK equivalent of Rendering blocks in Liquid. The shape of the section is the same: one Liquid section, one merchandiser-facing block-picker setting. The SDK’sblocks() controller does the HTTP call, deduplicates in-flight requests, caches responses in memory for the life of the page, and exposes the products on reactive state.
The SDK sends exactly the values your code supplies and returns results with an attributionToken for tracking.
Use this version when you have already installed @commerce-blocks/sdk in your theme. For a no-dependencies version, see the Liquid + Fetch guide.
Prerequisites
- The Layers Shopify app installed and the theme app extension enabled in Online Store → Themes → Customize → App embeds.
- The SDK installed and initialized in your theme. See SDK installation.
The section
sections/layers-recommendations.liquid is the same Liquid scaffold as the Fetch version: a section with a block-picker setting and a single placeholder element carrying the selected block ID.
select options are populated from block metaobjects. See the Fetch guide for the pattern that mirrors metaobjects into the dropdown so merchandisers select by name instead of pasting a ULID.
Hydrating the section
For each rendered section, create ansdk.blocks() controller with the selected block ID and the page anchor. Subscribe to the state and render on data changes.
Identity, context, and attribution
The app embed provides the storefront pixel and its standard theme tracking. Pass the values your block requests need through the SDK:- Identity: Pass
identitytocreateSDK()as a static object or a resolver that runs on every request. On a pixel-driven storefront, read device and session identifiers from the storefront pixel and pass them through. The SDK does not read them for you. - Cart context and exclusions: Pass
contextandexcludeProductIdson the request when you need cart-anchored blocks or cross-block product deduplication. - Click attribution: Results carry an
attributionToken. The storefront pixel covers standard themes. For custom-rendered cards, forwardattributionTokenon your click and add-to-cart tracking.
Anchors
sdk.blocks({ blockId, anchorId }) resolves the right value based on template:
Both numeric IDs and Shopify GIDs are accepted on product anchors; both IDs and handles are accepted on collection anchors.
Why the SDK over Fetch
Both paths send exactly the cart context, exclusions, identity, and attribution values your code passes.