Issue accessing drafts from Astro SSR on Vercel with Sanity client token.
Looking at your setup, the issue is that the Contributor role doesn't have sufficient permissions to read drafts. According to the roles documentation, the Contributor role only has "create" mode access, which allows reading and writing to draft content but cannot read published documents or use the previewDrafts perspective.
Here's what's happening:
Contributor permissions:
- Mode:
create(can create/edit drafts but limited read access) - Cannot use
perspective: 'previewDrafts' - Cannot read documents with periods in their IDs (like
drafts.)
Why it works in dev mode: In development, you're likely authenticated with your personal user account (which probably has Administrator or Editor role), so you have full access.
Solution options:
Use a Viewer or Editor token (recommended for preview):
- Viewer role: Can read all documents including drafts with
mode: 'read'andhistory: true - Editor role: Full publish permissions including draft access
Create a new API token in Sanity Manage with Viewer or Editor role instead of Contributor.
- Viewer role: Can read all documents including drafts with
Add the
perspectiveparameter to your client config:const sanityAuthClient = sanityClient.withConfig({ token: import.meta.env.SANITY_API_TOKEN, useCdn: false, perspective: 'previewDrafts', // This requires Viewer+ role });For Astro SSR on Vercel specifically, you'll want to:
- Use a Viewer token (read-only is fine for preview)
- Set
useCdn: false(you already have this ✓) - Add
perspective: 'previewDrafts'to prioritize drafts over published - Optionally set
apiVersionto match your dataset
Quick fix: Go to your project's API settings, create a new token with Viewer role, and use that instead. Viewer tokens can read both drafts and published documents, which is exactly what you need for preview functionality on Vercel.
The Contributor role is designed for users who can create/edit content but shouldn't necessarily see everything - it's more restrictive than you need for a preview environment. For accessing drafts via the API (especially with perspectives like previewDrafts), you need at least Viewer-level permissions.
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.