Documentation Index
Fetch the complete documentation index at: https://docs.uselayers.com/llms.txt
Use this file to discover all available pages before exploring further.
Overview
A complete Layers search implementation in a Shopify theme has four moving pieces:- Autocomplete — typeahead suggestions as the shopper types in the search field.
- Prepare — kick off expensive personalization and query understanding before the search results page loads, so it’s ready by the time the page is hydrated.
- Execute — fetch ranked, faceted results using the prepared
search_id. - Facets and sorts — render filter and sort controls from Liquid metaobjects, then refresh results as the shopper interacts.
Prerequisites
- The Layers Shopify app installed and synced.
- A storefront access token from
shop.metafields.layers.embed_settings. See Liquid integration. - The Storefront Pixel installed so
deviceIdandsessionIdare populated automatically. For headless contexts you can pass them manually in theidentitybody field.
1. Autocomplete in the header
The Autocomplete endpoint is aGET request that returns suggested queries as the shopper types. It also surfaces semantic redirects — if the typed text matches a configured redirect, the response includes _meta.redirect and your theme should navigate the shopper there directly.
AbortController for that.
2. Prepare the search before the results page
Callprepare the moment the shopper commits to a search (presses enter or clicks a suggestion). The endpoint returns a search_id immediately while Layers continues working on personalization in the background. The prepared data is cached for 15 minutes.
You have two practical options:
Option A — Prepare on submit, persist the search_id. Best when you can intercept the form submit.
3. Execute the search
On the results page, call the Search API with the preparedsearch_id. If prepare hasn’t returned yet, execute waits briefly for it; if more than 15 minutes have passed (or the id is missing), Search falls back to normal processing automatically.
| Field | Purpose |
|---|---|
search_id | ULID returned by prepare. Reuses the cached personalization work. Omit to fall back to standard processing. |
sort_order_code | The selected sort metaobject code. Omit for relevance-ranked results. |
filter_group | Active filters expressed via filter expressions. |
facets | List of facet codes (or wildcards like options.*) to return values for. |
retrieveFacetCount | Include per-value result counts. |
includeFacetRanges | Include min/max for numeric facets (price, ratings). |
4. Render facet and sort controls from Liquid
Both controls follow the same metaobject pattern documented elsewhere — read the metaobjects in Liquid, then pass codes to the API. Filter sort orders by thesearch scope on the search page.
- Rendering facets in Liquid — populating filter values with counts and ranges
- Rendering sort orders in Liquid — sort dropdown wiring
Recommended request flow
The end-to-end timeline for a typical search:Header submit
Shopper hits enter. Fire
prepare (non-blocking) and navigate to /search?q=.... Prepare keeps running while the browser loads the results page.Results page render
Liquid renders the sort dropdown and empty facet groups from metaobjects, including the active sort/filter state from the URL.
Execute
Page JS calls
execute with the search_id from prepare, plus any active sort and filters from the URL. Render the product grid and hydrate facet values from the response.Submitting click feedback
Once a shopper clicks a result, call the Search feedback endpoint with the click event. This is what trains relevancy over time — without it, semantic ranking and personalization can’t improve. The Storefront Pixel handles this automatically for themes using the standard search results template. If you’re rendering your own grid, send the feedback manually using theattributionToken returned in the execute response.
Troubleshooting
execute returns no results for a known-good query. Check that prepare and execute use the same searchQuery path parameter (and URL-encoded identically). A mismatched query invalidates the prepared cache and falls back to standard processing.
Suggestions show but redirects don’t fire. The redirect lives at data._meta.redirect.url. Make sure your typeahead handler checks for it before rendering the suggestion list, and that the configured redirect term in the dashboard actually matches the typed query.
Stale sort or filter state after back navigation. Read the active state from request.params in Liquid on first render, and mirror every sort/filter change to the URL via history.replaceState. The next page load and any back navigation will then hydrate correctly.
Prepare returns 202 but execute is slow. The prepared cache lives for 15 minutes. If execute runs much later, the cache is gone and you’re paying normal processing cost. Don’t cache search_id values across sessions.
See also
- Liquid integration — metaobject and metafield access
- Rendering facets in Liquid — filter sidebar pattern
- Rendering sort orders in Liquid — sort dropdown pattern
- Search API and Prepare Search endpoints
- Autocomplete API — typeahead suggestions
- Filter expressions — filter group grammar