Skip to content

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.

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. Sign in at sanity.io/manage (create an account if you need one) and create a new project.
  2. Open the project. Its Project ID is shown at the top of the page; copy it.
  3. 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 needs SANITY_READ_TOKEN.

In the project: API → Tokens → Add API token.

  1. 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.
  2. 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.
  1. In your project folder, copy the example settings if you haven’t yet: cp .env.example .env (Windows PowerShell: Copy-Item .env.example .env).

  2. In .env, fill in the Sanity part:

    Terminal window
    SANITY_PROJECT_ID=abc123xy
    SANITY_DATASET=production
    SANITY_WRITE_TOKEN=sk... # the Editor token, keep it on your computer

    Leave CMS=files for now: the upload reads your local files.

  3. Generate the Studio’s schema from the content model:

    Terminal window
    npm run cms -- sanity schema

    It writes studio/schema.json and 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.

  4. Upload all content and photos:

    Terminal window
    npm run cms -- sanity seed

    It 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):

Terminal window
npm run cms -- sanity seed --force

The Studio is a separate small project in the studio/ folder of your site.

  1. Set it up:

    Terminal window
    cd studio
    cp .env.example .env
  2. In studio/.env, fill in your project:

    Terminal window
    SANITY_STUDIO_PROJECT_ID=abc123xy
    SANITY_STUDIO_DATASET=production
    SANITY_STUDIO_APP_ID=
  3. Install and start it:

    Terminal window
    npm install
    npm run dev
  4. Open the address it prints (localhost with 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.

To let others edit without installing anything, host the Studio on Sanity:

Terminal window
cd studio
npm run deploy

If 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.

In sanity.io/manage → your project → Members, invite people by email. They sign in to the Studio with their own Sanity account.

  1. In your site’s .env (not the Studio’s), switch the source:

    Terminal window
    CMS=sanity
  2. Start the site (from the project folder, not studio/):

    Terminal window
    npm run dev

    While it starts, the terminal shows a line for each part it reads, such as services: 6 from Sanity. A 0 means that part is empty in Sanity: check the project id and dataset, and that seeding finished.

  3. Open the site. It should look exactly as before.

  4. 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.

  5. 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.

The site stays a static site, built on your host. On Vercel:

  1. Your project → Settings → Environment Variables. Add:

    Terminal window
    CMS=sanity
    SANITY_PROJECT_ID=abc123xy
    SANITY_DATASET=production

    For a private dataset, also SANITY_READ_TOKEN (the Viewer token). Don’t add SANITY_WRITE_TOKEN: the site never writes to Sanity.

  2. Redeploy (Deployments → the latest → ⋯ → Redeploy) or push a commit. In the build log you’ll see the same … from Sanity lines.

New to Vercel? Start with Put your site online.

Because content is read at build time, the live site changes when it’s rebuilt. Let Sanity start a build each time someone publishes:

  1. Create a deploy hook on your host. On Vercel: your project → Settings → Git → Deploy Hooks. Name it (e.g. Sanity), choose the branch main, click Create Hook and copy the URL. (Netlify calls these build hooks, Cloudflare Pages deploy hooks.)
  2. 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 schema printed, e.g. _type in ["home", "servicesPage", …] (it lists every type your site reads)
    • HTTP method: POST
  3. 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.

The live site only ever shows published content. To look at unpublished changes, make a second deployment that reads drafts:

  1. Create a read token (Viewer), if you don’t have one.

  2. 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
  3. 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.

With a private dataset, nothing can be read without a token:

  • Set SANITY_READ_TOKEN (Viewer) in .env and on your host.
  • The Studio works as before: editors sign in with their Sanity accounts.

When you add or change fields in src/content/model.ts (see Change the content model):

  1. Update the Studio’s schema and redeploy it:

    Terminal window
    npm run cms -- sanity schema
    cd studio
    npm run deploy
  2. 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.

  3. 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.

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.