Troubleshooting a POST GROQ request using JS Client in Next.js
Good news! The Sanity HTTP API does support POST requests for GROQ queries, which is exactly what you need when your query is too long for a GET request.
According to the Query API documentation, there's a specific endpoint for this:
Using POST for Large Queries
POST https://{projectId}.api.sanity.io/v{apiVersion}/data/query/{dataset}
The documentation notes that GET queries have an 11 KB limit due to browser URL length restrictions. When your query exceeds this, you need to use POST.
With the JS Client
Unfortunately, the @sanity/client doesn't have a built-in option to force POST requests. The client uses GET by default for cacheability. However, you have two good options:
Option 1: Use GROQ Parameters (Recommended first step)
Move large parts of your query into parameters to reduce the URL length:
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
apiVersion: '2025-02-19',
})
const result = await client.fetch(
`*[_type == $type && category in $categories]`,
{
type: 'post',
categories: ['tech', 'design', 'development'] // Large arrays go here
}
)GROQ parameters move data out of the URL and into the request body, which can significantly reduce your query string length.
Option 2: Direct HTTP POST Request
If parameters don't solve it, make a direct POST request to the HTTP API:
const query = `your very long GROQ query here`
const params = {} // optional parameters
const response = await fetch(
`https://${projectId}.api.sanity.io/v2025-02-19/data/query/${dataset}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
// Add if you need authentication:
// 'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
query: query,
params: params
})
}
)
const {result} = await response.json()The POST endpoint supports the same query options as GET and will also be cached on the CDN, so you don't lose performance benefits.
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.