serve()
, which exposes each workflow as its own HTTP endpoint.
If workflows were invoked only by their full URL, it would mean:
- You’d have to provide the URL explicitly like a trigger request
- You’d lose type safety for request and response payloads
context.invoke
, with full type safety and no URLs required.
1
Create workflow objects
Use
createWorkflow()
to define workflows as objects.It works just like serve()
—accepting the same arguments—but does not expose the workflow directly as an HTTP endpoint.
Instead, it simply initializes a workflow object.2
Expose multiple workflows under same endpoint
Use
serveMany()
instead of serve()
to expose multiple workflows on a single catch‑all route.If one workflow is going to invoke another, both must be included in the same serveMany
definition.
First step of using serveMany
is to define a catch-all route.app/serve-many/[...any]/route.ts
In Next.js, a catch‑all route can be defined by creating a
route.ts
file inside a directory named with [...]
, for example: app/serve-many/[...any]/route.ts
.For implementations of serveMany
in other frameworks, you can refer to the projects available in the examples
directory of the workflow-js repository.3
Invoke by passing workflow object
When invoking, pass the workflow object created with
createWorkflow()
(from step 1) as the argument to context.invoke()
.
This removes the need to specify a URL explicitly and ensures the call is
fully type‑safe.4
Trigger
In this example, both Route names are inferred from the keys you pass to
workflowOne
and workflowTwo
are exposed through serveMany
, sharing the same parent path.You can start workflowOne
by sending a trigger request to:
https://your-app/serve-many/workflow-one-route
.serveMany
.
For example, you can start workflowTwo
by sending trigger request to: https://your-app/serve-many/workflow-two-route
.