Configuration

Configuration

How the studio reads workflow.config.ts, and how workflow.stats.ts connects it to the engine's Postgres for live numbers through a read-only role.

The studio has two inputs. workflow.config.ts is your project: the same defineConfig({...}) object the compiler and the runtime read, so there is nothing studio-specific to declare. workflow.stats.ts is optional and belongs to the studio alone: it tells the studio where its numbers come from. Keeping them apart means the project describes what is deployed, and the stats file describes how one tool reads numbers about it.

workflow.config.ts

The studio loads the config's default export and compiles it with compileConfig, the same call that produces the bundle your runtime deploys. The lists you see are therefore exactly what would ship.

ts
import { defineConfig } from 'sendwend';
import { templates } from './src/templates';
import { onboarding } from './src/workflows/onboarding';
import { contract } from './src/workflows/schema';

export default defineConfig({
  title: 'Workflow Studio · Acme',
  contract,
  workflows: [onboarding],
  templates,
  email: { unsubscribeUrl: 'https://acme.io/unsubscribe' },
});
ts
// src/templates.ts: what the config registers; workflows send email.welcome
import { email } from './email'; // export const email = { welcome, ... }
import { push } from './push'; // export const push = { nudge, ... }

export const templates = { email, push };

What the studio takes from it:

FieldWhere it shows
titleThe browser tab, and the app name on push and chat previews
workflowsThe Workflows list and canvases, compiled to IR
segmentsThe Segments list, with each segment's compiled condition
templatesThe Templates list and previews, per channel registry
emailSettings, and the unsubscribe link above each email preview
runtimeThe time scale @sendwend/studio/postgres reads dates at (below)
database.schemaThe schema @sendwend/studio/postgres, generate and plan use (workflow by default)

Some rules follow from how the studio loads the file:

  • Nothing is scanned. The workflows, segments and templates are the ones registered in the config. Adding or removing one updates the studio over HMR.
  • The config runs on the dev server, never in the browser. The page receives compiled IR as data and imports only the template registries, because previews have to run template code. Each registry therefore has to be exported from a module of its own, not written inline in workflow.config.ts. A module that exports all registries as one object (export const templates = { email, push }, as above) works too: each channel is imported as a property of that export.
  • A config that does not compile shows the error instead of the views. An unregistered or cross-channel template, or a literal prop the template refuses, produces a compile error naming the workflow and node. So does a default export that is not defineConfig({...}), or a registry entry that is not an emailTemplate({...}) / pushTemplate({...}).
  • Email previews run inside the same EmailProvider a send does, so a layout that calls useEmailContext() works in the preview too. Each email template's own from, replyTo and unsubscribe link (its unsubscribeUrl, else the config's email.unsubscribeUrl) are shown above its preview.

--config <path> points the studio at a different file. In a monorepo that is how one studio serves an app that is not the current directory:

bash
pnpm exec sendwend dev --config apps/marketing/workflow.config.ts

workflow.stats.ts

Without a stats source the studio shows structure only: canvases without badges, a metrics tab that says no source is configured, segments without sizes. It never passes mock data off as real numbers.

A stats source is a module whose default export is defineStats({...}) from @sendwend/studio/config. The studio looks for workflow.stats.ts (or .tsx, .js) in the directory it is started from; --stats <path> names another file. Like the config, it runs on the dev server in Node, so it can query a database or an internal API with credentials from the environment that the page never sees. The page calls each function over HTTP (POST /__studio/stats/<method>), so every answer must be JSON.

Every function is optional. Each one feeds a specific part of the UI:

FunctionFeeds
reports()Home totals; the Workflows list columns and sparklines (entered, converted, delivered, opened, clicked)
nodeStats(workflow)The "users waiting here" badge on each canvas node
metrics(workflow, query)The four chart cards on a workflow's Overview tab (sent, open rate, click rate, conversion rate)
messages(workflow, range)The Message Metrics table: one row per message node
links(workflow, range)The most-clicked links panel
segments()Segment sizes and their recent history
profiles(segment, { limit, offset })The member table of one segment, a page at a time, and the profile drawer

The types (WorkflowReport, MetricPoint, MessageStat, LinkStat, SegmentReport, ProfilePage, …) are exported from @sendwend/studio/config, so a hand-written source is type-checked against what the UI expects. The SDK's example project ships one that returns deterministic made-up numbers, purely so the views have something to draw:

ts
import { defineStats } from '@sendwend/studio/config';
import { demoStats } from './src/demo-stats';

/** The studio's numbers: a deterministic stand-in for a real stats API (see src/demo-stats.ts). */
export default defineStats(demoStats);

A real project should not write one by hand if it runs the engine on Postgres. The engine already keeps the reporting.

