ElementsChannel

Overview

Reaching humans — declare email, SMS, WhatsApp, and push templates, send through fx.send, track receipts and consent.

Channel is how your backend reaches a person — the order-confirmation email, the SMS sign-in code, a WhatsApp notice, or a device push. You declare a template on a medium with per-locale {{field}} bodies, then send from a Flow with fx.send.

For developers wiring Mailpit locally and Resend / Taqnyat / FCM in production — templates and drivers first, never vendor SDKs inside do.

The one rule

Declare a template (channel.email(…).template({ catalog })), then fx.send(template, { to, data }). Bodies live on the declare (subject / text / html per locale). Consent and prior bounces suppress before any driver runs.

Around one fx.send

via: ["smtp", "resend"]
scenarioclean send
  1. 1Consent

    not suppressed — provider may be contacted

  2. 2Locale

    default:en → body en

  3. 3Drivers

    smtp — single attempt

  4. →

    Receipt — driver, ok/error, timestamp, message id

Smallest Example

Declare an email template

src/core/email.ts
import { channel } from "okengine";
import { z } from "zod";

const mail = channel.email({ from: "Shorter <shorter@localhost>" });

export const linkCreatedMail = mail.template("link-created", {
  description: "Short URL delivered to the demo inbox",
  locales: ["en"],
  schema: z.object({
    id: z.string(),
    code: z.string(),
    url: z.string(),
    shortUrl: z.string(),
  }),
  catalog: {
    en: {
      subject: "Short link {{code}}",
      text: "{{shortUrl}} → {{url}}",
      html: '<p><a href="{{shortUrl}}">{{code}}</a> → {{url}}</p>',
    },
  },
});

Import this module before oke() so auto-registry adopts the template (or pass it in oke({ channel: { templates: […] } })).

Send from a Flow

src/flows/links/signals.ts
import { on, flow, signal } from "okengine";
import { z } from "zod";
import { linkCreatedMail } from "@/core";

export const linkCreated = signal.once("link-created", {
  retries: 3,
  deadLetter: true,
  schema: z.object({
    id: z.string(),
    code: z.string(),
    url: z.string(),
    shortUrl: z.string(),
  }),
});

export const onCreated = on(
  linkCreated,
  flow("links.onCreated", {
    do: async (payload, fx) => {
      await fx.send(linkCreatedMail, {
        to: "you@localhost",
        data: {
          id: payload.id,
          code: payload.code,
          url: payload.url,
          shortUrl: payload.shortUrl,
        },
      });
    },
  }),
);

The compiler stamps sends: ["link-created"] on the Flow’s effects (template name — not email:link-created). Declaration and subscriber share signals.ts.

See it locally

With drivers.channel.email.dev: "smtp" and Mailpit pinned, open the Mailpit UI (MAILPIT_UI_URL). Subject and HTML come from the template catalog. Omit catalog and the runtime falls back to subject: link-created and text: JSON.stringify(data).

Progressive Patterns

From a bare send to catalog bodies, locale, and same-medium failover:

Template handle + recipient — catalog optional for local smoke tests:

await fx.send(linkCreatedMail, {
  to: "alice@example.com",
  data: {
    id: "lnk_1",
    code: "ok",
    url: "https://example.com",
    shortUrl: "http://localhost:6530/ok",
  },
});

Declaration Reference

DeclarationSignaturePurpose
channel.emailchannel.email(options?)Email medium binder
channel.smschannel.sms(options?)SMS medium binder
channel.whatsappchannel.whatsapp(options?)WhatsApp medium binder
channel.pushchannel.push(options?)Push medium binder
binder.templatebinder.template(name, options?)Auto-registered template
channel.templatechannel.template(name, options?)Medium-agnostic — not auto-registered

Medium options

OptionTypeDefaultMeaning
fromstring—Default sender (email From / SMS sender id)
senderstring—Alias stored as from

Template options

OptionTypeDefaultMeaning
descriptionstringnameConsole / docs label
localesstring[]—Declared locale tags for the template
schemaSchema—Payload shape (Zod / Standard Schema)
catalogobject—Per-locale subject / text / html
fromstringbinder’sOverride sender on agnostic channel.template only
mediummedium"email"Only on channel.template()

Empty name throws TypeError: channel.template: name is required.

oke({ channel.catalog }) overlays bodies after template catalogs (tests / plugin copy overrides). Manifest stays free of copy.

fx surface

MethodCapability / sendsMeaning
fx.send(template, opts?)template nameDeliver through the medium’s driver chain
fx.sendOtp(opts)"sms-otp"Provider-managed SMS OTP.
fx.verifyOtp(opts)"sms-otp"Check a provider OTP code
fx.deliverOtp(opts)"auth-otp"App-owned OTP across email / SMS / WhatsApp

fx.send options

OptionTypeMeaning
tostringRecipient (email / E.164 / FCM token / …)
dataobjectInterpolated into {{field}} catalog bodies
viarefsSame-medium driver order for failover
localestringExplicit locale (wins)
profileLocalestringProfile locale step
acceptLanguagestringRaw Accept-Language header

Dry-run records would have fired and never contacts a provider. Undeclared send → OKE1004 UNDECLARED_SEND.

Per-environment drivers

Email defaults from DRIVER_DEFAULTS.channel.email. SMS / WhatsApp / push are opt-in:

oke.config.ts
import { defineConfig } from "okengine/config";

export default defineConfig({
  drivers: {
    channel: {
      email: { dev: "smtp", test: "console", prod: "smtp" },
      // sms: { prod: "taqnyat" },
      // whatsapp: { prod: "wa-cloud" },
    },
  },
  images: {
    channel: { email: "axllent/mailpit:v1.31.2" },
  },
});
KeyDefault (dev / test / prod)Driver ids
channel.emailsmtp / console / smtpconsole · smtp · resend · sndr · taqnyat-mail
channel.smsnonetaqnyat · msegat · unifonic (console opens nothing)
channel.whatsappnonewa-cloud · taqnyat-whatsapp
channel.pushnone — not auto-boundPass oke({ channel: { drivers: […] } }) with webpush / fcm

Env knobs: Environment variables. Local SMTP catcher: Mailpit.

The Capabilities of Channel

Troubleshooting

Learn more

  • Email — drivers, catalog, Mailpit
  • SMS — fx.sendOtp / fx.verifyOtp
  • WhatsApp — channel.whatsapp + boot drivers
  • Push — FCM / Web Push binding
  • Receipts — ledger, outcomes, suppression
  • OTP plugin — /auth/otp/* over Channel
  • fx — fx.send options
  • i18n — Channel catalogs vs fx.t

Next

On this page