Creating a dataset with public and private data in Sanity.io
Yes, absolutely! You can create a single dataset that contains both public and private documents, and control access so that:
- Public clients (your website/app) only see public documents
- Studio users see both public and private documents
This is achieved through Custom Roles (available on Enterprise plans) combined with GROQ filters and API tokens. Here's how it works:
Document-Level Access Control
You can use content resources with GROQ filters to define which documents are accessible. For example, add a field to your documents like isPublic: boolean, then create a custom role that filters documents:
// Custom role configuration
{
name: 'public-reader',
permissions: [
{
name: 'read-public-only',
filter: '_type == "article" && isPublic == true',
grant: ['read']
}
]
}This means:
- API tokens assigned this role can only query documents where
isPublic == true - Studio users with full permissions see everything
- Your public-facing website uses the restricted token, while Studio access remains unrestricted
Important Considerations
Dataset Privacy: Set your dataset to private (not public). Public datasets allow anyone to read all documents regardless of authentication, which would bypass your access control.
API Tokens: Create different API tokens with different role assignments:
- One with the restricted "public-only" role for your frontend
- One with full access for your Studio/backend operations
Roles are Additive: If a token has multiple roles, it gets the combined permissions of all roles.
Alternative: Separate Datasets
If you're not on an Enterprise plan (Custom Roles are Enterprise-only), you would need to use separate datasets instead. You can configure multiple datasets using workspaces and have your frontend only query the public dataset while Studio users can switch between both datasets.
The single-dataset approach with Custom Roles is generally more flexible and easier to manage, especially if content needs to transition between public and private states. With separate datasets, you'd need to manually copy or recreate documents when changing their visibility status.
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.