APIs and SDKs

Creating and updating documents with @sanity/client

Learn how to create, update, and patch documents with @sanity/client, including array manipulation, chained operations, and conditional updates.

The @sanity/client library provides methods for creating and modifying documents in your dataset. This guide covers the create, createOrReplace, createIfNotExists, and patch methods with practical examples for each.

Prerequisites

All mutation methods require an authenticated client with a write token and return promises that resolve with the created or updated document. See Getting started with @sanity/client for setup instructions.

Creating documents with client.create()

The create() method creates a new document in your dataset. Sanity automatically generates a unique _id for the document unless you provide one.

You can also specify a custom document ID:

Creating or replacing with client.createOrReplace()

The createOrReplace() method creates a new document or completely replaces an existing one if a document with the specified _id already exists. This is useful for idempotent operations where you want to ensure a specific document state.

Creating if not exists with client.createIfNotExists()

The createIfNotExists() method creates a document only if no document with the specified _id exists. If the document already exists, the operation does nothing and returns the existing document.

This method is particularly useful for initialization scripts or ensuring default documents exist without overwriting user modifications.

Patching documents with client.patch()

The patch() method lets you update specific fields in an existing document without replacing the entire document. You can chain multiple operations together to perform complex updates.

The commit() method executes the patch operation. You can chain multiple patch operations before calling commit().

Setting fields with .set()

The set() method sets or overwrites field values. You can set multiple fields at once or use dot notation to set nested fields.

Setting only if missing with .setIfMissing()

The setIfMissing() method sets field values only if the fields don't already exist or are null. This is useful for setting default values without overwriting existing data.

Removing fields with .unset()

The unset() method removes fields from a document. You can remove multiple fields by passing an array of field paths.

Incrementing and decrementing with .inc() and .dec()

The inc() and dec() methods increment or decrement numeric field values. These operations are atomic and useful for counters, view counts, or other numeric tracking.

Conditional patches with .ifRevisionId()

The ifRevisionId() method ensures that a patch only applies if the document's current revision matches the specified revision ID. This prevents race conditions and ensures you're updating the version of the document you expect.

This is particularly useful in collaborative environments where multiple users or processes might be updating the same document.

Working with arrays

The patch API provides several methods for manipulating array fields, letting you add and remove items without replacing the entire array.

Inserting items with .insert()

The insert() method adds items to an array at a specific position. You can insert items before or after existing items, or at the beginning or end of the array.

Appending items with .append()

The append() method adds items to the end of an array. This is a convenient shorthand for inserting after the last item.

Prepending items with .prepend()

The prepend() method adds items to the beginning of an array.

Deleting array elements

You can remove items from arrays using the unset() method with array index notation. You can also use array filters to remove items that match specific criteria.

The @ symbol represents the current array item in filter expressions. You can use comparison operators to match items based on their values or properties.

Chaining multiple operations

You can chain multiple patch operations together to perform complex updates in a single transaction. All operations are applied atomically when you call commit().

This ensures data consistency and reduces the number of API calls needed to update a document.

Actions

In addition to the client’s helper functions, you can also create, edit, and delete documents with the Actions.

Actions allow you to use the same approach used by Studio to manipulate documents. Learn more in our guide to mutating documents with actions.

Next steps

For additional settings and patterns to use alongside mutations, check the advanced client patterns guide.

Was this page helpful?