How to exclude query attribute from Sanity HTTP API response?

2 replies
Last updated: May 15, 2024
Is it possible to disclude the
query
attribute in the HTTP API result? (https://www.sanity.io/docs/http-query#6qMYj0OG ). This makes the response payload very large and uncachable in Next.js
AI Update

Yes! You can exclude the query attribute from the HTTP API response by using the returnQuery parameter.

According to the Sanity HTTP Query API documentation, there's a query parameter called returnQuery that defaults to true. You can set it to false to omit the query from the response payload.

How to use it:

For GET requests:

https://{projectId}.api.sanity.io/v{apiVersion}/data/query/{dataset}?query=*[_type == 'movie']&returnQuery=false

For POST requests:

{
  "query": "*[_type == 'movie']",
  "returnQuery": false
}

Wait, actually looking at the documentation more carefully, I see that returnQuery is listed as a query parameter for the GET endpoint, but for POST requests you'd still pass it as a query parameter in the URL, not in the request body:

POST https://{projectId}.api.sanity.io/v{apiVersion}/data/query/{dataset}?returnQuery=false

With request body:

{
  "query": "*[_type == 'movie']"
}

Why this helps with Next.js caching:

By setting returnQuery=false, your response payload will be smaller and more consistent. The response will only include:

  • ms (processing time)
  • result (your actual query results)
  • syncTags (for cache invalidation)

This makes the response more cacheable because the payload is smaller and doesn't include the potentially large query string that was echoing back to you. This is especially helpful when you're using Next.js's caching mechanisms or the Sanity CDN endpoint (apicdn.sanity.io), as smaller, more predictable responses cache better.

Show original thread
2 replies
I checked the API request parameters in the codebase, and I’m afraid there doesn’t appear to be anything that would let you exclude
query
in the response.
user B
I was wrong. I just came across the
returnQuery
query parameter. When set to false, it doesn’t return the query.
I’ll file this with the Docs team to have it added to the documentation.

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?