Skip to content

Query returns no results in Google Script but works elsewhere

31repliesLast updated: Nov 13, 2026

Any ideas why my query is returning no results when called from a google script, but returns results otherwise?
AI Update

This usually means the script's request isn't authenticated and the documents can't be read publicly. The URL works in your browser because you're logged in to Sanity, so the browser sends your session. Google Apps Script sends no credentials, so it only gets back what anyone could read. Open the same query URL in a private or incognito window: if it's empty there too, this is the cause.

Why documents are hidden from unauthenticated requests

Two things decide what an anonymous request can see:

Fix

Option A: send a token. Create a robot token under the project's Settings > API > Tokens (Viewer or read access is enough for queries) and send it with every request as an Authorization: Bearer <token> header, as described in Authentication and tokens:

curl -H "Authorization: Bearer <token>" \
  "https://<projectId>.api.sanity.io/v2025-02-19/data/query/<dataset>?query=*[_type=='myType']"

In Google Apps Script, add the same Authorization header to the request options of the HTTP call you already make. Store the token in the script's secret or property storage, not in shared code. Use a dedicated robot token rather than your personal token.

Option B: use IDs without dots. If the content should be public, give documents IDs without .. For example, use - or _ as separators (allowed characters are a-zA-Z0-9._-). IDs can't be changed, so this means creating the documents again with new IDs. If you need structured identifiers, Sanity suggests putting them in a regular schema field (for example customId) instead of the _id. Also, don't create IDs starting with drafts. or versions. yourself: those prefixes are used internally.

Once the request is authenticated: perspectives

With a token, the perspective decides which versions come back. From API version 2025-02-19 the default is published, which leaves out drafts and unpublished documents. Before that version the default was raw. If you also need drafts, add &perspective=drafts (formerly previewDrafts) or &perspective=raw. With @sanity/client, set useCdn: false when using drafts. Always put an explicit API version in the URL so the default doesn't change without you noticing.

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