Sanity
With Sanity, your content lives in a Sanity project instead of files, and your team edits it in Sanity Studio, an editor generated from your site’s content model. The site still builds into static pages: it reads the published content from Sanity when it builds, and a webhook starts a new build whenever someone publishes.
Every template in the files/Sanity variant (the default when you run wptom new) works with
Sanity; nothing in your pages changes. The examples below use Handy
(npx wptom new my-site -t handy); the steps are the same for every template.
What moves to Sanity: every page, the site settings, the interface texts, every collection (in
Handy: services, jobs, tips and legal pages) and their photos. What stays a file: the brand
(src/content/singletons/brand.json, colors and fonts), because the build needs it to set up
the fonts. Edit it in your project as before.
What you’ll set up
Section titled “What you’ll set up”| Variable | What it is | Needed |
|---|---|---|
CMS |
sanity to read content from Sanity; files (the default) for the local files |
Always, to switch |
SANITY_PROJECT_ID |
Your Sanity project’s id | Always |
SANITY_DATASET |
The dataset to read; production if empty |
Always |
SANITY_WRITE_TOKEN |
A token with the Editor role, used only to upload the demo content | On your computer, only for seeding |
SANITY_READ_TOKEN |
A token with the Viewer role | Only for a private dataset or for previews of drafts |
SANITY_PERSPECTIVE |
published (the default) or drafts |
Only for a preview deployment |
SANITY_CDN_URL |
Your own address for Sanity’s image CDN, instead of cdn.sanity.io |
Rarely; leave it out |
The Studio, in the studio/ folder, has its own .env with SANITY_STUDIO_PROJECT_ID,
SANITY_STUDIO_DATASET and SANITY_STUDIO_APP_ID (see Sanity Studio).
1. Create a Sanity project
Section titled “1. Create a Sanity project”- Sign in at sanity.io/manage (create an account if you need one) and create a new project.
- Open the project. Its Project ID is shown at the top of the page; copy it.
- Under Datasets, check the dataset. New projects come with one called
production; use it, or create another. Note whether it’s Public or Private: a public dataset can be read without a token, a private one needsSANITY_READ_TOKEN.
2. Create the tokens
Section titled “2. Create the tokens”In the project: API → Tokens → Add API token.
- Write token: name it (e.g.
Seed), choose the Editor permission and save. Copy the token now; Sanity shows it only once. It’s only used on your computer to upload the content. Never add it to your host. - Read token (only for a private dataset or a drafts preview): another token with the Viewer permission. It can only read, so it’s safe in your host’s settings.
3. Upload your content
Section titled “3. Upload your content”-
In your project folder, copy the example settings if you haven’t yet:
cp .env.example .env(Windows PowerShell:Copy-Item .env.example .env). -
In
.env, fill in the Sanity part:Terminal window SANITY_PROJECT_ID=abc123xySANITY_DATASET=productionSANITY_WRITE_TOKEN=sk... # the Editor token, keep it on your computerLeave
CMS=filesfor now: the upload reads your local files. -
Generate the Studio’s schema from the content model:
Terminal window npm run cms -- sanity schemaIt writes
studio/schema.jsonand prints the webhook filter you’ll need later (_type in ["home", "servicesPage", …]). The template already ships this file, so this step matters most after you change the content model; running it again is harmless. -
Upload all content and photos:
Terminal window npm run cms -- sanity seedIt ends with
Seeded … documents and … photos.
Seeding is for filling an empty dataset. If any of the documents already exist (published or as drafts), it stops and changes nothing, so edits made in Sanity are never lost by accident:
3 of these documents already exist in Sanity (e.g. …). Seeding would replace them and lose edits made there; add --force to replace them.To overwrite them with your local files anyway (documents with the same id are replaced; entries that exist only in Sanity are left alone):
npm run cms -- sanity seed --force4. Run Sanity Studio
Section titled “4. Run Sanity Studio”The Studio is a separate small project in the studio/ folder of your site.
-
Set it up:
Terminal window cd studiocp .env.example .env -
In
studio/.env, fill in your project:Terminal window SANITY_STUDIO_PROJECT_ID=abc123xySANITY_STUDIO_DATASET=productionSANITY_STUDIO_APP_ID= -
Install and start it:
Terminal window npm installnpm run dev -
Open the address it prints (
localhostwith port 3333 by default) and sign in with your Sanity account. If the Studio says the address isn’t allowed, add it in sanity.io/manage → your project → API → CORS origins → Add CORS origin, with Allow credentials ticked.
You should see your content, organised like the site. Single pages (home, the other pages, site settings, the interface texts) open directly; they exist once each, so the Studio doesn’t offer to create, duplicate or delete them. Collections list their entries. Photos can be cropped, and the crop shows on the site. The body of collection entries (articles, legal pages) is written in a Markdown editor.
Host the Studio
Section titled “Host the Studio”To let others edit without installing anything, host the Studio on Sanity:
cd studionpm run deployIf you aren’t logged in to Sanity on this computer, run npx sanity login first. The first
deploy asks you to pick a name, and the Studio goes live at https://<name>.sanity.studio.
It also prints an app id: put it in studio/.env as SANITY_STUDIO_APP_ID, so later deploys
don’t ask again.
Prefer to host the Studio yourself? npm run build builds it as static files into
studio/dist. Add the address you host it on to the project’s CORS origins, with credentials.
Invite editors
Section titled “Invite editors”In sanity.io/manage → your project → Members, invite people by email. They sign in to the Studio with their own Sanity account.
5. Run the site from Sanity locally
Section titled “5. Run the site from Sanity locally”-
In your site’s
.env(not the Studio’s), switch the source:Terminal window CMS=sanity -
Start the site (from the project folder, not
studio/):Terminal window npm run devWhile it starts, the terminal shows a line for each part it reads, such as
services: 6 from Sanity. A0means that part is empty in Sanity: check the project id and dataset, and that seeding finished. -
Open the site. It should look exactly as before.
-
Test the round trip: change a heading in the Studio and click Publish. Content is read when the site starts or builds, so stop
npm run dev(Ctrl C) and start it again: the new heading is there. -
Check the production build too:
Terminal window npm run build
Want to go back to the files? Set CMS=files. Your files are still in the project, but they
don’t receive the edits made in Sanity.
6. Deploy on Vercel
Section titled “6. Deploy on Vercel”The site stays a static site, built on your host. On Vercel:
-
Your project → Settings → Environment Variables. Add:
Terminal window CMS=sanitySANITY_PROJECT_ID=abc123xySANITY_DATASET=productionFor a private dataset, also
SANITY_READ_TOKEN(the Viewer token). Don’t addSANITY_WRITE_TOKEN: the site never writes to Sanity. -
Redeploy (Deployments → the latest → ⋯ → Redeploy) or push a commit. In the build log you’ll see the same
… from Sanitylines.
New to Vercel? Start with Put your site online.
7. Rebuild when you publish
Section titled “7. Rebuild when you publish”Because content is read at build time, the live site changes when it’s rebuilt. Let Sanity start a build each time someone publishes:
- Create a deploy hook on your host. On Vercel: your project → Settings → Git → Deploy
Hooks. Name it (e.g.
Sanity), choose the branchmain, click Create Hook and copy the URL. (Netlify calls these build hooks, Cloudflare Pages deploy hooks.) - Add a webhook in Sanity. In sanity.io/manage → your project → API → Webhooks →
Create webhook:
- URL: the deploy hook URL
- Dataset: the one your site reads
- Trigger on: Create, Update and Delete
- Filter: the line
npm run cms -- sanity schemaprinted, e.g._type in ["home", "servicesPage", …](it lists every type your site reads) - HTTP method: POST
- Save, then publish a small change in the Studio. A new deployment appears in Vercel, and a minute or two later the change is live.
Saving a draft doesn’t start a build; publishing does.
Drafts and previews
Section titled “Drafts and previews”The live site only ever shows published content. To look at unpublished changes, make a second deployment that reads drafts:
-
Create a read token (Viewer), if you don’t have one.
-
Deploy the same repository as a separate site (on Vercel, a second project), with the same variables as the live site plus:
Terminal window SANITY_READ_TOKEN=sk...SANITY_PERSPECTIVE=drafts -
Give it its own deploy hook and a second Sanity webhook. To rebuild it on every saved draft, not only on publish, turn on the webhook’s option to trigger on drafts.
This preview is rebuilt like the live site, so it isn’t instant; it’s a full copy of the site with drafts in place of the published versions. Keep its address to yourself, or protect it with your host’s password protection.
You can also try it locally: put the same two lines in .env with CMS=sanity and restart
npm run dev.
Private datasets
Section titled “Private datasets”With a private dataset, nothing can be read without a token:
- Set
SANITY_READ_TOKEN(Viewer) in.envand on your host. - The Studio works as before: editors sign in with their Sanity accounts.
Changing the content model later
Section titled “Changing the content model later”When you add or change fields in src/content/model.ts (see
Change the content model):
-
Update the Studio’s schema and redeploy it:
Terminal window npm run cms -- sanity schemacd studionpm run deploy -
Fill in the new fields in the Studio and publish. If you added a required field, the build fails until every document has it, with a message naming the field.
-
If you added a new kind of document, update the webhook filter with the line the schema command prints.
Don’t seed again to apply model changes: --force would replace everything edited in Sanity with
your local files.
Troubleshooting
Section titled “Troubleshooting”Set SANITY_PROJECT_ID.
The seed command didn’t find the project id. Check .env in the project folder (not
studio/.env) and run the command from the project folder.
Set SANITY_WRITE_TOKEN (an Editor token) to seed.
Add the Editor token to .env. A Viewer token can’t write.
… of these documents already exist in Sanity …
The dataset isn’t empty. That’s the safety stop: add --force only if you want your local
files to replace what’s in Sanity.
Sanity query failed (401) or (403)
The dataset is private, or the token is wrong or has been deleted. Set a valid
SANITY_READ_TOKEN (Viewer).
Sanity query failed (…) with another code, or Sanity mutate failed (…)
Check SANITY_PROJECT_ID and SANITY_DATASET for typos, and for seeding that the token has the
Editor role.
The build fails with a field error after switching to Sanity. A document is missing a required field or has a value the site rejects. The message names the page and the field; fix it in the Studio and publish.
Publishing doesn’t update the live site. Check the webhook in sanity.io/manage (each webhook shows its recent attempts), that its filter covers the document type you changed, and that Vercel started a deployment. A draft alone doesn’t trigger it.
The local site doesn’t show a change from the Studio.
Publish it (a draft isn’t shown) and restart npm run dev.
The Studio is empty, or shows another project.
Check SANITY_STUDIO_PROJECT_ID and SANITY_STUDIO_DATASET in studio/.env, then restart
npm run dev there.
The Studio says the origin isn’t allowed. Add its address under API → CORS origins, with Allow credentials ticked.
