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 moves a document through stages under 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 to run. Starting an instance runs it against one document.

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 workflow packages. @sanity/workflow-engine provides the definition language and runtime. @sanity/workflow-cli deploys definitions and drives instances from a terminal. Install both at the same version:

npm install @sanity/workflow-engine @sanity/workflow-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/workflow-engine/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'.

Neither transition declares when, the GROQ condition that decides when a transition fires, so each uses the default $allActivitiesDone: the instance leaves the stage once every activity in that stage resolves. approved declares no transitions at all, which makes it terminal.

Configure and deploy the definition

The Workflows CLI finds the nearest sanity.workflow.ts in the directory you run it in or one of its parents. That file binds your definitions to a deployment: a name, an environment partition, and the dataset the engine writes its own documents to. Create it beside your workflows/ directory.

import type {WorkflowDeploymentInput} from '@sanity/workflow-engine'
import {defineWorkflowConfig} from '@sanity/workflow-engine/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 the dataset. name identifies this deployment when a config holds more than one. partition is the environment partition the engine scopes its documents to. A deployment alone on its dataset can omit it and uses default; this quick start sets it so instance IDs start with dev. definitions is a list, so one deployment can deploy several workflows. The config has no resourceAliases: that key is optional, and you add it 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.

expectedMinReaderModel states the stored-data model every runtime sharing this workflow resource supports. This definition needs model 10 because its subject field is required. If other runtimes use the resource, complete the reader upgrade before acknowledging model 10.

Deploy the definition:

npx sanity-workflows deploy

The CLI validates every definition before it writes anything, then reports what it created. npx sanity-workflows deploy --check validates without contacting the dataset, and --dry-run diffs against what is already deployed. Every command also resolves under its canonical workflows topic, so npx sanity-workflows workflows deploy does the same thing.

Share your definitions!

Start an instance

An instance is one live run of a definition against content. Starting one pins the definition version it runs under and freezes a snapshot of it, so deploying a change later does not alter an instance already in flight. Supply the document through the subject field the definition declared as an input.

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

dataset:PROJECT_ID:DATASET_NAME:DOCUMENT_ID is a global document reference: the scheme, your project ID, your dataset, and the document _id. 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 landed in:

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

Keep that id. Every command in the next step takes it. Pass the published document _id, not a draft or release version id: a versioned id is rejected before the instance is created, with Invalid GDR "…": dataset document ID "drafts.article-1" identifies a stored draft or release version.

Move the document 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 satisfies the stage’s only activity, so the to-review transition fires inside the same call and the instance is in review before the command returns. That chain of transitions inside one call is the cascade. Each stage in this definition has one activity, so each fire-action 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