Workflows

Run the engine from your own code

Create a Workflows engine from your deployment config, then start and advance an instance from a TypeScript script.

In the quick start, the Workflows CLI started and advanced an instance for you. The CLI is a wrapper around the engine, a TypeScript library you can call from your own code. Servers, workers, and Functions use the engine the same way.

This guide creates an engine from the quick start’s dev deployment, then starts an article-review instance and moves it to review from a script. Later guides import the engine module you create here.

You need:

  • The finished quick start: article-review deployed, and a sanity.workflow.ts that exports the dev deployment.
  • An API token with write access to the dataset, such as an Editor token. Authentication and tokens explains how to create one.
  • The _id of the published article from the quick start.

Install the client and a TypeScript runner

The engine needs a Sanity client to read and write workflow documents. tsx runs the TypeScript script without a build step.

Create the engine

Create scripts/engine.ts. It builds the client and the engine from the dev deployment, so the script and the CLI use the same dataset and partition:

clientConfigFromResource turns the deployment’s workflowResource into the client’s projectId and dataset. ENGINE_API_VERSION is the API version the CLI uses. Set useCdn: false so every read returns the latest instance state.

createEngine takes the client, the resource that holds workflow documents, and the partition. The engine only sees instances in its own partition, so partition must match the deployment the instances were started under. Deployments and partitions covers running more than one.

The engine acts as the token’s identity. Every action it fires is recorded against that user or robot, and conditions such as $actor evaluate against it. Actors, tokens, and what’s actually enforced explains what that identity can and can’t do.

Start and advance an instance

Create scripts/review-article.ts. It starts an instance against the article you pass in, lists the actions the token can fire, and fires write/submit:

gdrRef builds the same global document reference you typed into the quick start’s start command, from the deployment’s resource and the document _id.

evaluate reads the instance from the token’s point of view and doesn’t change it. Each action reports allowed. When an action isn’t allowed, it also reports a disabledReason.

fireAction fires write/submit, which marks the write activity done. The engine then runs the cascade: it fires every transition whose condition now holds. Here, the stage’s to-review transition fires because all of its activities are done. The CLI’s fire-action command runs the same code.

fireAction returns these values:

  • instance: the instance after the cascade.
  • cascaded: the number of transitions the cascade fired.
  • changed: whether the call changed the instance. It’s false when the call succeeds but has nothing to do, for example when the action already ran.

Run the script

Pass the token as an environment variable and the article _id as an argument. Replace YOUR_TOKEN with your API token, and DOCUMENT_ID with the article’s _id:

The script prints the new instance, the actions available in drafting, and the stage after the cascade:

Check the result from the CLI. The CLI reads the same partition, so it shows the instance your script started. Replace INSTANCE_ID with the ID the script printed after Started:

The instance is in review, with write done. Fire sign-off/approve from the CLI or add it to the script to finish it.

Instance not found

Next steps

Was this page helpful?