UNPKG

@softr/sdk

Version:

The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.

119 lines (88 loc) • 5.31 kB
--- name: workflows description: >- Trigger a Softr workflow from a server function: defineWorkflow from @softr/sdk declares the action with its workflowId, triggerId, permissions, and optional hardcoded body; workflow.trigger from @softr/sdk/server fires it with an optional flat runtime payload and resolves to void when the workflow finishes. metadata: type: sub-skill library: softr-sdk library_version: 1.19.0 --- **Boundary.** `workflow` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineWorkflow` and every type come from `@softr/sdk` and can be imported anywhere. # Triggering workflows `workflow.trigger` fires a Softr workflow and waits for it to finish. It resolves to `void`. ## Setup ```ts import { createServerFn } from '@tanstack/react-start' import { defineWorkflow } from '@softr/sdk' import { workflow } from '@softr/sdk/server' ``` ## Declare the workflow action Every trigger needs a workflow action: which workflow, which trigger step, who may run it, and any data that is always sent. The workflow must have a **Run custom workflow** trigger — no other trigger type can be fired from app code. ```ts const sendWelcomeEmail = defineWorkflow({ id: 'sendWelcomeEmail', workflowId: 'c9d8e7f6-a5b4-4c3d-8e2f-1a0b9c8d7e6f', triggerId: '926da496-749f-4006-999c-197d8a556122', permissions: { userGroupTypes: ['LOGGED_IN_USERS'] }, body: { email: '{LOGGED_IN_USER:EMAIL}', // resolved server-side from the signed-in user name: '{LOGGED_IN_USER:NAME}', plan: 'enterprise', // any hardcoded value — always sent, never overridden at runtime }, }) ``` - `id` is a name you choose, unique across the app. - `workflowId` and `triggerId` are opaque UUIDs identifying the workflow and its **Run custom workflow** trigger. `workflow_list` returns both. Pass them as inline string literals — never derive them at runtime. - `permissions` declares who may run the trigger. Uses the same format as queries and mutations. - `body` is hardcoded data the server resolves before calling the workflow. It is **not** passed by the caller at runtime — the workflow always receives it. Template values draw from the signed-in user and the current time (see table below). Write the object inline in `defineWorkflow`. Passing it through a variable widens the types. ## Template values in `body` These placeholders are resolved server-side when the workflow runs: | Value | Resolves to | | --- | --- | | `{LOGGED_IN_USER:EMAIL}` | Signed-in user's email | | `{LOGGED_IN_USER:NAME}` | Signed-in user's name | | `{LOGGED_IN_USER:RECORD_ID}` | Signed-in user's record id | | `{LOGGED_IN_USER:<field>}` | Any other field from the user record | | `{CURRENT_DATE_TIME}` | Current date and time | | `{CURRENT_DATE}` | Current date | ## Core pattern ```ts const runWorkflow = createServerFn({ method: 'POST' }) .validator(z.object({ message: z.string() })) .handler(async ({ data }) => { await workflow.trigger(sendWelcomeEmail, { message: data.message }) }) ``` `workflow.trigger` takes the action object and an optional flat runtime payload. The payload only accepts primitive values — `string`, `number`, `boolean`, `null`. Use the action's `body` for anything that is always the same or comes from the user session. `workflow.trigger` resolves only when the workflow has finished. ## Triggering without a runtime payload ```ts const run = createServerFn({ method: 'POST' }).handler(async () => { await workflow.trigger(sendWelcomeEmail) }) ``` Omit the second argument when the action's `body` already carries everything the workflow needs. ## Failure modes **Calling `workflow.trigger` from a component.** `workflow` lives on `@softr/sdk/server`. Wrap it in a `createServerFn`. ```ts // Wrong: calling from a click handler <button onClick={() => workflow.trigger(sendWelcomeEmail)}>Run</button> // Correct: server function, called from the button const run = createServerFn({ method: 'POST' }).handler(() => workflow.trigger(sendWelcomeEmail), ) // … const submit = useServerFn(run) <button onClick={() => submit()}>Run</button> ``` **Computing `workflowId` or `triggerId` at runtime.** Both are static UUIDs from the workflow definition. Derive them from user input and the call fails at request time. **Pointing at a workflow with no Run custom workflow trigger.** A workflow that runs on a schedule, a table event or an integration event has no trigger the app can call, and `workflow.trigger` fails at request time. Confirm the workflow has one before writing the `defineWorkflow`. If it does not, say which trigger it has instead — never that the workflow is missing — and name the ones that do have a Run custom workflow trigger. **Passing nested objects in the runtime payload.** The second argument to `workflow.trigger` only accepts flat values — `string`, `number`, `boolean`, or `null`. Put anything that needs server-side resolution in the action's `body` instead. **Not awaiting the result.** `workflow.trigger` is async. Fire-and-forget loses the result and swallows any error. ## References - [current-user/SKILL.md](../current-user/SKILL.md): reading the visitor off `context` to include them in the payload