Workflows
Write durable multi-step workflows in an Executor app, start and inspect runs on the Workflows tab, and terminate active runs.
Workflows run durable, multi-step TypeScript jobs inside an Executor app with per-step checkpoints, retries, and sleeps. Declare them with workflow under workflows in defineApp, call step.do, step.runQuery, step.runMutation, step.sleep, and step.sleepUntil, and manage runs from app code (ctx.workflows) or the app’s Workflows tab (Start workflow, Terminate run). Workflows work on Local, Self-host, and Cloud.
The step that matters most: export workflows: { ... } from defineApp, deploy the app, and start a run from the Workflows tab or ctx.workflows.start.
Prerequisites: a deployed Executor app that defines at least one workflow and has any required accounts linked. On Self-host and Cloud, you need permission to use the app and access to its required accounts to start a run, and permission to manage the app to terminate an app-scoped run.
You’ll know it worked when… the run appears in All runs on the app’s Workflows tab, reaches Complete, and opening the row shows the expected output under Result.
How to use it
Workflow authoring is the same on Local, Self-host, and Cloud. On Local, runs execute in the local server’s workflow runtime for the single local user; on Self-host and Cloud, runs execute in the hosted runtime and enforce app, account, and ownership checks.
1. Define a workflow in app code
Use workflow({ input, output?, description? }, async (ctx, input) => result) and return it in workflows from defineApp:
- Step durability (
step.do): wrap external API calls or non-deterministic work inctx.step.do(name, options?, async (stepCtx) => value). Each step receivesstepCtx.app.accounts,stepCtx.user.accounts(when started by a caller),stepCtx.fetch,stepCtx.signal, andstepCtx.idempotencyKey. Configure retries with{ retries: { limit, delay, backoff }, timeout }, and throwNonRetryableErrorto fail immediately without retrying. - Database reads and writes: workflow bodies do not have
ctx.sql. Read data withctx.step.runQuery(name, queryFn, input, options?)and write data withctx.step.runMutation(name, mutationFn, input, options?). Mount operations used bystep.runQueryorstep.runMutationin the statictoolsrouter. Inside a workflow step mutation, write through the transaction’stxargument in one synchronousctx.sql.transaction((tx) => ...)callback and return the step mutation’s result from that callback. - Delays: pause without holding a worker open using
ctx.step.sleep(name, duration)orctx.step.sleepUntil(name, timestampOrDate)(accepts an epoch timestamp in milliseconds or aDate). - App-only vs caller-started: type the workflow for the context it receives—use
WorkflowContext<typeof requirements, "app">for runs started by an app-scoped mutation, orWorkflowContext<typeof requirements>for caller-started runs. The invocation sets the actual scope.
2. Start and manage runs from tools or the dashboard
- From app code: queries and mutations can call
ctx.workflows.get({ run })andctx.workflows.list({ workflow?, limit?, cursor? }). Mutations can also callctx.workflows.start({ workflow, input, key? })andctx.workflows.terminate({ run }). Within the same app, caller, and account scope, resubmit the samekey, workflow name, and input to get the existing run; reusing that key with a different workflow name or input fails withconflict. - From the dashboard: open the app and select the Workflows tab.
- Select a workflow in the sidebar to show Input schema, Input, and Start workflow (or stay on All runs to inspect runs you can access across every workflow).
- Paste JSON matching Input schema into Input and click Start workflow.
- Open a run row to check its status (
Queued,Running,Waiting,Paused,Pausing,Complete,Failed, orTerminated—a run without a sleep can complete without showingWaiting). Open a completed row for Result or a failed row for Failure reason. - Click Terminate run to stop an active run (on Self-host and Cloud, you can inspect and terminate your own runs while you still have access; terminating an app-scoped run requires permission to manage the app). Use Newer runs and Older runs to page through history.
Example
Save the three files below in a folder (for example ./workflow-demo). Set dependencies.apps in package.json to the version your host reports (executor apps framework on Local CLI):
package.json:
{
"name": "workflow-demo",
"private": true,
"type": "module",
"dependencies": {
"apps": "<framework-version>"
}
}
migrations/0001_items.sql:
CREATE TABLE items (name TEXT NOT NULL, summary TEXT NOT NULL);
index.ts:
import {
defineApp,
mutation,
type MutationContext,
number,
object,
query,
type QueryContext,
router,
string,
workflow,
type WorkflowContext,
} from "apps";
const requirements = {};
const recordItem = mutation(
{ input: object({ item: string(), summary: string() }) },
async (ctx: MutationContext<typeof requirements>, input) =>
ctx.sql.transaction((tx) => {
tx.exec(
"INSERT INTO items (name, summary) VALUES (?, ?)",
input.item,
input.summary,
);
return { saved: true };
}),
);
const processItem = workflow(
{
description: "Summarize an item and record the result",
input: object({ item: string() }),
output: object({ item: string(), saved: number() }),
},
async (ctx: WorkflowContext<typeof requirements>, input) => {
const summary = await ctx.step.do(
"build-summary",
{ retries: { limit: 2, delay: "5 seconds", backoff: "exponential" } },
async (stepCtx) => `Processed ${input.item} (${stepCtx.idempotencyKey})`,
);
await ctx.step.runMutation("save-item", recordItem, {
item: input.item,
summary,
});
return { item: input.item, saved: 1 };
},
);
export default defineApp(requirements, async () => ({
tools: router({
recordItem,
start: mutation(
{ input: object({ item: string(), key: string().optional() }) },
async (ctx: MutationContext<typeof requirements>, input) =>
ctx.workflows.start({
workflow: "processItem",
input: { item: input.item },
...(input.key ? { key: input.key } : {}),
}),
),
status: query(
{ input: object({ run: string() }) },
async (ctx: QueryContext<typeof requirements>, input) =>
ctx.workflows.get({ run: input.run }),
),
}),
workflows: { processItem },
}));
Deploy the folder using Author an app -> Deploying for your target. In Workflows, select processItem, enter {"item":"sample"}, and click Start workflow. Open the completed run to see {"item":"sample","saved":1} under Result.
Limits
- Names and payloads: workflow names and step names must be 1–200 characters. Workflow input JSON is capped at 1 MiB. Idempotency
keyvalues must be 1–128 characters.ctx.workflows.listacceptslimitfrom 1 to 100 (default 20). - No direct I/O in the workflow body:
ctx.sql, accounts, andelicitare unavailable directly in the workflow body; usestep.do,step.runQuery, andstep.runMutation. - Single synchronous SQL transaction per step mutation: workflow step mutations may open at most one synchronous
ctx.sql.transactionand cannot return a Promise from the transaction callback. - No event waits or interactive elicitation: workflows cannot wait for inbound webhooks or pause for durable
elicitprompts. Background operations that require live user input fail. - Version and account pinning: a workflow run keeps the app version and account IDs selected when it starts. Executor resolves current credentials for those accounts when it executes a step that can use them.