Workflows

Quick start: run your first workflow

Define your first workflow in TypeScript, deploy it, and move a Sanity document through its stages.

Early access

Sanity Workflows tracks a document's progress through stages, using rules you deploy. You author a definition that names the stages, the work inside each one, and the conditions for moving between them. Deploying the definition makes it available. Starting it for one document creates an instance, the record that moves between stages.

This quick start deploys an article review workflow, starts an instance against a document in your dataset, and moves that instance to its final stage. Every step runs from a terminal with the Workflows CLI. No Sanity Studio is involved.

You need:

  • Node.js 20.12 or later.
  • A Sanity project and a dataset you can write to.
  • One published article document in that dataset, with title and body fields, for the workflow to act on. Note its _id. Later guides build on this document. If your content uses another type or other field names, substitute yours wherever they appear.

Install the packages

Install the latest Workflows packages. @sanity/workflows provides the definition language and the engine library your code calls. @sanity/workflows-cli deploys definitions and drives instances from a terminal. Install both at the same version:

npm install @sanity/workflows @sanity/workflows-cli

The CLI reads the token from your Sanity login session. Log in once:

npx sanity@latest login

Authorize in CI

Define a three-stage workflow

A definition names the stages a document passes through, the activities inside each stage, and the transitions that move an instance between stages. Create workflows/article-review.ts with three stages: drafting, review, and approved.

import {
  defineAction,
  defineActivity,
  defineField,
  defineStage,
  defineTransition,
  defineWorkflow,
} from '@sanity/workflows/define'

export const articleReview = defineWorkflow({
  name: 'article-review',
  title: 'Article review',
  description: 'Takes one article from drafting, through an editor review, to approved.',
  initialStage: 'drafting',
  fields: [
    defineField({
      type: 'subject',
      name: 'subject',
      title: 'Article',
      required: true,
      description: 'The document this instance moves through the stages.',
      initialValue: {type: 'input'},
    }),
  ],
  stages: [
    defineStage({
      name: 'drafting',
      title: 'Drafting',
      description: 'The writer is working on the article.',
      activities: [
        defineActivity({
          name: 'write',
          title: 'Write the article',
          actions: [
            defineAction({
              name: 'submit',
              title: 'Submit for review',
              status: 'done',
            }),
          ],
        }),
      ],
      transitions: [defineTransition({name: 'to-review', title: 'Send to review', to: 'review'})],
    }),
    defineStage({
      name: 'review',
      title: 'Editorial review',
      description: 'An editor reads the article and approves it.',
      activities: [
        defineActivity({
          name: 'sign-off',
          title: 'Review the article',
          actions: [
            defineAction({
              name: 'approve',
              title: 'Approve',
              status: 'done',
            }),
          ],
        }),
      ],
      transitions: [
        defineTransition({name: 'to-approved', title: 'Approve and finish', to: 'approved'}),
      ],
    }),
    defineStage({
      name: 'approved',
      title: 'Approved',
      description: 'The article is approved. Nothing more to do here.',
    }),
  ],
})

The subject field identifies the document the instance is about. initialValue: {type: 'input'} means you supply that document when you start the instance. required: true rejects a start that omits the reference. It also blocks actions and transitions if the selected document becomes unavailable while the workflow runs.

Each non-terminal stage holds one activity with one action. Firing the action resolves its activity, because the action declares status: 'done'.

A transition's when property is a GROQ condition, and the engine fires the transition when it is true. Neither transition here declares when. Without one, a transition uses the default condition $allActivitiesDone, so the instance leaves the stage once every activity in it resolves. approved declares no transitions, which makes it the terminal stage. An instance that reaches it is complete.

Configure and deploy the definition

The Workflows CLI reads its settings from sanity.workflow.ts. It looks for the file in the directory you run it in, then in each parent directory. The file defines a deployment: the definitions to deploy and where the engine stores its documents. Create the file beside your workflows/ directory:

import type {WorkflowDeploymentInput} from '@sanity/workflows'
import {defineWorkflowConfig} from '@sanity/workflows/define'

import {articleReview} from './workflows/article-review'

export const dev = {
  name: 'dev',
  partition: 'dev',
  expectedMinReaderModel: 10,
  workflowResource: {type: 'dataset', id: 'PROJECT_ID.DATASET_NAME'},
  definitions: [articleReview],
} satisfies WorkflowDeploymentInput

