Examples
Inventory Enrichment
Populate a product metafield from stock and location data
A worker triggered by Shopify Flow that rolls up a product’s inventory across locations and writes a summary to a product metafield — usable in themes, search filters, or feeds.
Examples are deliberately generic — they’re here to show the moving parts, not to be pasted in verbatim. Comments mark the spots you’ll need to adapt, including the metafield namespace/keys, which must match definitions you create in your Shopify admin.
Setup
- No secrets required — this worker only uses the built-in Admin API client.
- A product metafield definition to hold the summary (for example an
inventory.availability_summaryJSON metafield). - A Flow workflow triggered by something inventory-related (for example Inventory quantity changed), with a Run worker action whose Request body passes the product ID via template variables — for example
{ "productId": "{{ product.id }}" }.
The worker
// Triggered by Flow. The workflow's "Request body" arrives as the POST body
// with template variables already rendered — here we expect { "productId": "gid://..." }
let input;
try {
input = await request.json();
} catch {
return new Response("Expected a JSON body", { status: 400 });
}
const productId = input.productId;
if (!productId) {
return new Response("productId is required", { status: 400 });
}
// Pull the product's variants and their inventory levels across locations.
// this.ctx.props.admin is pre-authenticated and pinned to this worker's
// Admin API version.
const { data } = await this.ctx.props.admin.query(
`#graphql
query ProductInventory($id: ID!) {
product(id: $id) {
title
variants(first: 50) {
nodes {
inventoryQuantity
inventoryItem {
inventoryLevels(first: 20) {
nodes {
location { name }
quantities { name quantity }
}
}
}
}
}
}
}`,
{ id: productId },
);
// Roll the levels up into whatever summary is useful for your storefront —
// total stock, per-location availability, an in-stock flag, and so on.
// The shape below is a placeholder; build it from `data.product` to suit
// your theme.
const summary = {
title: data?.product?.title ?? null,
total_quantity: 0, // sum variant/location quantities here
locations: [], // e.g. [{ name, available }]
computed_at: new Date().toISOString(),
};
// Write the summary back to the product metafield your theme reads.
const result = await this.ctx.props.admin.query(
`#graphql
mutation SetSummary($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
userErrors { field message }
}
}`,
{
metafields: [
{
ownerId: productId,
namespace: "inventory", // must match your metafield definition
key: "availability_summary",
type: "json",
value: JSON.stringify(summary),
},
],
},
);
// GraphQL errors inside a 200 response don't throw — check userErrors so a
// bad write doesn't look like success.
const userErrors = result.data?.metafieldsSet?.userErrors ?? [];
if (userErrors.length > 0) {
return new Response(`Metafield write failed: ${userErrors[0].message}`, {
status: 502, // 5xx tells Flow to retry
});
}
return new Response("ok"); Notes
- Flow triggers pass different objects depending on the trigger — check your trigger’s available variables and adapt the body template and parsing.
- This pattern also suits scheduled refreshes: pair a scheduled Flow trigger with a product query to rebuild summaries in batches.
- Keep the response body small — anything returned here becomes the workflow’s action output and is truncated at 45 KB.