Different results between curl query and Sanity client fetch methods

8 replies
Last updated: Jan 28, 2026
Hello! Is there any reason I would get different results between these two methods of querying data?

curl <https://xuaf19yr.api.sanity.io/v1/data/query/production>\?query\=\*%5B_type%20%3D%3D%20%22articlePage%22%5D%20%7B%0A%09...%0A%7D

const sanity = require('@sanity/client')

const sanityClient = sanity({
  projectId: 'xuaf19yr',
  dataset: 'production',
  useCdn: false,
})

await sanityClient.fetch(`
  *[_type == "articlePage"] {
    ...
  }
`)
Any help appreciated!
AI Update

Yes! The most likely reason you're getting different results is due to API versioning and perspective differences between your two query methods.

The Key Differences

1. API Version Default

Your curl request is hitting the API directly without specifying an apiVersion, which means it defaults to v1 of the API. However, your @sanity/client code also doesn't specify an apiVersion, which similarly defaults to v1 - but there's a catch with how the client behaves.

2. Perspective Default (Most Likely Culprit)

The bigger issue is likely the perspective. As of API version 2025-02-19, the default perspective changed from raw to published. However, when you use an authenticated token with @sanity/client, the behavior can differ:

  • Direct HTTP API calls (your curl): Returns only published documents by default
  • @sanity/client with token: May return different results depending on token permissions and configuration

The Solution

Make your @sanity/client configuration explicit to ensure consistent behavior:

const sanity = require('@sanity/client')

const sanityClient = sanity({
  projectId: 'xuaf19yr',
  dataset: 'production',
  useCdn: false,
  apiVersion: '2025-02-19', // Always specify this!
  perspective: 'published',  // Explicitly set perspective
})

await sanityClient.fetch(`
  *[_type == "articlePage"] {
    ...
  }
`)

Why This Matters

Without specifying apiVersion and perspective:

  • You might be seeing draft documents mixed with published ones (if using the raw perspective)
  • GROQ operator behavior can differ between v1 and newer versions
  • The client might use different defaults than the direct HTTP API

The perspective setting controls which document versions you see:

  • published - Only published documents (recommended for production)
  • previewDrafts - Drafts if they exist, otherwise published
  • raw - Everything including both drafts and published

Always explicitly set both apiVersion and perspective in your client configuration to avoid surprises!

Show original thread
8 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.

Was this answer helpful?