Agent Actions

Operations

Agent Action allow advanced control of how they create or edit documents.

Agent Actions use the targetDocument property to establish how they write to your dataset.

Generate and Patch support initial values

Default behavior

By default, Agent Actions will never mutate a published document. Whenever you supply a published ID, the action creates a draft first before applying any changes. If a draft already exists, it will use the existing draft as the source.

To change this behavior, you can supply forcePublishedWrite: true to the action request. For example:

await client.agent.action.transform({
  schemaId: 'sanity.workspace.schema.default',
  documentId: 'publishedId',
  targetDocument: {
    operation: 'edit',
    _id: 'publishedId'
  },
  forcePublishedWrite: true,
  instruction: 'Replace "Create" with "Canvas"',
})

Documents that use liveEdit: true in their schema are treated as forcePublishedWrite: true by default.

For content release version documents, the operations will only create version documents when a version ID is paired with an operation that creates a new document.

Check the returned _id for clarity

Operation types

edit

Requires an _id for an existing document. This is the verbose version of omitting targetDocument and only relying on documentId.

create

The _id is optional. If omitted, a draft document is created. You can provide a valid version ID as the _id to create a release version of a document.

createOrReplace

If you provide an existing _id, the new document will override it. If the provided _id doesn't exist, the Agent Action will create a new document with that ID.

createIfNotExists

If the provided _id does not exist, a document is created using the documentId as the source. If it does exist, it used the provided _id document as the source.

Was this page helpful?