> For AI agents: the complete Sanity documentation index is available at [https://www.sanity.io/docs/llms.txt](https://www.sanity.io/docs/llms.txt).

# Quick start: run your first workflow

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

> [!WARNING]
> Early access
> Workflows is in early access, built in public. Read [How early access works](https://www.sanity.io/docs/workflows/prerelease) before you rely on it.



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**

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

**pnpm**

```shell
pnpm add @sanity/workflows @sanity/workflows-cli
```

**yarn**

```shell
yarn add @sanity/workflows @sanity/workflows-cli
```

**bun**

```shell
bun add @sanity/workflows @sanity/workflows-cli
```

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

**npm**

```shell
npx sanity@latest login
```

**pnpm**

```shell
pnpm dlx sanity@latest login
```

**yarn**

```shell
yarn dlx sanity@latest login
```

**bun**

```shell
bunx sanity@latest login
```

> [!TIP]
> Authorize in CI
> In CI, set `SANITY_AUTH_TOKEN` instead of logging in. With neither, the CLI stops before it writes anything and reports `No Sanity token found — run `sanity login`, or set SANITY_AUTH_TOKEN.`

## 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`.

**workflows/article-review.ts**

```typescript
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:

**sanity.workflow.ts**

```typescript
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](https://www.sanity.io/docs/workflows/upgrade) before you set 10.

Deploy the definition:

**npm**

```shell
npx sanity-workflows deploy
```

**pnpm**

```shell
pnpm dlx sanity-workflows deploy
```

**yarn**

```shell
yarn dlx sanity-workflows deploy
```

**bun**

```shell
bunx 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.

> [!NOTE]
> Share your definitions!
> During early access, we strongly encourage you to leave definition sharing enabled when deploying. Your definitions show us the domains and problems Workflows must support, helping us improve the product.
> Sharing is optional. See [Definition sharing](https://www.sanity.io/docs/workflows/deploy-definitions) for the opt-out control.



## 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:

**npm**

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

**pnpm**

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

**yarn**

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

**bun**

```shell
bunx 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](https://www.sanity.io/docs/workflows/cross-resource-workflows) shows. The command prints the instance ID and the stage the instance is in:

**CLI**

```text
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`:

**npm**

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

**pnpm**

```shell
pnpm dlx sanity-workflows fire-action INSTANCE_ID --activity write --action submit
```

**yarn**

```shell
yarn dlx sanity-workflows fire-action INSTANCE_ID --activity write --action submit
```

**bun**

```shell
bunx 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:

**npm**

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

**pnpm**

```shell
pnpm dlx sanity-workflows fire-action INSTANCE_ID --activity sign-off --action approve
```

**yarn**

```shell
yarn dlx sanity-workflows fire-action INSTANCE_ID --activity sign-off --action approve
```

**bun**

```shell
bunx sanity-workflows fire-action INSTANCE_ID --activity sign-off --action approve
```

Confirm where the instance ended up:

**npm**

```shell
npx sanity-workflows show INSTANCE_ID
```

**pnpm**

```shell
pnpm dlx sanity-workflows show INSTANCE_ID
```

**yarn**

```shell
yarn dlx sanity-workflows show INSTANCE_ID
```

**bun**

```shell
bunx 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.

> [!NOTE]
> Running workflows in production
> In this quick start, your terminal ran the engine during `start` and `fire-action`. Because the engine is a library rather than a hosted background service, production workflows use [Sanity Functions](https://www.sanity.io/docs/workflows/sanity-functions) to call `tick`, which rechecks time-based conditions, and `drainEffects`, which runs queued effects.

## Next steps

- [Run the engine from your own code](https://www.sanity.io/docs/workflows/run-the-engine)
- [Generate Sanity Functions from your workflow definitions](https://www.sanity.io/docs/workflows/generate-functions) to automate transitions and effects in production. To write the Functions yourself, [Run Workflows with Sanity Functions](https://www.sanity.io/docs/workflows/sanity-functions) lists what each must do.
- [Send a Slack message from a workflow](https://www.sanity.io/docs/workflows/send-slack-message) with a generated Function
- [Put these workflows in front of editors in Sanity Studio](https://www.sanity.io/docs/workflows/studio-plugin)
- [See a complete workflow with approval data, change requests, and a review loop](https://www.sanity.io/docs/workflows/cookbook-editorial-review)
- [Prove a definition’s paths in memory before you deploy it](https://www.sanity.io/docs/workflows/testing)
- [Run the same definition in more than one environment](https://www.sanity.io/docs/workflows/deploy-definitions)

> [!NOTE]
> Visiting agent?
> Workflows has MCP tools that let an agent inspect, operate, author, validate, and deploy workflows. An agent host embeds these tools. Ask the person you work for whether your host [includes the Workflows tools](https://www.sanity.io/docs/workflows/mcp).