Reading the engine's Postgres

A runtime on Postgres (journeyWorker with a Postgres store, or the self-hosted journeyNode) records every instance, message, delivery, open, click, unsubscribe and segment snapshot in its schema. @sendwend/studio/postgres turns that into a complete stats source:

ts
// workflow.stats.ts
import { postgresStats } from '@sendwend/studio/postgres';
import config from './workflow.config';

export default postgresStats({
  config,
  connectionString: process.env.STATS_DATABASE_URL!,
  environment: process.env.ENVIRONMENT ?? 'production',
});

It implements all seven functions. Its options:

OptionDefaultMeaning
configrequiredThe project: its database.schema, and the runtime entry's timeScale
connectionStringrequiredThe engine's database, as postgres connects to it
environmentproductionWhich runtime entry the data was written under
timeZonethis machine'sThe zone the studio's calendar days are cut in

environment and fast-forward. A runtime entry with a timeScale greater than 1 plays a journey day out in minutes, which is how you watch a week-long journey run locally. postgresStats reads every timestamp on the journey's clock, so an hour of local run reads as weeks of history, anchored on the latest activity in the data so last night's run still lands in "the last 30 days" this morning. Name the environment the database belongs to. The default, production, reads at scale 1; pass development (or whatever your fast-forwarded entry is called) to read a local run. A production database read as development would plot every journey day as minutes.

timeZone. The studio's date pickers are the viewer's calendar days, so buckets are cut in the same zone. Cut in UTC instead, "today" east of Greenwich would be an empty bucket on every chart.

Connections. It keeps one small pool (4 connections) per connection string, shared across reloads: the dev server re-evaluates the stats module on every config change, and a pool per evaluation would exhaust the database.

The site you are reading runs the SDK in production, and its stats file falls back to the local database string in wrangler.jsonc when STATS_DATABASE_URL is unset, and passes ENVIRONMENT and STATS_TIME_ZONE through:

ts
import { readFileSync } from 'node:fs';
import { postgresStats } from '@sendwend/studio/postgres';
import config from './workflow.config';

const env: Record<string, string | undefined> = process.env;

const connectionString =
  env.STATS_DATABASE_URL ??
  /"localConnectionString":\s*"([^"]+)"/.exec(
    readFileSync(new URL('wrangler.jsonc', import.meta.url), 'utf8')
  )?.[1];
if (connectionString === undefined)
  throw new Error('workflow.stats.ts: no database to read');

export default postgresStats({
  config,
  connectionString,
  ...(env.ENVIRONMENT === undefined ? {} : { environment: env.ENVIRONMENT }),
  ...(env.STATS_TIME_ZONE === undefined
    ? {}
    : { timeZone: env.STATS_TIME_ZONE }),
});

How the numbers are counted

The definitions matter when you compare them with a provider's dashboard:

  • Delivered is the transport's acceptance for email and Android, and the device's own report for iOS and web push. A push APNs accepted can still never arrive, so an app version without the reporting extension shows its iOS pushes as undelivered.
  • Opened and clicked are counted only on delivered messages, and on the chart against the bucket the message was delivered in, so a rate is a share of one cohort and stays within 100%.
  • Human and machine opens and clicks are split where the engine recorded the difference (mail privacy proxies prefetch pixels; security scanners follow links). The links panel counts human clicks on content links only: the unsubscribe link is not engagement.
  • Converted is counted against the day the user entered, not the day they converted, so converted / entered is the share of one cohort.
  • Users waiting on a node are running instances whose current node is that node.
  • A profile's created date is the user's first event: the engine knows nothing older about them.

A read-only database role

The studio is read-only, and postgresStats issues only select statements against the engine's schema. Give it a role that can do nothing else, so a mistake in a stats file, or a leaked STATS_DATABASE_URL, cannot write to production. This is plain Postgres, not a Sendwend API; adjust the schema name if your config sets database.schema:

sql
create role studio_reader login password '…';
grant usage on schema workflow to studio_reader;
grant select on all tables in schema workflow to studio_reader;

grant select on all tables covers the tables that exist when you run it. Tables a later SDK version adds (after a generate and a migration) need the grant again, or an alter default privileges in schema workflow grant select on tables to studio_reader run as the role that owns your migrations. The tables postgresStats reads are instance, message, message_click, message_link, segment, segment_member, segment_snapshot, profile and event, if you prefer to grant table by table.

profiles reads user properties (including email addresses) from the profile table, so treat a studio connected to production as you would any other tool with read access to customer data: run it locally, keep the connection string in the environment, and leave out the profile grant if the members view is not needed.

Next

  • Views: what each function's numbers look like on screen.
  • Postgres: the engine's schema and how a Postgres store is set up.