Stored values: what your custom triggers remember
How custom trigger code in Workflow Trigger Extensions remembers values between events with ctx.track, ctx.kv and ctx.storage, how much each plan stores, and how to view and delete stored values.
A custom trigger sees one event at a time. To compare an event with the last one, to count events or to fire only once, your code has to remember something between events. The app gives your code three ways to do that, and a tab where you can look at everything that is stored and delete it.
| In your code | Use it for | Good to know |
|---|---|---|
ctx.track(key, value) |
The value before and after a change, for anything your code can read | Kept for 90 days after the record was last seen |
ctx.kv |
Counters, "only once" flags and small lookups | Values up to 64 KB, up to 100 calls per run |
ctx.storage |
Values that are too large for the key-value store | Slower than ctx.kv, does not expire |
All three are part of every plan. They are for Custom triggers. The built-in triggers keep their own old and new values, see Old and new values: what a workflow can do with them.
ctx.track: before and after for anything
await ctx.track(key, value) remembers value under key and gives back what was remembered the last time:
| Field | Meaning |
|---|---|
first |
true the first time this key is seen. Nothing has changed yet. |
changed |
true when the value differs from the remembered one. Always false the first time. |
before |
The value remembered last time, null the first time. |
after |
The value you passed in. |
changedKeys |
When the value is a plain object: the names of its keys that changed. |
- Put the record's id in the key, for example
"order-total:" + payload.id. Every record then has its own remembered value, and the app can remove it when Shopify asks for that customer's data to be erased. - A remembered value is kept for 90 days after its record was last seen. Pass
{ ttlSeconds }as a third argument to keep it shorter or longer, up to 365 days. await ctx.forget(key)removes one remembered value.- A test run in the editor keeps its own copy, so testing never moves the value a live event is compared with.
The template Order total changed (before and after) works this way. The order event does not carry the total and no event carries the old one, so the code reads the total from your store and lets ctx.track remember it:
export async function transform(payload, topic, shop, ctx) {
const id = payload.admin_graphql_api_id || "gid://shopify/Order/" + payload.id
const data = await ctx.shopify(
`query Total($id: ID!) {
order(id: $id) { name currentTotalPriceSet { shopMoney { amount currencyCode } } }
}`,
{ id },
)
if (!data.order) return null
const money = data.order.currentTotalPriceSet.shopMoney
// The key carries the order id: every order has its own remembered total.
const change = await ctx.track("order-total:" + payload.id, money.amount)
if (!change.changed) return null // also the first time this order is seen
return {
orderName: data.order.name,
oldTotal: change.before,
newTotal: change.after,
currency: money.currencyCode,
}
}ctx.kv: counters, flags and small lookups
Your store's own key-value store.
| Call | What it does |
|---|---|
await ctx.kv.get(key) |
The stored value, or null when the key does not exist or has expired |
await ctx.kv.set(key, value, { ttlSeconds }) |
Stores a value. ttlSeconds is optional; without it the value does not expire |
await ctx.kv.increment(key, amount, { ttlSeconds }) |
Adds to a counter and returns the new number. A negative amount subtracts. Two events at the same moment never lose a count |
await ctx.kv.delete(key) |
Deletes a key |
await ctx.kv.list({ prefix }) |
Your keys that start with prefix, up to 1,000, with their size and expiry |
A value can be up to 64 KB and a key up to 512 characters. ttlSeconds can be between 1 second and 365 days. One run can make up to 100 calls.
The template Product updated more than N times in a day counts with it and fires exactly once:
const LIMIT = 20
export async function transform(payload, topic, shop, ctx) {
const day = new Date().toISOString().slice(0, 10)
// A counter per product and day; it expires on its own after two days.
const count = await ctx.kv.increment("updates:" + payload.id + ":" + day, 1, { ttlSeconds: 172800 })
// Exactly the update that crosses the line, so it fires once, not on every one after.
if (count !== LIMIT + 1) return null
return { title: payload.title, updatesToday: count, limit: LIMIT, day }
}ctx.storage: larger values
ctx.storage.get, ctx.storage.set, ctx.storage.delete and ctx.storage.list are for values that do not fit the key-value store. It is slower than ctx.kv and its values do not expire, so use it only when a value is too large for ctx.kv.
How much you can store
| Plan | Keys in the key-value store | Large value storage | Largest value in ctx.storage |
|---|---|---|---|
| Free | 1,000 | 5 MB in up to 500 keys | 64 KB |
| Starter | 10,000 | 50 MB in up to 5,000 keys | 128 KB |
| Grow | 100,000 | 250 MB in up to 25,000 keys | 256 KB |
| Unlimited | 1,000,000 | 1 GB in up to 100,000 keys | 512 KB |
When the key-value store is full, writing a new key fails with a message that names your limit. Changing a key that already exists always works. Delete keys you no longer need, or give them a shorter ttlSeconds.
The Stored values tab
Open Developer in the app and choose Stored values. The editor of a custom trigger links there too: See what your triggers have stored, under What you can use.
- Storage usage shows how many keys you use of your plan's keys, and how much of the large value storage.
- Key-value store lists every key with its size, when it changed and when it expires. Search by the start of a key, for example
track:. A value remembered byctx.trackis kept undertrack:, followed by the trigger's handle. - Large value storage lists what your code keeps with
ctx.storage. - The view button of a key shows its value. A long value shows its first 20,000 characters; the API returns the whole value.
- Delete a key when you no longer need it. A trigger that remembered this value starts over: the next event for that record is only remembered and does not fire.
From the API and MCP
With an API key you can read the keys and values (personal data in values is masked), set or delete a value and add to a counter: Developer API, MCP, GitHub and trigger simulation.
