
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeThe good news is that you're on the right track! When switching to a private dataset, you do need to use a token, which you've already added. Here are the key things to check:
Make sure your Sanity client configuration includes the token. In your Next.js app, this typically looks like:
import { createClient } from 'next-sanity'
export const client = createClient({
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
apiVersion: '2024-01-01',
useCdn: true, // This works fine with tokens!
token: process.env.SANITY_API_READ_TOKEN,
})Important note about useCdn: You CAN use useCdn: true with tokens for private datasets. Since JavaScript Client v3.0.0+, the Sanity CDN supports authenticated requests with caching segmented per token. Setting it to false will work, but isn't required and you'll miss out on the CDN's performance benefits.
This is crucial - where you use the token matters for security:
SANITY_API_READ_TOKEN without the NEXT_PUBLIC_ prefix. This keeps the token secure and only accessible in server components, API routes, and server actions.NEXT_PUBLIC_SANITY_API_READ_TOKEN, but this exposes your token publicly.Your .env.local should look like:
NEXT_PUBLIC_SANITY_PROJECT_ID=your-project-id NEXT_PUBLIC_SANITY_DATASET=your-dataset-name SANITY_API_READ_TOKEN=your-token-here
If you're using the token without the NEXT_PUBLIC_ prefix (which you should be), make sure you're fetching data in server components or API routes, not client components. In Next.js App Router, components are server components by default unless you add "use client" at the top.
Make sure your token has at least Viewer permissions for your dataset. You can check this in Sanity Manage:
npx sanity@latest manage from your command lineAfter adding or changing environment variables, you must restart your Next.js development server for the changes to take effect. Stop your server and run npm run dev (or yarn dev) again.
If you're seeing content on localhost with a public dataset but not with a private one, it's often because your fetching code is running client-side where the SANITY_API_READ_TOKEN environment variable isn't available (since it doesn't have the NEXT_PUBLIC_ prefix).
Check if you have "use client" in the file where you're fetching data. If so, you'll need to either:
NEXT_PUBLIC_SANITY_API_READ_TOKEN if you absolutely need client-side accessAfter checking these items, your private dataset content should appear! Let me know if you're still having issues.
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