APIs and SDKs

Creating transactions with @sanity/client

Learn how to group multiple mutations into atomic transactions using @sanity/client, including chaining operations, inline patches, and clientless patterns.

When you need to create, update, and delete multiple documents as a single unit, transactions ensure that all changes succeed or none are applied. This prevents partial updates that could leave your content in an inconsistent state.

This article covers how to create and commit transactions, chain multiple operations, use inline patch syntax, and build standalone transactions without a client instance.

Prerequisites

Before using transactions, make sure you have @sanity/client installed and configured with write access. See Getting started with @sanity/client for setup instructions.

When to use transactions

Use transactions when your mutations depend on each other. For example, if you create a new category and assign it to a post in the same operation, a transaction guarantees both changes apply together. If the category creation fails, the post won't be left referencing a category that doesn't exist.

For independent mutations that don't depend on each other, individual create(), patch(), and delete() calls are simpler and sufficient.

Creating and committing a transaction

Call client.transaction() to start a new transaction, chain your mutation operations, and call .commit() to execute them. All operations are applied atomically. If any operation fails, none of the changes take effect.

Replace product-abc and product-discontinued-xyz with your actual document IDs.

Chaining related operations

Transactions are most useful when operations depend on each other. This example creates a new category, assigns it to a post, and removes an old category in one atomic operation:

Using inline patch syntax

Instead of using a callback function for patches, you can pass an object describing the patch operations directly. This is often more concise when you're setting multiple fields at once:

Inline patches support set, unset, setIfMissing, inc, dec, insert, and append.

Building transactions without a client instance

You can construct patches and transactions independently using the Patch and Transaction classes from @sanity/client. This is useful when you want to build mutation logic in one place and execute it later, or pass it between functions:

Next steps

For mutation options like visibility modes, dry runs, and request cancellation, see Advanced client patterns. To learn more about how transactions relate to the underlying mutation API, see the Transactions reference.

Was this page helpful?