Private dataset with read token returning drafts - how to exclude them?
Great question! You're on the right track with making your dataset private and using a read token, but you need to configure the perspective option in your Sanity client to exclude drafts from your production app.
When you use a token (even a read token), by default the API may return both drafts and published documents. The key is to set the perspective option to 'published' when configuring your Sanity client. This tells Sanity to only return published documents and exclude any draft versions.
Here's how to configure your client:
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
apiVersion: '2025-02-19', // use current date or latest API version
token: 'your-read-token', // your private read token
perspective: 'published', // This is the key! Only returns published docs
useCdn: true, // can still use CDN with published perspective
})The perspectives feature gives you different views of your content:
published- Returns only published documents (what you want for production!)previewDrafts- Prioritizes drafts over published versions (great for preview environments)raw- Returns everything, both drafts and published
For your use case, you should:
- Keep your dataset private ✅
- Use a read token in your client ✅
- Set
perspective: 'published'in your production client ✅
If you also have a preview environment where editors need to see draft changes, you can create a separate client configuration with perspective: 'previewDrafts' for that environment specifically.
Note: With the recent API version 2025-02-19, published is now the default perspective, but it's still good practice to explicitly set it in your client configuration for clarity.
This way you get both security (private dataset with token authentication) AND clean production data (no drafts)!
Have a great weekend! 🎉
Show original thread2 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.