How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Fetching drafts with previewDrafts perspective returns empty array

14 repliesLast updated: Nov 29, 2025

Hey all,
I have an issue with fetching drafts from Sanity, here is my code, it's pretty basic:

const clientConfig: ClientConfig = {
	dataset,
	projectId,
	apiVersion,
	useCdn: false,
	perspective: "previewDrafts",
};

const client = createClient(clientConfig);

const query = `*[ (_id in path('drafts.**')) ]`;

const data = await client.fetch(query);
I'm trying to get all drafts, if I run the same query inside Vision inside the Sanity studio, it does return 1 document. If I change the query to return non-drafts (e.g. *[]), the code also works fine, it's just drafts that it doesn't want to return (it returns an empty array, no errors). Could it be some permissions or set up issue?
AI Update

Hey! I can see what's happening here. The issue is actually related to authentication requirements for accessing documents with dots in their _id, which includes drafts.

According to Sanity's documentation on IDs and Paths, there's an important security rule:

Any document ID containing a dot (.) can only be accessed when authenticated. This includes all draft documents since they have IDs like drafts.your-document-id.

From the docs:

"All documents that contain a . in their _id can only be accessed when a user is logged in or a valid authentication token is provided for client and HTTP API calls (minimum read permission required)."

The Solution

You need to add an API token to your client configuration. Here's how to fix it:

const clientConfig: ClientConfig = {
  dataset,
  projectId,
  apiVersion,
  useCdn: false,
  perspective: "previewDrafts",
  token: process.env.SANITY_API_TOKEN, // Add this line
};

const client = createClient(clientConfig);
const query = `*[(_id in path('drafts.**'))]`;
const data = await client.fetch(query);

Why Vision Works But Your Code Doesn't

The Vision plugin in Sanity Studio works because you're already authenticated when logged into the Studio. Your external code, however, needs explicit authentication via a token to access any documents with dots in their IDs.

Additional Notes

This is a security feature to prevent unauthorized access to draft content and other internal documents.

Show original thread
14 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