Query instance history and audit trails
Query workflow instance history, resolve actor stamps, separate effect history, and record custom audit entries.
Every step an instance takes is recorded in its history array. This guide reads that trail for the instance you finished in the quick start, first with the CLI and then from a script with the engine. A GROQ query covers reporting across many instances.
You need:
- The finished quick start, and the
INSTANCE_IDitsstartcommand printed. That instance is inapproved. If you lost the id,npx sanity-workflows list --include-completedlists 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 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. Firing write/submit resolved the activity, and the Op applied: status.set row records the status: 'done' that the action declares. The to-review transition then fired inside the same call, and its entries follow. That is the cascade from the quick start, recorded.
(by …) is the id of the user whose token committed the entry. In the quick start, that is your CLI login.
Read the history from a script
In code, engine.getInstance({instanceId}) returns the complete WorkflowInstance. Read instance.history and narrow each entry by _type. Create scripts/show-history.ts. It imports the engine from the engine guide and prints the stage moves and fired actions:
import type {HistoryEntry} from '@sanity/workflow-engine'
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 the entry’s fields: activity and action exist only on actionFired, and fromStage and toStage only on transitionFired. Run it with the quick start’s instance id:
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 is whoever committed each entry, not the token that reads it. Running the script with a different token prints the same ids.
Query history across instances
For reporting across instances, query the workflow resource with the client that scripts/engine.ts exports. Create scripts/list-histories.ts. It filters by the engine’s partition. Instance documents store the partition in their tag field, so the query filters on tag == $tag and binds the quick start’s partition, 'dev', as $tag. That is the bulk-read path. getInstance is the safer default for one known instance.
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 count as the CLI’s History section. If you ran the engine guide’s script, its 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. Every engine-stamped entry may also have executionContext. Event-specific fields are listed under HistoryEntry types in the engine reference.
To stream entries as an in-flight instance appends them, run npx sanity-workflows tail INSTANCE_ID. The quick start’s instance is finished, so tail it from a new instance.
What actor and execution-context stamps record, and why they are not an authenticated record, is explained in 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 includes an MCP server for inspecting, operating, authoring, validating, and deploying workflows. Ask your human to set up the MCP server.
Was this page helpful?
Feedback submitted, thanks for helping us improve the docs. Have you tried these alternatives?