Sanity ships its content backend (the “Content Lake”) as a managed service — there is no self-hostable version of the lake. But the Studio (the editor UI) is a JavaScript app you build and host wherever you like. This article covers what “self-hosting Sanity” actually means today: you host the Studio (a static SPA), Sanity hosts the data, and the operational decisions sit around authentication, custom domains, CORS, and how the Studio talks to your frontend’s preview path.
How to verify
A correctly self-hosted Studio shows:
curl -sI https://studio.example.org/ | head -1
# HTTP/2 200
# The Studio loads and connects to the Content Lake:
curl -s "https://<projectId>.api.sanity.io/v2024-01-01/projects/<projectId>" \
-H "Authorization: Bearer <token>" | jq '.metadata.cors[]'
# (must include https://studio.example.org)
# The frontend reads from the lake:
curl -s "https://<projectId>.api.sanity.io/v2024-01-01/data/query/production?query=*%5B_type%3D%3D%22post%22%5D%5B0%5D" \
| jq '.result.title'
# "Hello"
If the Studio loads but you see “Authentication failed”, the CORS origin isn’t in the project’s allow-list — fix that in the Sanity manage console before anything else.
What’s happening
A Sanity project is a Content Lake (a managed Postgres-like store with a query language called GROQ), a set of datasets (think branches: usually production and development), and a Studio config that describes the schema. The Studio is a Vite-built SPA — sanity dev runs it locally, sanity build outputs a static dist/ directory you host on any HTTP server. There is no Studio backend; it talks directly to *.api.sanity.io from the browser.
Authentication for editors happens through Sanity’s auth — they sign in with Google/GitHub/email-link, the lake checks they have a role on your project, the Studio gets a session token. None of that needs your infrastructure. What you control is: which domain the Studio loads from, what CORS origins are allowed, and whether the project is configured with an SSO provider (SAML for enterprise plans).
The reason teams self-host the Studio instead of using sanity deploy (which puts it on <projectName>.sanity.studio) usually comes down to one of three things: brand domain (studio.example.org looks better than acme.sanity.studio); CSP / SSO requirements that need the Studio on a domain you control; CI/CD discipline (Studio version pinned to a commit in your monorepo). All three are operational reasons, not feature reasons.
The procedure
-
Initialize the Studio in your repo. Usually as a sibling to your Next.js frontend:
npm create sanity@latest studio -- --template clean --dataset production cd studioYou’ll be prompted for a project ID; either reuse an existing project or create a new one from the prompt.
-
Define schemas in
studio/schemas/. Each is a TypeScript file exporting a schema descriptor:// studio/schemas/post.ts export default { name: 'post', type: 'document', title: 'Post', fields: [ { name: 'title', type: 'string', validation: r => r.required() }, { name: 'slug', type: 'slug', options: { source: 'title' } }, { name: 'body', type: 'array', of: [{ type: 'block' }] }, ], }; -
Build the static assets.
cd studio npm run build # outputs dist/ as a static SPA -
Host it. Three production-grade paths:
- As an Astro/Next.js route in your existing site: copy
dist/into your frontend’s public dir under/studio/and ship it on your existing CDN. - As a standalone static site behind your CDN: deploy the
dist/to S3 + CloudFront, or to Vercel/Netlify/Cloudflare Pages as a static project. - Behind Nginx on your own VM, served as a static directory.
In all three, the URL path becomes the “Studio URL” — write it down because Sanity’s CORS allow-list needs it.
- As an Astro/Next.js route in your existing site: copy
-
Configure CORS in the Sanity console. Manage > API > CORS origins. Add
https://studio.example.organdhttps://example.org(the frontend reads the same lake too). Set “Allow credentials” only on the Studio origin. -
Set up auth. For most teams, the default Google/GitHub/email login is fine. For enterprise: invite editors as members of the project in manage.sanity.io and (optionally) enable SAML SSO from the project Settings > Access page.
-
Pin the Studio version in CI. Don’t let CI auto-upgrade
sanitybetween builds — the Studio config schema occasionally has breaking changes between minors. Lock inpackage-lock.jsonand bump deliberately. -
Wire a deploy hook to invalidate the Studio CDN cache on every push to main. The Studio is a static SPA — visitors who already have it cached will keep running the old version for a while otherwise.
Operational notes
- The Sanity dataset (
production,development) is global per project — there is no separate “Studio dataset” and “frontend dataset”. Editors who switch datasets in the Studio change what your frontend reads if you don’t pin it in the client config. - The Content Lake has an API CDN (
<projectId>.apicdn.sanity.io) and a non-CDN endpoint (<projectId>.api.sanity.io). Frontends read from the CDN; the Studio writes through the non-CDN endpoint. Confusing the two leads to stale reads in the Studio. - Sanity tokens scope to a dataset and a permission level — never put a write token in browser code. The Studio uses session auth, not tokens.
- The “Vision” plugin lets editors run GROQ queries in the Studio — disable it on production Studios; it has full read access to whatever the logged-in user can see, which is “everything” for an editor.
- Custom CSP on the host domain must allow
*.sanity.iofor the Studio to call the API. Block that and the Studio shows a blank page with a single console error.
For teams that want their editorial UI on a brand domain with the deploy in CI, Stack Harbor operates the Studio host — CORS origins audited, Studio version pinned, CDN invalidations wired to release, SSO documented in the runbook — so the editorial team only sees their domain. That model lives under managed operations, with related Tina and Storyblok notes alongside.