Query instance history and audit trails
Query workflow instance history, resolve actor stamps, separate effect history, and record custom audit entries.
The engine records every step an instance takes in the instance’s history array. This guide reads that history for the instance you finished in the quick start. First you read it with the CLI, then from a script. Finally, a GROQ query reads history across many instances for reporting.
You need:
- The finished quick start, and the instance ID its
startcommand printed. That instance is inapproved. If you lost the ID, runnpx sanity-workflows list --include-completedto find it. - For the script steps,
scripts/engine.tsfrom Run the engine from your own code, and aSANITY_AUTH_TOKENthat can read the dataset.
Show the history with the CLI
Add --include history to the show command from the quick start:
npx sanity-workflows show INSTANCE_ID --include historypnpm dlx sanity-workflows show INSTANCE_ID --include historyyarn dlx sanity-workflows show INSTANCE_ID --include historybunx sanity-workflows show INSTANCE_ID --include historyAfter the stage summary, the History section lists every entry in the order the engine wrote it. Your instance ID, timestamps, and user ID will differ:
article-review v1 dev.wf-instance.a1b2c3d4e5f6
Stage: approved
Started: 2026-10-23 14:02:11
Completed: 2026-10-23 14:05:37
Partition: dev
Stage history:
• drafting (exited 2026-10-23 14:03:48)
✔ write [done]
• review (exited 2026-10-23 14:05:37)
✔ sign-off [done]
• approved (current)
History:
[2026-10-23 14:02:11] Stage entered: drafting (by pAbC123xyz)
[2026-10-23 14:03:48] Action fired: write → submit (by pAbC123xyz)
[2026-10-23 14:03:48] Activity status changed: write active → done (by pAbC123xyz)
[2026-10-23 14:03:48] Op applied: status.set write (by pAbC123xyz)
[2026-10-23 14:03:48] Stage exited: drafting (by pAbC123xyz)
[2026-10-23 14:03:48] Transition fired: to-review, drafting → review (by pAbC123xyz)
[2026-10-23 14:03:48] Stage entered: review (by pAbC123xyz)
[2026-10-23 14:05:37] Action fired: sign-off → approve (by pAbC123xyz)
[2026-10-23 14:05:37] Activity status changed: sign-off active → done (by pAbC123xyz)
[2026-10-23 14:05:37] Op applied: status.set sign-off (by pAbC123xyz)
[2026-10-23 14:05:37] Stage exited: review (by pAbC123xyz)
[2026-10-23 14:05:37] Transition fired: to-approved, review → approved (by pAbC123xyz)
[2026-10-23 14:05:37] Stage entered: approved (by pAbC123xyz)Each fire-action call wrote six entries. For the first call, the entries record these steps:
- The
submitaction fired on thewriteactivity. writechanged fromactivetodone.- The
Op applied: status.setentry recorded thestatus: 'done'that the action declares. - The instance left
drafting. - The
to-reviewtransition fired. - The instance entered
review.
The last three entries come from the transition that fired inside the same call. That chain is the cascade from the quick start.
(by …) shows the ID of the user each entry is attributed to. In the quick start, that’s the user you logged in as with the CLI.
Read the history from a script
In code, engine.getInstance({instanceId}) returns the complete WorkflowInstance. Read instance.history, and check each entry’s _type to know which fields it has. Create scripts/show-history.ts. It imports the engine from scripts/engine.ts and prints the stage changes and fired actions:
import type {HistoryEntry} from '@sanity/workflows'
import {engine} from './engine'
async function main() {
const instanceId = process.argv[2]
if (!instanceId) {
throw new Error('Pass the instance id: npx tsx scripts/show-history.ts INSTANCE_ID')
}
const instance = await engine.getInstance({instanceId})
const history: HistoryEntry[] = instance.history
for (const entry of history) {
switch (entry._type) {
case 'stageEntered':
console.log(`${entry.at} entered ${entry.stage}`)
break
case 'actionFired':
console.log(`${entry.at} fired ${entry.activity}/${entry.action} as ${entry.actor?.id}`)
break
case 'transitionFired':
console.log(`${entry.at} ${entry.transition}: ${entry.fromStage} → ${entry.toStage}`)
break
}
}
}
main().catch((error) => {
console.error(error)
process.exit(1)
})Inside each case, TypeScript knows which fields the entry has. For example, activity and action exist only on actionFired, and fromStage and toStage only on transitionFired. Run the script with the quick start’s instance ID, and replace YOUR_TOKEN with a token that can read the dataset:
SANITY_AUTH_TOKEN=YOUR_TOKEN npx tsx scripts/show-history.ts INSTANCE_IDThe script prints the same events as the CLI’s History section, with full ISO timestamps:
2026-10-23T14:02:11.402Z entered drafting
2026-10-23T14:03:48.117Z fired write/submit as pAbC123xyz
2026-10-23T14:03:48.126Z to-review: drafting → review
2026-10-23T14:03:48.126Z entered review
2026-10-23T14:05:37.865Z fired sign-off/approve as pAbC123xyz
2026-10-23T14:05:37.871Z to-approved: review → approved
2026-10-23T14:05:37.871Z entered approvedThe actor in each entry is the user the engine acted for when it wrote the entry. It doesn’t depend on the token that reads the history, so running the script with a different token prints the same IDs.
Usually the actor is the owner of the token that made the change. If a service passes access to createEngine, the actor is the person that service named.
Query history across instances
To report across instances, query the workflow resource with the client that scripts/engine.ts exports. Use this path for bulk reads. For one known instance, getInstance is the safer choice.
The query keeps to the engine’s partition. Instance documents store the partition in a field named tag, so the query filters on tag == $tag and passes the quick start’s partition, 'dev', as $tag. Create scripts/list-histories.ts:
import {client} from './engine'
async function main() {
// @sanity/client reserves `tag` as a request option, so type the GROQ params as a record.
const params: Record<string, string> = {tag: 'dev'}
const rows = await client.fetch(
`*[_type == "sanity.workflow.instance" && tag == $tag]{
_id,
definition,
currentStage,
history
}`,
params,
)
for (const row of rows) {
console.log(`${row._id} ${row.definition} ${row.currentStage} ${row.history.length} entries`)
}
}
main().catch((error) => {
console.error(error)
process.exit(1)
})Run it. Each row is one instance in the dev partition. The quick start’s instance shows 13 entries, the same number as the CLI’s History section. If you ran the script from the engine guide, that instance is listed too:
SANITY_AUTH_TOKEN=YOUR_TOKEN npx tsx scripts/list-histories.tsdev.wf-instance.a1b2c3d4e5f6 article-review approved 13 entriesInspect recorded history events
Every entry has _key, _type, and at. Entries the engine writes can also have executionContext, which records where the engine ran. Each event type adds its own fields. HistoryEntry types in the engine reference lists them.
To watch new entries as a running instance adds them, run npx sanity-workflows tail INSTANCE_ID. The quick start’s instance is finished, so start a new instance to try it.
To learn what the actor and execution context record, and why they don’t prove who acted, see Actor identity and tokens.
Effect history, activity feeds, retention, and custom audit rows are covered in Instance history and audit trail contracts.
Next steps
- Engine, effects, and insights reference: history read paths, entry types, and retention.
- Definitions, stages, and instances: the durable instance document.
- Run an external step with an effect: effect delivery and the separate result ledger.
- Define activities, actions, and operations: the state changes recorded in history.
- Actors and enforcement: who an entry is attributed to and what that proves.
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.
Was this page helpful?
Feedback submitted, thanks for helping us improve the docs. Have you tried these alternatives?