Installation
Install the SDK, write a contract, a template and a workflow, run them locally against Postgres in fast-forward, and deploy the Worker to Cloudflare.
This page takes a new project from pnpm add sendwend zod to a journey running on your machine, then deployed as a Cloudflare Worker. The files below follow the SDK's examples/cloudflare-demo, trimmed to what this journey uses. For AWS Lambda or a plain Node process, the first five steps are identical; see AWS Lambda and Self-hosting for the host.
A project has four parts: one contract, a set of templates, the workflows, and one workflow.config.ts that registers all of it. The compiler, the Worker and Sendwend Studio all read that one object.
1. Install the packages
The peer dependencies are optional. Install the ones the subpaths you use need:
| Peer | Needed by |
|---|---|
react, react-dom | sendwend/react-email, and sendwend/cloudflare, which uses it |
postgres | sendwend/postgres-js |
@aws/durable-execution-sdk-js, @aws-sdk/client-lambda | sendwend/aws only |
For the Cloudflare host in this guide, with emails written in React Email:
The Worker's JourneyRunner is typed against a small ambient declaration of cloudflare:workers that ships with the package. Add it to tsconfig.json (the SDK avoids @cloudflare/workers-types because its globals clash with React's DOM types):
2. Write the contract
The contract declares the events journeys react to and the profile fields they read, as Zod schemas. Put it in a module both your API and the Worker can import. See Contract for the rules.
Event payloads are object schemas (looseObject keeps extra analytics fields). The profile is a flat object of string, number, boolean, enum and literal fields. Defaults, transforms and refinements are refused when the contract is defined, because input and output must mean the same JSON.
3. Add templates
Each template is one object with a stable id, a strict props schema, and either transactional: true or a topic the contract declares.
Gather them per channel. The workflows send the definitions; the config registers the whole.
See Templates and channels for push, Slack and Discord.
4. Write a workflow
id is wire identity: instances, the entry ledger and stats are keyed on it, so rename name instead. reentry is required; see Re-entry.
5. Register it in workflow.config.ts
The ENVIRONMENT variable picks the runtime entry (production when unset). timeScale: 600 turns "1 day" into 2.4 minutes, so the two-day delay above takes 4.8 minutes locally. Time windows are times of day and cannot be compressed, so a scaled journey skips them. A mode with no entry runs in real time without message logs; a production entry, if you write one, only admits timeScale: 1. The unsubscribeUrl is the link the tip email carries, because its topic lets the user opt out; the transactional welcome has none.
Check the project now: pnpm exec sendwend dev serves the canvas and the template previews at http://localhost:4321, or shows the compile error that names the workflow and node.
6. Write the Worker
To send through Amazon SES instead, pass sesEmailTransport({ region, credentials }) from sendwend/transports/ses, with credentials from the Worker's env.
| Entry | Why |
|---|---|
nodejs_compat | The bearer check uses node:crypto's timingSafeEqual |
workflows | One binding runs every journey: the IR is data, so there is one runner class |
hyperdrive | The engine's Postgres; localConnectionString is what wrangler dev connects to |
version_metadata | Each new version makes the bundle compiled into it live by itself |
send_email | Cloudflare Email, for cloudflareEmailTransport |
triggers.crons | The sweep: clock-only segments, date triggers and upkeep. A 15-minute cron starts a date send within 15 minutes |
For local runs, override the mode in .dev.vars (keep it out of git):
A host in development runs open, without API_TOKEN, and in fast-forward. Any mode that is not development, test or one with its own runtime entry is held to production's rule: no API_TOKEN, no service.
7. Start a local Postgres
The engine's tables live in one schema, workflow by default. Postgres 15 or later is required (the opt-out table, suppression, uses UNIQUE NULLS NOT DISTINCT).
Nothing creates tables at runtime. This script, from the demo, creates them with renderPostgresDdl(). Every statement is CREATE ... IF NOT EXISTS, so it is safe to run again:
If your app already migrates with drizzle, use pnpm exec sendwend generate --out <file> instead: it writes the same tables as one drizzle file for drizzle-kit to migrate. See Postgres.
8. Run it locally
Check that the Worker is up and the journeys are deployed:
With version_metadata, a version applies its own bundle on its first request; POST /bundle/deploy applies it on demand and answers 503 with every missing table or column if the database is not ready.
9. Send the first event
POST /ingest takes the profile and the events it belongs to as one batch: the profile is merged first, so the journey the event starts always finds its recipient.
The id is the occurrence's identity for that user: send the same one twice and it is recorded once. With a contract, a batch that does not match it is refused with a 400 and the field paths, before anything is stored.
10. Check that it works
- The send. With
logMessages: true, thewrangler devoutput logs each outbound message with its channel, template, recipient and resolved props. Local Cloudflare Email only simulates: the rendered HTML lands in.wrangler/tmp/email/and the path is logged. - The delay. At
timeScale: 600, the tip goes out 4.8 minutes after the welcome. Send apurchaseforu_1before then and the goal ends the journey instead. - The preview.
GET /preview/welcome?props={"name":"Ada"}renders a template as the Worker would send it. - The data.
psql postgres://postgres:pw@localhost:54320/workflowand look at theworkflowschema: the entry, the instance and the message copy are rows.
11. Deploy to Cloudflare
Create the production database (any Postgres 15+), create its tables, and put Hyperdrive in front of it with query caching off. The engine reads wake subscriptions, profiles and segment membership right after writing them, and a cached answer would miss wakes.
Put the returned id in wrangler.jsonc, then set the bearer and deploy:
sendwend plan compiles the project, reads which workflow versions journeys are still running on, and exits non-zero if the new code would break one. Put it in CI ahead of wrangler deploy: on Cloudflare a published version serves at once. The new version then makes its own bundle live on its first request or cron run.
Send events from the service that knows what users do, with JourneyClient:
With a contract, event names and payloads are typed and checked before anything is sent. See Sending events for visitors, devices and the queue, and Cloudflare for the full deployment notes: the email transport, List-Unsubscribe over https, Workflows limits and rollbacks.
Next
Basic usage walks through the everyday loop: a new event, a segment, a journey with a wait and a branch, a plan and a deploy.