Getting latest draft via HTTP API

1 replies
Last updated: Jun 14, 2023
Hey all. Is there a way using the HTTP API to get the latest draft of some content? I can't seem to find any documentation on getting drafts through the HTTP API.
AI Update

Absolutely! You can get draft content through the HTTP API using the perspective parameter. Sanity has built-in support for this via their Perspectives feature.

Here's how it works:

Using the previewDrafts perspective

The easiest way to get the latest draft is to use the previewDrafts perspective, which prioritizes draft documents over published versions:

/v2025-02-19/data/query/YOUR_DATASET?query=*[]&perspective=previewDrafts

This will return draft versions when they exist, and fall back to published content when no draft is available.

Available perspectives

You have several options depending on your needs:

  • previewDrafts - Returns drafts when available, falls back to published (perfect for preview environments)
  • published - Returns only published content (default as of API version 2025-02-19)
  • raw - Returns all documents including both drafts and published versions
  • drafts - Returns content as if all drafts were published (useful for preview)

Important notes

  1. CDN caching: When using draft perspectives, you must set useCdn: false in your client configuration or avoid the CDN in your HTTP requests, since draft content should never be cached
  2. Authentication: You'll need an authenticated request (with a valid token) to access draft content
  3. Draft ID pattern: Draft documents have IDs prefixed with drafts. (e.g., drafts.post-123 vs post-123)

Using with the JS/TS client

If you're using @sanity/client, you can configure it like this:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  useCdn: false, // Required for drafts
  perspective: 'previewDrafts',
  token: 'your-token' // Required for authentication
})

This is similar to how other headless CMSs handle drafts (like Contentful's preview API, Strapi's publicationState=preview, or Storyblok's version=draft), but Sanity's perspective system is more flexible and works consistently across all queries.

Check out the full Perspectives documentation and the guide on presenting and previewing content for more details!

Show original thread
1 reply
You can include a token in your request and also update your query if you want to get drafts specifically:
https://www.sanity.io/docs/http-auth
https://www.sanity.io/docs/drafts#ebfb408fa9b6

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.

Was this answer helpful?