
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeWhen you're working with multiple datasets in Sanity (like one public and one private), you need to specify which dataset to query by configuring it in your Sanity client. By default, the client uses whatever dataset you specified when you created it, so to query your private dataset, you'll need to either:
1. Create a separate client instance for your private dataset:
import { createClient } from '@sanity/client'
// Client for public dataset
const publicClient = createClient({
projectId: 'your-project-id',
dataset: 'public', // your public dataset name
apiVersion: '2024-01-01',
useCdn: true,
})
// Client for private dataset
const privateClient = createClient({
projectId: 'your-project-id',
dataset: 'private', // your private dataset name
apiVersion: '2024-01-01',
useCdn: false, // don't use CDN for private data
token: 'your-auth-token', // required for private datasets
})
// Now query the private dataset
const data = await privateClient.fetch('*[_type == "article"]')2. Or use the same client with the .withConfig() method:
const client = createClient({
projectId: 'your-project-id',
dataset: 'public',
apiVersion: '2024-01-01',
})
// Query private dataset by overriding the dataset config
const privateData = await client
.withConfig({
dataset: 'private',
token: 'your-auth-token'
})
.fetch('*[_type == "article"]')Important considerations:
useCdn: false) when querying private datasets, as the CDN caches responses and could expose private data.The dataset is specified at the client configuration level, not within the GROQ query itself. The GROQ query syntax remains the same regardless of which dataset you're querying—you just need to point your client at the correct dataset.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store