Monorepo
How to lay out a workspace where an api sends events and a separate engine app runs the journeys, with one shared contract, one generated schema and one version of Sendwend.
This page is for a team that already has a backend in a pnpm (or similar) workspace and wants to add Sendwend next to it. At the end you will have three pieces that each have one job: a contract package both sides import, an engine app that runs the journeys, and an api that reports what users do. The layout described here is the one this website's own repository uses in production, generalized.
The layout
The split follows who needs what. The api needs the event and profile types, not React templates or a database client. The engine app needs everything. The database package owns migrations for the whole workspace, the engine's tables included.
The contract package
The contract is the agreement between producer and engine: the events journeys react to, the profile fields they read, and the subscription topics. Put it in a package that holds schemas only, so the api can import it without pulling in templates, React or a database driver.
Expose it as its own subpath ("./workflow": "./src/workflow.ts" in the package's exports) and list sendwend and zod as its dependencies. Two habits from the reference repo are worth copying:
- Events only the server may send. An account's first sign-in is known only to the api, so the repo keeps a
serverEventslist next to the contract (['sign_up'] as const satisfies readonly (typeof contract.eventNames)[number][]). The api never forwards those names from client analytics, so a client cannot start the onboarding journeys by callingtrack('sign_up'). - Contract changes are additive. Add an event, or an optional field on a payload or the profile. Removing or narrowing something is two releases: first stop the api sending the old shape, then remove it from the contract. The engine validates every inbound batch against its own copy of the contract, and the profile is a strict object, so a field the old api still sends and the new contract dropped is refused.
The engine app
apps/workflow holds everything the engine runs. workflow.config.ts is the one registration point that the compiler, the host and the studio all read:
src/workflows/index.tsexports the journeys as an array in registration order, so adding a journey is one new file and one line.src/workflows/schema.tsre-exportscontract,eandufrom the contract package, so journey files import from one local module.- Each channel has a registry module (
src/templates/email/index.tsexportingexport const email = { welcome, tip }), andsrc/templates/index.tsgathers them astemplates = { email, push }. The studio imports the registries in the browser to render previews, so each one must be exported from a module of its own rather than written inline in the config. See templates and channels. src/index.tsis the host entry. Its shape is on the Cloudflare page; the AWS Lambda and self-hosted entries read the same config.src/env.tsis a zod schema the host parses on first run (journeyWorker({ env })), so a missing or malformed secret fails at startup with every problem listed, not at the send that needs it:
tests/snapshotscompileConfig(config), so a change that alters a journey's IR shows up in review. See testing.
The engine app's package.json carries the scripts the rest of this page refers to:
The api sends events
The api owns one module that builds the clients, typed by the shared contract:
The reference repo splits traffic between the two:
- Profile and events go through
JourneyQueue. Sign-ins, sign-ups and forwarded client events are queued as one batch each. Onceingestresolves the batch is durable, so a Worker outage loses nothing and the request waits only for the queue write. - Devices and push opens go direct through
JourneyClient, after the response (waitUntil), never throwing. Their order matters more than a single delivery, and the app registers its push token again on every launch anyway. - The
fetchoption carries a timeout, so a hung engine cannot hold an account deletion or awaitUntilopen.
Profile fields travel in the same batch as the event that needs them, so a journey never starts without its recipient. The full caller API is on Sending events.
The generated database schema
The engine's tables live in their own Postgres schema (workflow unless database: { schema } says otherwise) and the host never creates or alters them at runtime. In a monorepo with drizzle, the engine app writes them as one drizzle file into the database package, and that package's migrations apply them with everything else:
Re-export the generated file from the database package's index and include the schema in drizzle-kit's schemaFilter (['application', 'workflow'] in the reference repo). Run db:schema:check in CI: it writes nothing and fails when the file no longer matches the installed SDK, which is how an SDK upgrade that changed the tables is caught before it ships. The file is laid out the way a prettier-compatible formatter at 100 columns lays it out, so formatting it changes nothing. Details are on Postgres.
Check the plan, then deploy
A running journey is pinned to the workflow IR it entered on, and that IR names templates, segments and actions by id (versioning). On Cloudflare a new version's code serves the moment it is published, so the host's own refusal comes too late. Run sendwend plan against the production database before wrangler deploy:
It compiles the project, reads which versions journeys are still running on, and exits non-zero if the release would drop a segment or action they name, remove a template or move it to another channel, or add a required prop the pinned props never supplied. It also prints what the database still lacks.
The reference repo's release job runs in this order:
db:schema:check, type checks and tests.planagainst production, then migrations.wrangler deployof the engine, then the api, then the web app. Consumers go before producers: the new engine accepts what the old api sends and what the new api will send, as long as contract changes are additive.
With version_metadata in wrangler.jsonc, the new Worker version makes its own bundle live on its first request or cron run, so there is no deploy call to make afterwards.
Keep one version of sendwend
The contract package and the engine app must resolve the same copy of sendwend. With two copies, the contract's types stop matching the Worker's, and the errors read as if the contract were wrong. Two settings in pnpm-workspace.yaml prevent it:
- The catalog. Every package that depends on
sendwendwrites"sendwend": "catalog:", so the version is declared once and an upgrade is a one-line change. Ranges repeated by hand drift and install two copies. - The react override.
sendwendtakesreactandreact-domas optional peers. The contract package renders nothing, so it declares neither, and pnpm resolved those peers for it to the latest React on npm, which differed from the version the engine app pinned. pnpm then installed a secondsendwendfor the contract package, keyed by the different peer set. Pinning React for the whole workspace withoverrideskeeps one peer set and therefore onesendwend.
If type errors appear between e.xxx from the contract and the workflow builder after an install, run pnpm why sendwend and look for two versions or two peer suffixes.
Where the studio runs
@sendwend/studio is a dev dependency of the engine app, and pnpm studio starts it from that directory on port 4321. It reads workflow.config.ts on its own dev server and compiles it with compileConfig, so what it shows is exactly what would ship. It never changes the project and never calls the engine's HTTP API.
Numbers come from workflow.stats.ts, which runs in Node on the studio's server and reads the engine's reporting tables directly:
environment names the runtime entry the data was written under, so a local database filled in fast-forward is read on the journey's clock. Point it at production with a read-only connection string to see live numbers. More on the studio overview and configuration.
Pitfalls
- One database schema per engine. The routing tables are global. Two engines on one schema, such as staging and production sharing a database, swap each other's routes on every deploy. Give each its own
database: { schema }or its own database. - Migrate before the new version serves.
POST /bundle/deployand a version applying its own bundle both refuse to run until the tables match, but a version already serving does not wait for a column it reads. - A queue is not a reason to skip ordering. A batch the engine's contract refuses is retried by the queue consumer until the engine catches up, then lands in the dead letter queue. A direct
POST /ingestanswers 400, and a producer that only logs it loses the event. Deploy the engine first either way. - The contract package has no side effects. Keep React, database clients and environment reads out of it, or every importer pays for them.