import { workflow } from "@novu/framework";

workflow("my-workflow", async ({ step, payload, subscriber }) => {
  await step.inApp("send-in-app", async () => {
    return {
      body: "Hello there",
    };
  });
}, {
  payloadSchema: z.object({
    body: z.string(),
  }),
  name: "My Workflow",
  description: "This is a workflow",
  tags: ["business", "critical"],
  preferences: {
    channels: {
      inApp: { enabled: true }
    },
  },
});
import { workflow } from "@novu/framework";

workflow("my-workflow", async ({ step, payload, subscriber }) => {
  await step.inApp("send-in-app", async () => {
    return {
      body: "Hello there",
    };
  });
}, {
  payloadSchema: z.object({
    body: z.string(),
  }),
  name: "My Workflow",
  description: "This is a workflow",
  tags: ["business", "critical"],
  preferences: {
    channels: {
      inApp: { enabled: true }
    },
  },
});
import { workflow } from '@novu/framework';

workflow(
  workflowId: string,
  handler: WorkflowHandler,
  options?: WorkflowOptions
): WorkflowInstance;
workflowId
number
required

This id should be unique within your organization.

handler
(context: WorkflowContext) => Promise<void>
required

The definition function of the workflow.

options
WorkflowOptions

An optional options object for workflow level configurations

Workflow Context

This context is passed by the workflow engine to provide contextual information about current workflow execution.

subscriber
Subscriber
payload
InferProperties<payloadSchema>

The payload of the event that triggered the workflow, will be validated against the payloadSchema if provided.

step
object

The object that contains all the step functions, read more at Step Functions.

Workflow Channel Preferences

With Workflow channel preferences, you can control the default delivery preference for a channel and whether a subscriber can change it. Novu will show the subscriber preferences in <Inbox/> component. Subscribers can enable and disable any active channel in the workflow.

In the all object, you can specify default preferences for all channels. The enabled field on the all object is used as fallback value if a channel is not specified explicitly in channels.

The readOnly field controls whether subscribers can change the delivery preference for a channel. Critical workflows are defined with { readOnly: true }.

In the channels object, you can specify In-App, SMS, Email, Chat, and Push channel preferences. Each channel takes an object with an optional enabled flag that controls whether a notification delivery channel is enabled or disabled by default for subscribers.

Default values

By default, enabled is true for all channels. The readOnly flag is false.

These preferences can also be controlled from the Novu Dashboard per workflow. To do so, click on the cog icon at the top right of your screen, and then select the “Preferences” tab.

const newWorkflow = workflow(
  'default-preferences',
  async ({ step }) => {
    await step.inApp('send-in-app', () => ({
      body: 'Hello there',
    }));
  },
  {
    preferences: {
      {
        all: { enabled: true, readOnly: false },
        channels: {
          inApp: { enabled: true },
          email: { enabled: true },
          sms: { enabled: true },
          chat: { enabled: true },
          push: { enabled: true },
        },
      }
    },
  }
);