How to authenticate Sanity client to fetch unpublished drafts in next-sanity?
Great! I can see you're using the real-time preview features. To access draft content with client.fetch, you need to set the correct perspective parameter on your Sanity client. The previewDrafts perspective is what you're looking for.
When you configure your Sanity client, you can set the perspective to previewDrafts which will include draft content in your queries. Here's how to do it:
import { createClient } from 'next-sanity'
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
apiVersion: '2024-01-01',
useCdn: false, // Important: disable CDN for draft content
perspective: 'previewDrafts', // This includes drafts
token: process.env.SANITY_API_READ_TOKEN, // Required for draft access
})The key points are:
- Set
perspective: 'previewDrafts'- This tells the client to return draft content when available, falling back to published content when no draft exists - Provide a
token- Draft content requires authentication, so you need to pass a read token (you can create one in your Sanity project settings) - Disable CDN (
useCdn: false) - The CDN doesn't serve draft content, so you need direct API access
If you're using the modern Live Content API with defineLive, you can pass the token in your configuration:
import { defineLive } from 'next-sanity/live'
export const { sanityFetch, SanityLive } = defineLive({
client,
serverToken: process.env.SANITY_API_READ_TOKEN,
browserToken: process.env.NEXT_PUBLIC_SANITY_API_READ_TOKEN,
})Then ensure your client has the previewDrafts perspective set, especially when working with Draft Mode in your Next.js app.
Note about API versions: As of the recent API version 2025-02-19, the default perspective changed to published, so explicitly setting the perspective is now more important than ever if you want to see draft content.
The available perspectives are:
published- Only published documents (now the default)previewDrafts- Drafts when available, otherwise publishedraw- All documents including drafts and published versions
Show original thread24 replies
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.