Workflows

Workflows

A workflow is an id, a trigger, a re-entry policy, an optional goal and exit condition, and a chain of steps. This page covers the definition and every way into a journey.

A workflow is one journey: what starts it, who may enter it again, when it counts as a success, and the steps a user goes through. workflow(definition) takes the configuration, and the chain after it holds the steps. The split is deliberate: options describe the journey as a whole, the chain describes what happens in order.

ts
// src/workflows/welcome.ts
import { not, performed, trigger, workflow } from 'sendwend';
import { e, u } from '../contract';
import { email } from '../email';

export const welcomeSeries = workflow({
  id: 'welcome_series',
  reentry: { mode: 'never' },
  name: 'Welcome series',
  description:
    'Greets a new user and sends a tip if they have not bought within two days.',
  trigger: trigger.event(e.login),
  goal: { condition: performed(e.purchase), within: '7 days' },
})
  .email(email.welcome, { name: u.name })
  .delay('2 days')
  .branch([
    not(performed(e.purchase)),
    (w) => w.email(email.tip, { name: u.name }),
  ]);

A workflow is registered in workflow.config.ts under workflows, together with the segments and custom actions it uses. The steps page covers every method of the chain.

The definition

OptionMeaning
idWire identity. Instances, the entry ledger and stats are keyed on it; never rename it for readability
triggerA trigger.xxx() value: how a user enters
reentryRequired: whether a user who already entered may enter again. See Re-entry
goalA condition, or { condition, within?, exitOnMatch? }. Counts conversions; exits on match by default
exitWhenLeave without counting as a conversion (unsubscribed, no longer eligible)
nameThe label Sendwend Studio shows. Metadata, outside the content hash
descriptionOne line on what the flow does. Metadata, outside the content hash

Identity and metadata

id is what the engine stores: an instance id is built from it, the entry ledger is keyed on it, and the reporting groups by it. Renaming it starts a different workflow as far as the engine is concerned. To change what people see, change name.

name and description are metadata. They land in the IR's meta field, which is excluded from the content hash, so editing them neither creates a new version that in-flight users are pinned to nor shows up as a change in plan() (it reports metadata_only). The same is true of a branch label, an exit reason and a filter's reason. See Versioning.

Goal and exit

The goal turns a journey into something you can measure. A conversion is recorded when the goal condition holds, and by default the user leaves the journey at that moment: a user who bought does not need the rest of a purchase nudge.

ts
workflow({
  id: 'checkout_recovery',
  reentry: { mode: 'after_exit', cooldown: '7 days' },
  trigger: trigger.event(e.begin_checkout),
  goal: { condition: performed(e.purchase), within: '3 days' },
  exitWhen: not(reachable('email')),
});
  • within is the attribution window: how long after entry a match still counts. Without it the window is unbounded.
  • exitOnMatch: false records the conversion and lets the journey carry on.
  • A performed() inside a goal counts only events after the user entered, so history from before entry cannot satisfy it.
  • exitWhen ends the journey without a conversion. It is deliberately not anchored at entry: "the user unsubscribed" should end the journey whenever it happened.

Both are checked when the journey starts and again after every step where time passes (delay, timeWindow, waitUntil), the moments when the user's state may have moved. Between two steps that run back to back nothing meaningful changes, so checking there would only spend steps.

Triggers

ts
trigger.event(e.login);
trigger.event(e.purchase_failed, (p) =>
  notInArray(p.code, ['CHECKOUT_CANCELED'])
);
trigger.event(e.sign_up, { filter: exists(u.email) });
trigger.enters(dormant7d);
trigger.date('2026-12-25 09:00:00', { filter: purchaser });
trigger.webhook();

A trigger is a plain value: define it once and pass it to as many workflows as you like. The event name and payload type come from the e.xxx reference, so the factory needs no generics.

Event triggers

trigger.event(e.x) starts the journey when the event is ingested. It takes two independent gates, and the user enters only when the event fired and both hold:

  • A payload where, the optional second argument: an arrow over the typed payload fields, using the ordinary conditions. (p) => gte(p.value, 100) only lets large orders in.
  • A profile filter, in the options: a condition on the user, such as exists(u.email) or a segment.
ts
trigger.event(e.purchase, (p) => gte(p.value, 100), {
  filter: exists(u.email),
});

The filter is checked before an instance exists. A user it turns away has not entered: no instance, nothing held against the re-entry policy, nothing in the entry counts. That is the difference from a filter step at the top of the chain, which runs inside an instance that has already entered.

An event trigger ignores visitors (people who have not signed in) unless it says otherwise with { visitors: true }. Such a journey can only reach them by push, since a visitor has no address, and an entry made as a visitor counts as the user's once they sign in.

Each occurrence of the event is one entry attempt. The same event redelivered (the same id) is the same occurrence and enters at most once, in every re-entry mode.

