APIs and SDKs

Content Releases and versions with @sanity/client

Learn how to create releases, manage document versions, and schedule publishing using the Sanity JavaScript client.

Content Releases let you group document changes and publish them together. The @sanity/client library provides helper methods on the client.releases namespace for managing releases, along with top-level methods for working with document versions.

These methods require an authenticated client with a write token. See Getting started with @sanity/client for setup instructions. The examples on this page assume a configured client and require @sanity/client 7.8.0 or later.

Create a release

Use client.releases.create() to create a new release. The method returns an object containing the releaseId, which you use to add document versions to the release.

The releaseType can be scheduled, asap, or undecided, and defaults to undecided when omitted. Sanity generates the releaseId for you unless you pass one explicitly.

Add document versions to a release

Use client.createVersion() to add a document version to a release. The most common case is versioning an existing published document. The example below uses baseId, which tells Sanity to copy the current published content into the version for you. To set the version's content explicitly instead, for example for a document that doesn't exist in published form yet, pass an inline document. See Choosing between baseId and inline document.

This snapshots the current published product-123 into the release as a versioned document with the ID versions.<releaseId>.product-123. The published document remains unchanged until the release is published.

Choosing between baseId and inline document

When you create a version of an existing published document, prefer baseId. Sanity copies the current published content into the version for you, so you don't have to fetch and repackage it client-side. baseId is the source of the version's content; publishedId is the logical document the version refers to. In the common case where you are versioning a document's own published edition, both values are the same. You can also pass ifBaseRevisionId to make the action fail if the base document has changed since you read it.

Pass an inline document when there is no published edition to copy from, for example a product you are introducing in this release:

_type is required. The version's _id is derived from the release and published IDs, so you don't need to set it. Calling createVersion() with an inline document logs a console warning recommending baseId, which you can disregard when the document has no published edition to copy from.

Mark a document for unpublishing

Use client.unpublishVersion() to mark a document for removal when the release runs. The document stays published until the release is executed.

Get a release and its documents

Retrieve a release's metadata with client.releases.get(), and list its documents with client.releases.fetchDocuments().

get() returns undefined when no release matches the ID, so guard the result before reading from it. The release's state is one of active, scheduling, scheduled, publishing, published, archiving, archived, or unarchiving.

Schedule a release

Schedule a release to publish at a specific time with client.releases.schedule(). Pass an ISO 8601 date string as the publishAt value.

Publish a release

To publish a release immediately instead of scheduling it, use client.releases.publish(). This is how you run a release created with the asap release type.

The new content is queryable as soon as the action returns. For larger releases, replacing the versions.<releaseId>.* documents with their published counterparts can take longer, and both the version and published documents are locked until that finishes.

Delete a release after publishing

After a release has been published, you can clean it up with client.releases.delete(). Delete accepts releases in the published or archived state, so check the release state first. To remove a release that is still active, use client.releases.archive(), which also deletes its document versions.

Full example: create and schedule a release

Here's a complete workflow that creates a release, adds document versions, and schedules it to publish:

Release actions with the Actions API

The mutating helper methods shown above use the client.action() method under the hood. If you need more control, you can dispatch release actions directly. This lets you archive, unarchive, and unschedule releases, as well as create, discard, replace, and unpublish individual document versions, among other operations.

For example, to archive and then unarchive a release:

You can also manage individual document versions through actions:

For the full list of available action types and their options, see Mutate documents with actions.

Next steps

Was this page helpful?