Trigger Workflows
You can trigger Workflows both programmatically and via the Workflows APIs, including:
- With Workers via HTTP requests in a
fetch
handler, or bindings from aqueue
orscheduled
handler - Using the Workflows REST API
- Via the wrangler CLI in your terminal
You can interact with Workflows programmatically from any Worker script by creating a binding to a Workflow. A Worker can bind to multiple Workflows, including Workflows defined in other Workers projects (scripts) within your account.
You can interact with a Workflow:
- Directly over HTTP via the
fetch
handler - From a Queue consumer inside a
queue
handler - From a Cron Trigger inside a
scheduled
handler - Within a Durable Object
To bind to a Workflow from your Workers code, you need to define a binding to a specific Workflow. For example, to bind to the Workflow defined in the get started guide, you would configure a wrangler.toml
with the below:
The binding = "MY_WORKFLOW"
line defines the JavaScript variable that our Workflow methods are accessible on, including create
(which triggers a new instance) or get
(which returns the status of an existing instance).
The following example shows how you can manage Workflows from within a Worker, including:
- Retrieving the status of an existing Workflow instance by its ID
- Creating (triggering) a new Workflow instance
- Returning the status of a given instance ID
You can inspect the status of any running Workflow instance by calling status
against a specific instance ID. This allows you to programmatically inspect whether an instance is queued (waiting to be scheduled), actively running, paused, or errored.
The possible values of status are as follows:
You can stop a Workflow instance by calling abort
against a specific instance ID.
Once stopped, the Workflow instance cannot be resumed.
Restarting an instance will immediately cancel any in-progress steps, erase any intermediate state, and treat the Workflow as if it was run for the first time.
Refer to the Workflows REST API documentation.
Refer to the CLI quick start to learn more about how to manage and trigger Workflows via the command-line.