Reading the triggering event

trigger.event() returns a handle on the event that started the journey. Its payload is a table of that event's fields, typed by the event, and a field can go anywhere a profile property can: a condition, a message prop, an emitted payload, an action argument.

ts
const checkout = trigger.event(e.begin_checkout);

workflow({
  id: 'checkout_recovery',
  reentry: { mode: 'never' },
  trigger: checkout,
})
  .delay('1 hour')
  .branch(
    [gte(checkout.payload.value, 100), (w) => w.email(email.vip_offer)],
    (w) => w.email(email.reminder)
  )
  .email(email.cart_summary, {
    total: checkout.payload.value,
    currency: checkout.payload.currency,
  });
  • It is a snapshot. The journey reads the payload it entered with for as long as it runs. A second begin_checkout a day later changes nothing a running journey reads.
  • Scalars only. A nested field is reached by its path (checkout.payload.cart.items); an object or an array is not a value and reads as missing. A field the event left out reads as missing too, like an unset profile property.
  • Only in the workflow that trigger starts. compileBundle refuses a reference taken from another trigger, one in a workflow with a segment, date or webhook trigger (they carry no payload), and one in a segment or a trigger filter (neither runs inside a journey; test the event with the trigger's where). With a contract, compileConfig also checks that every field read is declared for the event.

To act on an event that arrives during the journey, use performed(e.purchase, (p) => gte(p.value, 100)) in a waitUntil or a branch. That tests the event log, not the trigger.

Segment entry

trigger.enters(segment) starts the journey when a user goes from not matching a named segment to matching it. Membership is re-evaluated on the user's own events and profile updates, and by the segment sweep (the host's cron), which is what catches a segment entered by the clock alone, such as "has not logged in for 7 days".

ts
export const dormant7d = segment({
  id: 'dormant_7d',
  condition: and(
    performed(e.login),
    not(performed(e.login, { within: '7 days' }))
  ),
});

workflow({
  id: 'winback',
  reentry: { mode: 'after_exit' },
  trigger: trigger.enters(dormant7d),
}).email(email.we_miss_you, { name: u.name });

Only a named segment() is accepted, because entry needs a membership table and an anonymous condition has none. Whoever already matches when the trigger is first deployed is recorded as a member and starts nothing: entering a segment is not being in it. Conditions and segments explains the rule and why a deploy never starts journeys in bulk. Each fresh qualification (left the segment, matched again) is one entry attempt under the re-entry policy. Visitors are not swept and never enter segment triggers.

trigger.segment is the earlier name of trigger.enters and is still accepted.

A send at a time

trigger.date(at, { filter }) starts the journey once, at a time, for every known user the filter holds for when that time comes. The time is written 'YYYY-MM-DD HH:mm:ss' in UTC; anything else throws when the workflow is built.

ts
workflow({
  id: 'black_friday',
  reentry: { mode: 'never' },
  trigger: trigger.date('2026-11-27 09:00:00', { filter: purchaser }),
}).email(email.black_friday, { name: u.name });

The filter is asked of each user when the sweep reaches them, not read from a membership kept beforehand, so someone who has been a purchaser for three years enters like one who became one yesterday. Without a filter every known user enters. Visitors do not.

  • The sweep does the starting. The send begins at the first sweep at or after the time, so a 15-minute cron starts it within 15 minutes. It walks the audience 500 users at a time and keeps its place in the store: a sweep spends about 30 seconds on it, the next carries on, and one that is cut off loses nobody and repeats nobody. A large audience takes several sweeps; run the cron more often around a big send if that matters.
  • A time runs once. The run is identified by the workflow and the time. Redeploying does not repeat it, and a deploy that lands just after the time does not cancel it. Changing the time is another run, and who enters again is the workflow's reentry policy.
  • A time already past when the trigger is first deployed is not run, and neither is one no sweep picked up within a day (a cron that was off). A send that late is not the one that was scheduled.
  • A start that fails is retried. The page it was on is done again by the next sweep, skipping whoever already entered; a user who still cannot be started a day after the time is passed over.
  • The filter is checked before an instance exists, as with an event trigger's filter.
  • The journey's entry time is the trigger's, and the trigger carries no payload.

Webhook triggers

trigger.webhook() compiles and deploys, but the runtime does not route it yet: a deploy lists such workflows as unrouted. Use an event trigger, with your own endpoint calling ingest, until it does.

Composing workflows

Workflows connect through events. A step can emit an event with .sendEvent(), and that event can trigger another workflow, so the call graph between journeys is visible in code. There is no goto: looping goes through re-entry, under the workflow's policy.

Repeated sequences of steps are shared with flow() fragments, and repeated audiences with segments.

  • Steps: everything the chain can do.
  • Re-entry: the required reentry option.
  • Examples: complete workflows drawn as canvases.