export default defineWorkflowConfig({deployments: [dev]})

Replace PROJECT_ID with your Sanity project ID and DATASET_NAME with your dataset name. Each key in the deployment has one job:

  • name: identifies this deployment when a config holds more than one.
  • partition: keeps this environment’s documents separate from other environments on the same dataset. Instance IDs start with the partition, so this quick start’s instance IDs start with dev. If this is the only deployment on its dataset, you can leave partition out, and the engine uses default.
  • expectedMinReaderModel: the reader model, a version of the engine’s stored-data model, that you confirm every runtime on this dataset can read. A runtime is any app, Function, or script that calls the engine for this dataset. Deploy fails if the definitions need a higher reader model.
  • workflowResource: the dataset where the engine stores definitions and instances.
  • definitions: the workflows to deploy. It’s a list, so one deployment can hold several workflows.

The config has no resourceAliases. You add that optional key only when a workflow acts on content outside this dataset.

The config exports the deployment as dev so other code can import it. The Studio plugin, apps, and scripts you add in later guides must use the same dataset and partition: 'dev'. An engine on any other partition can't see these instances.

This definition needs reader model 10, because its subject field is required. With a lower expectedMinReaderModel, the deploy fails with an error that includes the submitted definitions require at least 10. If other runtimes already use this dataset, upgrade them before you set 10.

Deploy the definition:

npx sanity-workflows deploy

The CLI validates every definition before it writes anything, then reports what it created. To check your definitions without deploying them, add one of these flags:

  • --check: validates the definitions without contacting the dataset.
  • --dry-run: validates the definitions and shows how they differ from what’s already deployed. It writes nothing.

Every command also works with a workflows prefix, so npx sanity-workflows workflows deploy is the same command.

Share your definitions!

Start an instance

An instance is one pass through a definition for one document. When an instance starts, it keeps a copy of the definition version it started with. Deploying a changed definition later doesn’t change instances that are already running. To start an instance, pass your article as the subject field, which the definition declares as an input:

npx sanity-workflows start article-review \
  --field subject='{"id":"dataset:PROJECT_ID:DATASET_NAME:DOCUMENT_ID","type":"article"}'

The id value is a global document reference (GDR). A GDR names the dataset as well as the document: dataset:, then your project ID, your dataset name, and the document’s _id. Replace DOCUMENT_ID with the _id you noted. type is the document’s _type. A subject can point at any dataset the deployment can reach, as Coordinate content across projects and datasets shows. The command prints the instance ID and the stage the instance is in:

Started dev.wf-instance.a1b2c3d4e5f6 — now at drafting

Keep that instance ID. Every command in the next step takes it.

Pass the published document _id, not a draft or release version ID such as drafts.article-1. The CLI rejects a version ID before it creates the instance. The error includes dataset document ID "drafts.article-1" identifies a stored draft or release version.

Move the instance through the stages

Moving an instance means firing the actions that resolve its activities. The article-review definition has one action per non-terminal stage. Fire the first to resolve write in drafting:

npx sanity-workflows fire-action INSTANCE_ID --activity write --action submit

Replace INSTANCE_ID with the ID the start command printed.

Resolving write completes the only activity in drafting. So the to-review transition fires in the same call, and the instance is in review before the command returns. A chain of transitions inside one call is called a cascade. Each stage here has one activity, so each fire-action call moves the instance one stage. Fire the second action:

npx sanity-workflows fire-action INSTANCE_ID --activity sign-off --action approve

Confirm where the instance ended up:

npx sanity-workflows show INSTANCE_ID

show prints the state, activities, and effects of the instance. The instance is in approved and complete: approved declares no transitions, so nothing can move it further. To see what can be fired on an instance at any point, run npx sanity-workflows fire-action INSTANCE_ID with no --action.

To find instances later, run npx sanity-workflows list. It lists in-flight instances by default; add --include-completed to see this one. npx sanity-workflows show INSTANCE_ID --include history adds every step the instance took, and --json prints the full instance document.

Running workflows in production

Next steps

Visiting agent?

Was this page helpful?

Sanity logosanity.ioAll Systems Operational© Sanity 2026
Change site theme