# @sanity/mutate

> Experimental toolkit for working with Sanity mutations in JavaScript & TypeScript

Latest version **0.18.2** (published 2026-09-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sanity/mutate
pnpm add @sanity/mutate
yarn add @sanity/mutate
bun add @sanity/mutate
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.18.2 |
| Published | 2026-09-09 |
| First published | 2024-08-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 9 |
| Unpacked size | 468.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| Author | Sanity.io <hello@sanity.io> |
| Maintainers | sanity-svc.npm, sanity-io |
| Keywords | sanity, mutations, patch |

## Links

- npm: https://www.npmjs.com/package/@sanity/mutate
- Repository: https://github.com/sanity-io/mutate
- npm.io page: https://npm.io/package/@sanity/mutate

## Dependencies (9)

- [rxjs](https://npm.io/package/rxjs.md) ^7.8.2
- [lodash](https://npm.io/package/lodash.md) ^4.18.1
- [nanoid](https://npm.io/package/nanoid.md) ^5.1.11
- [mendoza](https://npm.io/package/mendoza.md) ^3.0.8
- [hotscript](https://npm.io/package/hotscript.md) ^1.0.13
- [@sanity/uuid](https://npm.io/package/@sanity/uuid.md) ^3.0.2
- [@sanity/client](https://npm.io/package/@sanity/client.md) ^8.6.1
- [@isaacs/ttlcache](https://npm.io/package/@isaacs/ttlcache.md) ^2.1.4
- [@sanity/diff-match-patch](https://npm.io/package/@sanity/diff-match-patch.md) ^3.2.0

## Recent versions

- 0.18.2 (latest) — 2026-09-09
- 0.17.0-media-library.1 (media-library) — 2026-02-06
- 0.15.1-canary.1 (canary) — 2026-01-23
- 0.10.1-lodash.0 (lodash) — 2024-10-22
- 0.18.1 — 2026-06-11
- 0.18.0 — 2026-05-22
- 0.17.1 — 2026-05-14
- 0.17.0 — 2026-05-13
- 0.16.1 — 2026-01-23
- 0.16.0 — 2026-01-23
- 0.15.1-canary.0 — 2026-01-23
- 0.15.0 — 2025-12-05
- 0.14.0 — 2025-10-14
- 0.13.0 — 2025-09-04
- 0.12.6 — 2025-08-27
- … 30 more at https://npm.io/package/@sanity/mutate/versions

## README

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="docs/public/mark-display-dark.svg">
    <img src="docs/public/mark-display-light.svg" alt="@sanity/mutate" width="144" height="144">
  </picture>
</p>

# @sanity/mutate

> [!NOTE]
> Experimental. Use at your own risk.

A TypeScript toolkit for [Sanity](https://sanity.io) mutations:

- Declarative, composable mutation creators
- A pure, in-memory apply engine
- An optimistic local store with rebase semantics

## Install

```sh
npm install @sanity/mutate
```

## Example

```ts
import {
  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)),
  },
)
```

## Documentation

Full reference and guides at **<https://mutate.sanity.dev>** (sources in [`docs/`](./docs); run the site locally with `pnpm docs:dev`):

- [Getting Started](https://mutate.sanity.dev/guide/getting-started)
- [Applying Mutations](https://mutate.sanity.dev/guide/applying-mutations) — in-memory apply
- [Optimistic Store](https://mutate.sanity.dev/guide/optimistic-store) — local replica with rebase
- [Recipes](https://mutate.sanity.dev/guide/recipes)
- [Differences from the Sanity API](https://mutate.sanity.dev/guide/differences-from-sanity-api)
- API reference: [mutations](https://mutate.sanity.dev/api/mutations), [patches](https://mutate.sanity.dev/api/patches), [operations](https://mutate.sanity.dev/api/operations), [encoders](https://mutate.sanity.dev/api/encoders)

## License

MIT

---
_Source: https://npm.io/package/@sanity/mutate · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
