Frontend vs Backend Proxying for Sanity API Requests
For published, public content, query Sanity directly from the frontend through the API CDN. That's the normal pattern and it doesn't need a proxy. Put your backend in front of Sanity only when a request needs a token, meaning private datasets, drafts or writes, or when you need to filter or combine data or cache it yourself.
Reading published content directly
- Use the API CDN. Set
useCdn: trueand the client reads fromapicdn.sanity.io, which is the recommended setup for frontends serving end users. Cached responses aren't rate limited. Cache misses go to the live API, which has limits: 500 concurrent queries per dataset and 500 API calls per second per IP. Going over a limit returns429(API CDN).
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'production',
apiVersion: '2025-01-01', // pin an explicit API version
useCdn: true,
})
const posts = await client.fetch('*[_type == "post"]{title, slug}')- Use a public dataset and send no token. Anyone can read a public dataset without authenticating. An unauthenticated request can only see documents whose
_idhas no dot in it, so drafts (drafts.…) and release versions (versions.…) are never returned, whatever perspective the request asks for (Keeping your data safe). - Allow your origins with CORS. Add each frontend domain in Manage under API › CORS origins. For a single-page app that reads content directly, you usually want to disallow credentials. CORS only applies to browsers, not to server-side requests (CORS and browser security).
When to go through your backend
- Private datasets or drafts. These need a token. Don't put a token in JavaScript served to the browser: everyone can read it, so a private dataset effectively becomes public, and a write token lets anyone change or delete your data. The documented options are to serve that JavaScript only to authorized users, or to have your backend fetch from Sanity and pass on only data that's safe to share (Keeping your data safe). For server-side reads, create a token with Viewer (read-only) permissions in Manage under the API tab and keep it in a server-only environment variable (token handling).
- Writes from a frontend. Send them to a small proxy server or cloud function that validates the data, turns it into a Sanity document and submits it with a
@sanity/clientconfigured with a write token (Keeping your data safe). Sanity Functions are a different tool: they run when content changes (create, update, delete, schedules), not when a client calls them. Use them for follow-up work such as enrichment or cache invalidation, not as your write endpoint. - Business logic, merging with other services, or your own caching layer. Any of these is a reason to route reads through your API.
Applied to your three apps
- End-user app: query the API CDN directly against a public dataset, with no token. If the content is private or paywalled, read through your backend instead.
- Admin app: if it's Sanity Studio, editors log in and Studio uses their own credentials, so you don't ship a token. If it's a custom app, send its token-requiring reads and all its writes through your backend.
- Backend service: hold the tokens there, with Viewer for reads and write permissions only where needed. Validate input before mutating, and batch mutations into transactions.
@sanity/clientretries rate-limited queries but not mutations (API CDN).
Images and other asset files aren't private, even in a private dataset, so don't rely on dataset visibility to protect them.
Show original thread2 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.