[!NOTE] Experimental. Use at your own risk.
A TypeScript toolkit for Sanity mutations:
- Declarative, composable mutation creators
- A pure, in-memory apply engine
- An optimistic local store with rebase semantics
npm install @sanity/mutateimport {
at,
create,
createIfNotExists,
patch,
SanityEncoder,
set,
setIfMissing,
} from '@sanity/mutate'
const projectId = '<projectId>'
const dataset = '<dataset>'
const mutations = [
create({_type: 'dog', name: 'Fido'}),
createIfNotExists({_id: 'document-1', _type: 'someType'}),
patch('other-document', [
at('published', set(true)),
at('address', setIfMissing({_type: 'address'})),
at('address.city', set('Oslo')),
]),
]
await fetch(
`https://${projectId}.api.sanity.io/v2026-05-12/data/mutate/${dataset}`,
{
method: 'POST',
mode: 'cors',
credentials: 'include',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(SanityEncoder.encodeAll(mutations)),
},
)Full reference and guides at https://mutate.sanity.dev (sources in docs/; run the site locally with pnpm docs:dev):
- Getting Started
- Applying Mutations — in-memory apply
- Optimistic Store — local replica with rebase
- Recipes
- Differences from the Sanity API
- API reference: mutations, patches, operations, encoders
MIT