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
Functions let you create computed attributes with JavaScript. Use a Function when you need a value that is not already stored on the product, such as discount percentage, SKU coverage, average margin, or a custom label based on several product fields. A Function has three parts:- Input query - The product data the Function can read.
- JS function - The JavaScript that calculates the value.
- Output schema - The type and shape of the value the Function returns.
The JS function editor is currently in beta. Existing derived attributes and JSONLogic-based computed attributes continue to work.
Prerequisites
Before you create a Function, make sure:- Your products have completed a catalog sync.
- The product data you want to use exists in Layers, such as variants, tags, prices, metafields, or collection titles.
- You know how the result should be used: searchable, filterable, sortable, visible in API responses, or available as a storefront facet.
Create a Function
Name the attribute
Enter a clear name, such as “Discount Percentage” or “Inventory Health”. Layers creates a computed attribute code from the name.
Choose a template or start from scratch
Click Templates to start from a built-in recipe, or write your own Function in the editor.
Define the input query
In Select inputs, select only the product fields your Function needs. Smaller input queries are easier to review and faster to process.
Define the output schema
In Define outputs, describe the value your Function returns. The schema tells Layers whether the result is a number, string, boolean, array, or object.
Preview with a product
Open Test results, choose a product, and click Run. Review Received data and Returned data before saving.
Input query
The input query controls what product data your Function receives. You can use fields such as:title,vendor,productType,handleavailable,publishedAt,createdAt,updatedAttags,namedTags,collectionTitlespriceRangevariantsmetafieldscategory,options,images,featuredMedia
price, compareAtPrice, cost, sku, available, inventoryQuantity, or createdAt.
Output schema
The output schema tells Layers what your Function returns. Supported output types are:numberstringbooleanarrayobject
If a Function returns
null, or returns an empty value for a product, Layers excludes that computed value from the product. This keeps empty computed attributes out of API responses and facet results.Preview and compile status
The editor shows a compile status:- Not compiled - The Function has not been compiled yet.
- Queued - The Function is waiting to compile.
- Compiling - Layers is compiling the Function.
- Ready - The Function compiled successfully and can run during catalog processing.
- Failed - Layers could not compile the Function. Review the compile error in the editor.
- Console output - Logs captured while the Function ran.
- Received data - The input assembled from your input query.
- Returned data - The value returned by the Function.
Function limits
Functions are designed for product data transformation. They must be synchronous and self-contained. Supported:- Plain JavaScript helper functions.
- Array methods such as
map,filter, andreduce. - Built-in JavaScript methods for strings, numbers, objects, arrays, dates,
Math, andJSON.
- Imports or
require. - Network calls such as
fetch. - Async functions,
await, and Promise APIs. - Timers such as
setTimeoutorsetInterval. - Classes.
for,for...of,for...in,while, anddo...whileloops.- Recursive functions.
Troubleshooting
The Run button is disabled
Select a product in the Preview panel and make sure the editor does not show a blocking syntax error.The Function returns no value
Check Received data to confirm your input query includes the field you expect. If the input is empty, add the missing field to the input query.A returned field is missing
Compare Returned data with your output schema. Required fields must be returned for every product where the Function produces a result.The output type is rejected
Make sure the returned value matches the output schema. For example, a boolean field must returntrue or false, not "true" or "false".