Query returns no results in Google Script but works elsewhere
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:
- Your Google Script is likely using a newer API version (or no specified version, defaulting to latest) which uses the
publishedperspective - 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 thread31 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.