canonical: https://jentic.com/apis/sanity.io/sanity-mutation

# Sanity Mutations API

The Sanity Mutations API creates, updates, and deletes documents in Sanity Content Lake, applying an array of mutations to a dataset as one transaction so either every mutation runs or none do. Mutation types are create, createOrReplace, createIfNotExists, delete, and patch, and delete and patch can target documents by ID or by a GROQ query, with patches applying set, setIfMissing, unset, inc, dec, and insert operations in a fixed order. Requests can return the changed document IDs or full documents, set their own transaction ID, auto-generate array item keys, validate without writing through a dry run, and choose whether to wait until changes are visible to queries. An ifRevisionID check gives optimistic locking so a write fails if the document changed since it was read.

## For AI agents

Create, replace, patch, and delete Sanity documents by ID or GROQ query in one transaction per call, with dry-run validation and optimistic locking by revision ID.

## Scope

Does not run read queries, upload assets, or publish releases. Use for transactional document writes in a Sanity dataset only.

## Capabilities

- Create a document with a generated or given _id, or create it only if it does not already exist
- Patch a document with set, unset, inc, dec, setIfMissing, and insert operations in one call
- Delete or patch every document matching a GROQ query, up to 10,000 documents per transaction
- Validate a batch of mutations without writing anything by setting dryRun to true
- Lock against concurrent edits with ifRevisionID so the patch fails if the document has changed
- Return changed document IDs or full documents with returnIds and returnDocuments

## Use cases

### AI agent writing content changes

An agent asked to fix typos or update prices across documents needs to write to Sanity safely. Through Jentic it searches for 'update a Sanity document', loads the mutate operation, and posts a mutations array with patch set operations while the Sanity token is injected at call time. Running first with dryRun true lets it validate the batch, and the real call applies every change in one transaction.

Example prompt: POST `/data/mutate/production` with dryRun true and a mutations array containing a patch for id '123' that sets title to 'Updated title', then repeat without dryRun and returnIds true

### Bulk cleanup by GROQ query

Removing or correcting many documents at once is a single mutation when it targets a GROQ query instead of an ID. A delete or patch with a query and params acts on every matching document, up to 10,000 per transaction; larger sets are split and paginated by _id as the spec recommends.

Example prompt: POST `/data/mutate/staging` with a delete mutation whose query is '*[_type == "feature" && viewCount < $views]' and params views 10, with returnIds true

### Safe concurrent editing

When several processes edit the same document, a blind write can overwrite someone else's change. Passing ifRevisionID with the revision the agent last read makes the patch fail if the document has moved on, and visibility sync makes the call return only once the change is visible to subsequent queries.

Example prompt: POST `/data/mutate/production` with a patch for id 'settings' that includes ifRevisionID from the last read and sets title, with visibility 'sync'

## Key endpoints

| Method | Path | Description |
| --- | --- | --- |
| POST | `/data/mutate/{dataset}` | Apply an array of mutations as one transaction |

## Key resources

- **Mutations** — create, createOrReplace, createIfNotExists, delete, and patch operations
- **Transactions** — One transaction ID and per-mutation results for each request

## AI readiness

This API is usable in Jentic One now. Its AI-readiness score against Jentic's framework shows where it stands today and where improvements would make it even easier for agents to use.

- **Score:** 0 / 100
- **Maturity:** Non-Ready
- **Dimensions:**
  - Foundational Compliance: 79 / 100
  - Developer Experience & Jentic Compatibility: 65 / 100
  - AI-Readiness & Agent Experience: 27 / 100
  - Agent Usability: 94 / 100
  - Security: 60 / 100
  - AI Discoverability: 0 / 100
- **View full report:** https://jentic.com/apis/sanity.io/sanity-mutation/scorecard
- **How the score is calculated:** https://docs.jentic.com/reference/api-readiness-framework/overview/
- **More about the dimensions:** https://docs.jentic.com/reference/api-readiness-framework/specification/#dimensional-model-overview

### Score it yourself

Every API in the directory is allowlisted, so you can re-score it with no key required.

- **Score your own API:** https://jentic.com/scorecard.md
- **Scoring CLI agent skill:** https://github.com/jentic/jentic-api-scorecard/blob/main/skills/jentic-api-scorecard/SKILL.md

```sh
npx @jentic/api-scorecard-cli score <openapi-url>
```

## Why Jentic

- **Setup:** Wiring the Sanity Mutations API by hand means templating the projectId and apiVersion into the host, sending a bearer token with write access, and shaping each mutation type and patch operation exactly. Through Jentic you install once, import it from the API Directory, store the token once, and your agent calls it.
- **Permission scoping:** Sanity puts the dataset name in the URL path (`/data/mutate/{dataset}`), so a rule can pin your agent to one dataset such as staging. The mutation type travels in the request body, so a rule bounds where the agent can write, not whether it creates or deletes; use a Sanity token whose own role fits the job.
- **Credential handling:** Your Sanity token is stored once, encrypted, by your own Jentic One instance and injected at execution time. It never enters the agent's prompt, logs, or context.
- **Discovery method:** Agents search Jentic by intent such as 'update a Sanity document' or 'delete Sanity documents by query', and Jentic returns the mutate operation with its input schema, including every mutation type.

## Related APIs

- **Sanity Actions API** — Higher-level publish, unpublish, and release actions
- **Sanity Doc API** — Read the latest write of a document and its revision
- **Sanity HTTP API** — Query documents with GROQ to find what to change
- **Contentful** — Headless CMS with entries, assets, and publishing

## FAQ

### What authentication does the Sanity Mutations API use?

Per its OpenAPI spec, the Sanity Mutations API uses a bearer token in the Authorization header. The spec notes deleting system documents requires manage permission. Through Jentic the token is stored by your own instance and attached at call time, so it stays out of the agent's context.

### What is the difference between create, createOrReplace, and createIfNotExists?

create fails if a document with the given ID already exists, and generates an ID if _id is missing or ends with a dot. createOrReplace replaces an existing document completely, or acts as delete then create if the type differs. createIfNotExists silently does nothing when the document already exists.

### Can I test Sanity mutations without changing data?

Yes. Set dryRun to true and the mutations are validated but not executed, so an agent can check a batch before it writes.

### What are the rate limits for the Sanity Mutations API?

The OpenAPI spec does not specify rate limits. It does cap delete and patch by query at 10,000 documents per transaction. Check the Sanity documentation at https://www.sanity.io/docs for current API limits.

### Is there a Sanity MCP server?

You don't need an MCP server to give your agent the Sanity Mutations API. Jentic connects it directly from the API Directory: import it, store your Sanity token once, and your agent calls the mutate operation with the full mutation schema when it needs to write.

### How do I update a Sanity document through Jentic?

Search Jentic for 'update a Sanity document', load the mutate operation, and execute POST `/data/mutate/{dataset}` with a mutations array containing a patch with the document id and a set object. To run it on your own infrastructure, install Jentic One from its GitHub repo.

### Can I limit what my agent is allowed to do with the Sanity Mutations API?

Yes, by dataset. Write a rule that allows the mutate operation only at `/data/mutate/staging`, and the agent can create and patch staging documents while production stays out of reach. Every call it makes is logged.
