How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Safe usage of Sanity write tokens in frontend vs backend

3 repliesLast updated: Nov 29, 2025

Hi all, one quick question.The "robot user token" with write access should never be used in frontend bundle (like gatsby). Only correct way to use the write token is to implement an API endpoint and hide the token behind that API. client code invokes the api and then api uses the write token to make modification in sanity.... is my understanding correct?
or is there some other safe way to use write tokens in "font-end-client-only" implementations?
AI Update

Your understanding is 100% correct! 🎯

Write tokens (including robot user tokens with write access) should never be included in frontend bundles like Gatsby, Next.js client components, or any client-side JavaScript. This is a critical security principle.

The proper approach is exactly what you described:

This pattern works great with:

Why this matters: If you expose a write token in your frontend bundle, anyone can inspect your JavaScript, extract the token, and have full write access to your Sanity project. They could delete, modify, or create any content.

For read-only access: If you only need to fetch published content, you can use a read-only token (Viewer role) directly in the frontend, though many projects work fine without any token for public content. For accessing draft content in preview mode, you'd still want that token server-side or at least carefully scoped.

The API tokens documentation covers token security best practices in detail, and the principle of least privilege applies - only give tokens the minimum permissions they need for their specific use case.

Show original thread
3 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.

Related contributions