Sanity query returns no documents despite data being present in Studio

17 replies
Last updated: Jan 11, 2023
I’m having an issue where data is not being returned from a query that is present in our studio. The vision tool returns the data properly from within the studio. When using the sanity client however, no documents are returned for the given type. I confirmed both the project ID and dataset are correct. Is there any way to debug this further? I don’t seem to be getting any error messages or anything of that sort. I also confirmed with a
*
query to return all possible documents and attempted to filter it using javascript rather than the query, the documents are still not present so it does not appear to be an issue with the query. In both dev and deployed environments, the data is present in the studio. Other documents and data types other than this one are returned properly. I confirmed the documents are published. I have edited various documents of this type multiple times from different deploys and re-published them to no avail. Also confirmed that useCdn is set to false. Generated a fresh API key, still persists. Confirmed it is in the CORS URLs as well. No console errors in either studio or page that attempts to retrieve data.
AI Update

This sounds like a classic API perspective mismatch issue. Since Vision returns your documents but your client doesn't, and you've confirmed the documents are published, the most likely culprit is the API perspective setting.

The Root Cause: API Version Default Change

If you're using API version v2025-02-19 or later, the default perspective changed from raw to published. This is a breaking change that catches many developers off guard.

Here's what's likely happening:

  • Vision tool uses the raw perspective by default (shows all documents including drafts)
  • Your client is using the newer API version which defaults to published perspective
  • Even though you published the documents, there might be a perspective configuration issue

How to Fix It

Option 1: Explicitly Set the Perspective in Your Client

Add the perspective parameter to your client queries:

const client = sanityClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  apiVersion: '2025-02-19',
  useCdn: false,
  perspective: 'raw', // or 'previewDrafts' or 'published'
})

Or set it per-query:

const result = await client.fetch(query, params, {
  perspective: 'raw'
})

Option 2: Check Your API Version

If you're using an older API version string in your client config (like '2021-03-25'), try updating to the latest version and explicitly setting the perspective.

Additional Debugging Steps

Since you've already ruled out many common issues, try these:

  1. Check the actual API request: Open your browser's Network tab and inspect the actual API request being made. Look at the URL parameters - you should see ?perspective=published (or similar). Compare this to what Vision is using.

  2. Test with Vision's perspective: In Vision, there's a perspective dropdown (usually in the top-right). Try switching it to published and see if your documents disappear. If they do, that confirms the perspective issue.

  3. Verify document IDs: Use Vision with the raw perspective and check if your documents have drafts. prefixes. Even "published" documents might only exist as drafts if something went wrong during publishing:

*[_type == "yourType"] { _id, _type }

Look for IDs like drafts.document-id vs document-id. If you only see the drafts. versions, that's your problem - they were never actually published, just saved as drafts.

  1. Check for document-level permissions: If you're using custom access control, verify that your API token has read permissions for these specific documents.

  2. Try a different API version: Temporarily set your client to use apiVersion: '2021-03-25' to see if the behavior changes (this version defaults to raw perspective).

Why This Happens

Sanity uses perspectives to provide different views of your content. The published perspective only returns documents without the drafts. prefix, while raw returns everything. If there's any inconsistency in how your documents were published, or if you're comparing different perspectives, you'll see this exact symptom.

The fact that other document types work fine suggests this particular type might have publishing workflow issues or was created/modified in a way that affected its published state differently.

The Quick Test

Add this to your client code and see if your documents appear:

const result = await client.fetch(
  '*[_type == "yourType"]',
  {},
  { perspective: 'raw' }
)

If they show up with perspective: 'raw' but not with perspective: 'published', you've confirmed the issue - your documents exist only as drafts despite appearing published in the Studio.

Show original thread
17 replies
If i attempt to run the query from the command line I get an error:
Error: Failed to run query:
An unexpected error occurred
    at Object.action (~/****/node_modules/sanity/lib/_chunks/_internal-ca5bebdb.js:1491:13)
Which is also no help. This is node v18.12.1
I get this error on both our staging and production datasets
ok, running the query from the studio directory resolves the error and returns the data correctly
the error was previously a 500 internal server error
the sanity client still does not return the data however in our web app
confirmed this issue on a separate dev machine as well to rule out local configuration
Given that other data shows up, what’s most likely happening is the documents are drafts and your client is not picking up on the token you’re providing. If you’re using an env to obscure your token, I’d start by checking there.
It could also be that your client is using a different API version than the one that Vision uses. If you haven’t specified an API version, your client will default to
v1
, which is lacking features of
v2021-03-25
(which is what Vision defaults to).
If you’re using an env to obscure your token, I’d start by checking there.
I’m not sure where the token actually comes into play here now that I’m looking more closely. The data is retrieved server side and I set up the client according to this: https://www.sanity.io/docs/connect-your-content-to-next-js
I set both vision and the client to
2022-10-21
manually
and verified that vision still returns the correct results
Also when I tested the CLI I set this version using the
--api-version
flag
I confirmed that the documents are not drafts also - I edited a few and re-published them. All of them appear to be published to begin with
ok so adding a
token
property to the API config did return the documents now. But why weren’t they there before if they are published?
OK got it - the person that imported this data put dots in the IDs
I believe that is the issue at least?
Ah, yes. If a document is on a non-root path (like
drafts.
or anything else with a
.
) it will only be queryable if you make an authenticated request.
ok thanks for the clarification

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?