CLI quick start
Workflows allow you to build durable, multi-step applications using the Workers platform. A Workflow can automatically retry, persist state, run for hours or days, and coordinate between third-party APIs.
You can build Workflows to post-process file uploads to R2 object storage, automate generation of Workers AI embeddings into a Vectorize vector database, or to trigger user lifecycle emails using your favorite email API.
- Sign up for a Cloudflare account ↗.
- Install
Node.js
↗.
Node.js version manager
Use a Node version manager like Volta ↗ or nvm ↗ to avoid permission issues and change Node.js versions. Wrangler, discussed later in this guide, requires a Node version of 16.17.0
or later.
Workflows are defined as part of a Worker script.
To create a Workflow, use the create cloudflare
(C3) CLI tool, specifying the Workflows starter template:
This will create a new folder called workflows-tutorial
, which contains two files:
src/index.ts
- this is where your Worker script, including your Workflows definition, is defined.wrangler.toml
- the configuration for your Workers project and your Workflow.
Open the src/index.ts
file in your text editor. This file contains the following code, which is the most basic instance of a Workflow definition:
Specifically, the code above:
- Extends the Workflows base class (
WorkflowsEntrypoint
) and defines arun
method for our Workflow. - Passes in our
Params
type as a type parameter so that events that trigger our Workflow are typed. - Defines several steps that return state.
- Defines a custom retry configuration for a step.
- Binds to the Workflow from a Worker’s
fetch
handler so that we can create (trigger) instances of our Workflow via a HTTP call.
You can edit this Workflow by adding (or removing) additional step
calls, changing the retry configuration, and/or making your own API calls. This Workflow template is designed to illustrate some of Workflows APIs.
Workflows are deployed via wrangler
, which is installed when you first ran npm create cloudflare
above. Workflows are Worker scripts, and are deployed the same way:
You can run a Workflow via the wrangler
CLI, via a Worker binding, or via the Workflows REST API.
Refer to the events and parameters documentation to understand how events are passed to Workflows.
You can bind to a Workflow from any handler in a Workers script, allowing you to programatically trigger and pass parameters to a Workflow instance from your own application code.
To bind a Workflow to a Worker, you need to define a [[workflows]]
binding in your wrangler.toml
configuration:
You can then invoke the methods on this binding directly from your Worker script’s env
parameter. The Workflow
type has methods for:
create()
- creating (triggering) a new instance of the Workflow, returning the ID.get()
- retrieve a Workflow instance by its ID.status()
- get the current status of a unique Workflow instance.
For example, the following Worker will fetch the status of an existing Workflow instance by ID (if supplied), else it will create a new Workflow instance and return its ID:
Refer to the triggering Workflows documentation for how to trigger a Workflow from other Workers’ handler functions.
The wrangler workflows
command group has several sub-commands for managing and inspecting Workflows and their instances:
- List Workflows:
wrangler workflows list
- Inspect the instances of a Workflow:
wrangler workflows instances list YOUR_WORKFLOW_NAME
- View the state of a running Workflow instance by its ID:
wrangler workflows instances describe YOUR_WORKFLOW_NAME WORKFLOW_ID
You can also view the state of the latest instance of a Workflow by using the latest
keyword instead of an ID:
The output of instances describe
shows:
- The status (success, failure, running) of each step
- Any state emitted by the step
- Any
sleep
state, including when the Workflow will wake up - Retries associated with each step
- Errors, including exception messages
- Learn more about how events are passed to a Workflow.
- Binding to and triggering Workflow instances using the Workers API.
- The Rules of Workflows and best practices for building applications using Workflows.
If you have any feature requests or notice any bugs, share your feedback directly with the Cloudflare team by joining the Cloudflare Developers community on Discord ↗.