Query returns no results in Google Script but works elsewhere

31 replies
Last updated: May 20, 2021
Any ideas why my query is returning no results when called from a google script, but returns results otherwise?
AI Update

This is likely a perspective issue - specifically related to a recent breaking change in Sanity's API. Let me explain what's probably happening:

The Most Likely Culprit: API Perspective Default Change

As of the 2025-02-19 API version, Sanity changed the default perspective from raw to published. This is a breaking change that catches a lot of people off guard.

Here's what's probably happening:

  1. Your Google Script is likely using a newer API version (or no specified version, defaulting to latest) which uses the published perspective
  2. Your other environment (Studio, local dev, etc.) might be using an older API version or explicitly setting perspective=raw

The published perspective only returns documents without the drafts. prefix - meaning if your content is still in draft state and hasn't been published, it won't show up.

How to Fix It

Add the perspective parameter to your Google Script API calls:

// Add ?perspective=previewDrafts to your query URL
const url = `https://${projectId}.api.sanity.io/v2021-10-21/data/query/${dataset}?query=${encodeURIComponent(query)}&perspective=previewDrafts`;

Or if you want to see everything (drafts + published):

// Use perspective=raw to see all documents
const url = `...&perspective=raw`;

Other Potential Issues

While perspective is the most likely cause, here are other things to check:

Authentication: Make sure your Google Script has the right token with read permissions for your dataset

API Version: Check if you're specifying an API version in your URL (like v2021-10-21 or v2025-02-19)

CORS/Network: Though less likely with Google Scripts, ensure the request is actually reaching Sanity (check response status codes)

Query syntax: Test your exact query in Sanity's Vision plugin in your Studio to verify it returns results there

The perspectives documentation has more details about how these different views work and when to use each one.

Show original thread
31 replies
One thing that I could think of is if the dataset is set to private.
You can change between public (everyone can read) or private (need to be authed or have token to read)
It's definitely public, you can put that url in your address bar and it works
Nope, not for me
You probably have a token stored in your browser
oh you're right.. it doesn't work in a private window
didn't realise it would use your logged in session
but yeah..
Weird, I tested to set one of my datasets to private and the kept the other public and got the same result as you describe.. unfortunately that was my only idea 😕
Are your documents published? Do the
_id
attributes contain a
.
by any chance?
not sure what it means for a document to be published.. I've only been using the data lake so far
and the ids do have `.`s
I'll have a read up on document publishing
Ah. Common gotcha - https://www.sanity.io/docs/ids
Dots are treated as "paths", and the default security rules says that anything not within the "root path" is not public to unauthenticated users
So
foo.bar
is "private", while
foobar
is public. This is the way drafts work when you use the studio: they are documents with an ID prefix:
drafts.someDocumentId
ah, I think I read that page at some point but it didn't sink in. I thought using paths in my ids was just going to give me more flexibility down the line
no big deal, token auth it is
👍
thanks for the help!
no big deal, token auth it is
👍
Weird, I tested to set one of my datasets to private and the kept the other public and got the same result as you describe.. unfortunately that was my only idea 😕
Are your documents published? Do the
_id
attributes contain a
.
by any chance?
and the ids do have `.`s
not sure what it means for a document to be published.. I've only been using the data lake so far
I'll have a read up on document publishing
Ah. Common gotcha - https://www.sanity.io/docs/ids
Dots are treated as "paths", and the default security rules says that anything not within the "root path" is not public to unauthenticated users
So
foo.bar
is "private", while
foobar
is public. This is the way drafts work when you use the studio: they are documents with an ID prefix:
drafts.someDocumentId
ah, I think I read that page at some point but it didn't sink in. I thought using paths in my ids was just going to give me more flexibility down the line
👍
Happy to help!

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?