Skip to main content

What are the effects?

Effects are the business logic written in TypeScript that is executed by Trace API after an action is submitted. The effects declared in the configuration project are stringified functions that are stored in the database, so they can be interpreted at runtime by Trace API.
No external constants or functions can be used in the effects.

Workflow definitions

The dsl.$definitions object is the read-only storage of the workflow. It can be used to store constants, environment variables, workflow ids and stringified functions that will be accessible in the effects. The definitions can be of any shape, but by convention, it’s shape is the following:
object
The repository of the workflow where workflow constants are stored.
object
An object where each key is the name of the function and the value is the stringified function.
object
A map between the workflow label and its id.
object
A map between the environment variable name and its value.

Notifications

Notifications (usually emails) are sent to a user (could be a single user, or a group) when performing an action. They are sent when the state.notifications is updated. Thus, notifications are usually built and sent inside the effects of an action. There is 3 steps to send a notification through configuration :
  1. Create a function inside workflowDefinitions to build the template of the email, and manipulate data derived from the state (because no external constants or functions can be used in the effects.)
create a custom function in the workflow /definitions/functions
  1. Inside the effects, import this function.
Usage in effects
  1. Updating state.notifications by pushing the template object with the correct data.
Update state
To see more details about it, go to the notification page.

The Stratumn constant

You can use the Stratumn constant to get access to Sentry, a Pino logger and the StratumnError class. Unlike other variables, this constant does not need to be created or imported, it is accessible globally in the effects.
The Sentry SDK, allowing you to capture errors, wrap functions with spans, add context, set tags, add metrics etc.This variable is the same as if you did import * as Sentry from "@sentry/node"; in your code.
Usage your effect function
A Pino logger instance, allowing you to log messages with different levels.
Usage your effect function
The Stratumn Error class, allowing you to create custom errors with a code and a message.
string
required
The name of the error.
string
required
The code of the error.
string literal
required
The status of the error.
string
required
The message of the error.
object
required
The context of the error. It can be used to add additional data to the error.

Batch context

When a user applies the same action to many traces at once (a batch action), each trace still runs the action’s effects on its own. The dsl.$variables.batch object lets an effect know that it is running as part of a batch, and where it sits in that batch. dsl.$variables.batch is always present. Outside a batch it holds the not-in-batch default, so you never have to guard against it being undefined.
Requires @stratumn/dsl 1.24.0 or above.
boolean
required
true when the action is executing as part of a batch action, false otherwise.
This is the discriminant: the two fields below are only reachable once you have narrowed on it.
number
The 1-based position of the current trace within the batch (1 to totalActionInBatch).
Only available when isBatchAction is true.
number
The total number of traces the batch will act upon.
Only available when isBatchAction is true.
batch is a discriminated union on isBatchAction. You must narrow on the flag before reading actionPositionInBatch or totalActionInBatch — reading them directly is a compile error:
Only actions executed by the batch engine receive a batch context. Every other execution path — a single action submitted from the UI, an automated action, a link created from another effect via dsl.$modules.createLink, or the public API — sees the not-in-batch default.

Ordering is not guaranteed

actionPositionInBatch identifies which trace you are on, not when it runs. The batch engine processes traces in parallel chunks, so positions do not complete in order: position 250 may well finish before position 3.
Do not use actionPositionInBatch === totalActionInBatch as an “end of the batch” hook to fire a summary email or a final computation. It only means “the numerically last trace”, not “the last one to finish” — and if that trace fails, the effect never runs at all. The platform already sends a single digest notification once the whole batch settles, so per-trace effects don’t need to reproduce it.
A safe use of the batch context is to make a per-trace effect lighter or better labelled when it runs at scale — for example, skipping a per-trace notification that would otherwise be sent hundreds of times:
Skip per-trace notifications when running in a batch

External functions

Some functions are declared directly in Trace API, and can be accessed direcly in the effects using dsl.$modules.
An asynchronous function that searches for traces, allowing optional filters to be applied based on the traces’ data.
string
required
The id of the workflow to search traces in.
JSONDeepFilterValue[]
The filters to apply to the search.
An asynchronous function used to create a new scheduler.
The scheduler can be for example used to create links at a specific interval.
string
required
The unique name of the scheduler.
object
The data that will be used by the scheduler.
string
required
The cron expression that defines the schedule.
string
required
Any string to identify the creator of the scheduler.
An asynchronous function that stops a scheduler using its id.
string
required
The id of the scheduler to stop.
Exposes the moment function from the moment library (v2.29.1).
See the moment documentation for more information.
Example
Exposes z from the zod library (v3.24.0).
See the zod documentation for more information.
Example
An asynchronous function that generates a PDF or a DOCX, PPTX, Excel based on a template and provided data.
string
required
The name of the generated PDF file.
string
required
The key of the PDF template to use, as defined in the workflow configuration.
string
required
The id of the trace where the PDF will be stored.
PdfFieldData[] | Record<string, unknown>
required
The data to fill the PDF template with.
'pdf-lib' | 'docx-templater'
required
The engine to use for PDF generation.

Example - Order a meal

Let’s imagine a restaurant workflow where a client can order a meal. The workflow has two groups: cook and client. In the first action, the client will select a meal to order and submit the form. The goal of the effect is to:
  • store the selected meal in the trace’s data,
  • add the 'Order received' status to the trace with a progress of 20%,
  • and update the next available actions for each group:
  • the cook group should be able to do the prepareOrder action and comment,
  • the client group should only be able to do the comment action.
orderMealEffect