Can a single Sanity dataset contain both public and private documents?
Yes, one dataset can hold both. The approach that works on every plan is to give private documents an _id that contains a dot, such as contacts.abc123. In a public dataset, unauthenticated requests can only read documents whose ID has no dot. Your website, querying without a token, sees only the public documents. Studio users are logged in, so they see everything. Drafts (drafts.<id>) are kept private the same way. On Enterprise you can also use custom roles with GROQ-filtered content resources.
Option 1: Dotted document IDs (any plan)
From IDs and paths: any ID containing . is private, and reading it requires a logged-in user or a token with at least read access. Published IDs at the root (no dot) are public.
Studio generates random UUIDs for new documents and doesn't let you choose the ID, but the API and clients can set a custom _id. Create private documents from code, for example in a server-side function:
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'production',
apiVersion: '2025-01-01', // use a current date
token: process.env.SANITY_WRITE_TOKEN, // server-side only
useCdn: false,
})
await client.create({
_id: `contacts.${crypto.randomUUID()}`,
_type: 'contact',
email: 'someone@example.com',
})Your frontend queries without a token and never gets contacts.* documents. In GROQ, authenticated code can select them with *[_id in path("contacts.*")].
Caveats from the docs:
- Don't create IDs starting with
drafts.orversions.. Sanity uses those prefixes internally. - Never put personal or sensitive data in the ID itself. Every ID ever written is kept until the dataset is deleted.
- Asset files (images and files) can be viewed without authentication, even in private datasets (Keeping your data safe).
- If you need Studio to show or edit these documents, test how they appear in your Structure setup.
Option 2: Private dataset plus a custom role (Enterprise)
Custom roles and content resources are Enterprise-only. A content resource is a set of documents defined by a GROQ filter, for example _type == "article" && isPublic == true. You create resources under Access > Resources and roles under Access > Roles in sanity.io/manage, or with the Access API. Then assign the role to a robot token for your frontend.
Things to get right:
- Make the dataset private. In a public dataset, anyone can read every published document no matter what roles you set up.
- Give the token only the custom role. Roles add up. If the token also has Viewer or Editor, the restriction does nothing.
- Keep the token out of browser JavaScript. Anything served publicly can be read, token included. Query from a server or proxy you control. The filter limits which documents the token can read, not which queries it can run (
count()still works across everything it can read).
Option 3: Separate datasets
You can also use a public dataset and a private one, with Studio workspaces to switch between them. Each workspace can have its own schema, so the two datasets don't need to share one. The downside is that moving a document between public and private means copying it to the other dataset.
Plan notes (from the pricing page)
- Free: datasets are public only. Dotted IDs (Option 1) work here.
- Growth: allows private datasets.
- Enterprise: adds custom roles and access control.
Show original thread10 replies
Was this answer helpful?
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.