Serving flows
A flow is a self-contained system in your app, with actions that clients call. Serving flows is the step that makes them callable. One config object lists your flows, says where the app keeps its data and which models it uses, and gives you back a running app.
The example is a support desk with three flows. support has a reply action that a page calls. billing is started by Stripe. digest is started by a schedule.
One object
import { createFlowState } from "@flow-state-dev/engine";
import { sqliteStores } from "@flow-state-dev/store-sqlite";
import support from "./src/flows/support/flow";
import billing from "./src/flows/billing/flow";
import digest from "./src/flows/digest/flow";
export default createFlowState({
flows: [support, billing, digest],
models: { default: "openai/gpt-6-luna" },
stores: sqliteStores({ filename: "support.db" }),
});
flows is a list. Each is served under its own id, the kind you gave it in defineFlow.
models names the default model. Leave it out and Flow State finds providers from environment variables such as OPENAI_API_KEY. stores says where the app keeps what it saves: sessions, state, requests. Leave it out and everything lives in memory and is gone on restart. Every host and every flow uses the models and stores set here.
Keep this file apart from your route handler. The route imports it, and the fsdev command loads its default export. Every other option is in Runtime options.
What it serves
Every flow gets HTTP routes under /api/flows, with the flow's id next: /api/flows/support/.... Calling reply is a POST to /api/flows/support/actions/reply. HTTP API has the responses and the rest of the routes.
Who may call them is decided by a principal resolver, authentication.resolvePrincipal on the flow or resolvePrincipal on the app, which turns a request's credentials into the caller. With no resolver, a call takes its userId from the request body, which is for local development only. Authentication covers what to set before you expose a flow.
Where it runs
The flows do not change with the host. What changes is the file that mounts the app. On Next.js that is a route file, shown in Add to an existing app. On a plain Node process it is one call.
import { serve } from "@flow-state-dev/node";
import flowstate from "../fsdev.config";
await serve(flowstate, { host: "127.0.0.1" });
serve starts a long-lived server for the app. It runs the same check as fsdev serve, which does this from the terminal using fsdev.config.ts: on any host but loopback, it refuses to start while a flow has no resolver, and refuses calls to a flow registered later without one. allowUnauthenticated: true turns the check off. With no host, serve binds every network interface, so the sample binds 127.0.0.1 until a resolver is set. Runtime options has the port, health check and shutdown behaviour.
Other ways in
HTTP is the built-in door. From a terminal, fsdev run support reply -i '{"message":"hi"}' calls the action directly, with the stores and models from fsdev.config.ts.
MCP and webhooks are adapters you add to createFlowState with adapters. Each one hands the app the same kind of request. Schedules need no adapter.
MCP. The Model Context Protocol is how clients such as Claude Desktop use outside tools. With the MCP adapter, the actions of support become tools, and an MCP call is an action call. See MCP server.
A webhook. Stripe posting to your app is not an action call. It posts to a webhook route, POST /api/flows/billing/webhooks/stripe, and no action name is involved. The flow says which block runs for which event, where a block is a unit of work.
import { defineFlow, defineWebhookBinding } from "@flow-state-dev/core";
import { recordPayment } from "./blocks/record-payment";
export default defineFlow({
kind: "billing",
actions: {},
authentication: { defaultUserId: "system", requireUser: false },
webhooks: {
stripe: {
on: {
"invoice.paid": defineWebhookBinding<{ data: { object: { id: string; customer: string } } }>({
block: recordPayment,
input: (e) => ({ invoiceId: e.payload.data.object.id }),
}),
},
},
},
})();
The binding has the shape of an action, a block plus how to run it, but it lives on webhooks and never in actions. Stripe's event type picks it, so a caller cannot name it as an action, and it is never an MCP tool. input turns Stripe's event into the input of recordPayment. actions is empty because a flow that only receives webhooks has nothing for a caller to call.
The flow holds routing and no secret. The app declares how to verify a delivery, with a webhook adapter in adapters: createWebhookTransportAdapter({ providers: { stripe: { verify: stripeWebhookVerifier(...), eventType } } }). verify checks Stripe's signature and eventType reads the event's type from the payload.
A delivery with a bad signature is refused with 401 and nothing runs. A verified one is answered 202 at once and the block runs after the response, so a 202 does not mean recordPayment has finished. An event type with no binding is also answered 202 and ignored.
With no sessionId on the binding, each event runs in its own fresh session. Derive one from the payload, as in sessionId: (e) => `customer-${e.payload.data.object.customer}` , and one customer's invoices share a session while another customer gets a separate one. No end user is involved, so the run uses defaultUserId: "system", with requireUser: false. Those two fields name the user the run acts as; they do not authenticate anyone. verify guards the webhook route; the app's other routes need a resolver. See Webhooks and the Stripe recipe.
A schedule. A schedule is a name and a cron string on the flow, with the block to run. The app keeps the clock: when the time comes it calls that schedule's dispatch route, which every app serves under /api/flows, and the route runs the block. On a serverless host, one cron route wakes the app to do it. It is not an action call either. See Schedules.
What it gets you
- A flow is written once.
supportis reached by a page, a terminal and an MCP client with no code for each. - Moving hosts changes one file. Going from a Next.js route to
servetouches the mount file, notsupport,billingordigest. - A forged webhook never starts a block. A request with a bad signature never reaches
recordPayment. Webhooks has the responses. - A misconfigured app fails when it starts, not on the first request. Runtime options lists what is refused.
How it fits with the others
Flows and actions define what is served: the actions a flow exposes are its HTTP and MCP surface. Auth and tenancy decides who may use each door. Persistence covers what stores keeps. A flow that hands work to another flow dispatches it, which is not a door: see Background work.
Next
- Add to an existing app mounts the handler in a Next.js route.
- Stripe webhook builds the
billingflow end to end. - Runtime options lists every option on
createFlowState.