Joint session with Vercel: How to build intelligent storefronts (May 15th)

Differentiate unpublished vs published draft in Sanity API

7 repliesLast updated: Nov 29, 2025

Is there a way to differentiate between an
unpublished
document and a
published draft
from the sanity api?
AI Update

Yes! You can differentiate between an unpublished document and a published draft using perspectives and document IDs.

The key distinction is:

Using Perspectives

The most straightforward approach is to use the published perspective when querying. This returns only published documents:

const client = createClient({
  // ... config
  perspective: 'published'
})

With this perspective:

Checking in Your Query Results

You can also check programmatically by looking at the _id:

GROQ Queries for Detection

To find only unpublished documents (never been published):

*[_id in path("drafts.**") && count(*[^._id == "drafts." + _id]) == 0]

To find published documents with draft changes:

*[count(*[_id in [^._id, "drafts." + ^._id]]) > 1]

To exclude all drafts from production queries:

*[_type == 'post' && !(_id in path("drafts.**"))]

The previewDrafts perspective is also useful for preview environments - it automatically prioritizes drafts over published versions when both exist, which is perfect for showing the latest changes before they go live.

Show original thread
7 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Related contributions