Skip to content

Fix Sanity CORS errors: add the right origin

Add the exact origin (scheme, host, port) under API, CORS origins, with credentials where someone logs in. Why it can still fail after you added it.

Updated 23 November 2026Tested with
  • Sanity CLI 8.33.0

A Sanity CORS error means the browser asked Sanity's API for data from an address the project doesn't trust. Add that exact origin (scheme, host and port, no path) to the project's CORS list, and tick "Allow credentials" if a logged-in user or a token is involved. You can do it at sanity.io/manage under API, CORS origins, or from the terminal with npx sanity cors add.

What the error looks like

In the browser console you'll see something like this, and the Studio or your page shows no data:

Terminal
Access to fetch at 'https://abc123xy.api.sanity.io/v2025-02-19/data/query/production?…'from origin 'http://localhost:3000' has been blocked by CORS policy

A running Studio says it in plain words instead: it shows a screen asking you to add the origin, with the exact address it's running on. Only requests from a browser are affected. Server-side code (a Next.js server component, an Astro build, a script) doesn't send an origin and never hits this.

Add the origin

  1. Step 1: Find the exact origin

    It's what's in the address bar, up to the first slash after the host: http://localhost:3000, https://www.example.com, https://acme.sanity.studio. http and https are different origins; so are example.com and www.example.com, and every port.

  2. Step 2: Decide on credentials

    Tick "Allow credentials" for any address where someone logs in or a token is sent: the Studio, a preview or draft mode page, the Presentation tool's site. Leave it off for a public site that only reads published content from a public dataset.

  3. Step 3: Add it

    In the dashboard: sanity.io/manage, your project, API, CORS origins, Add CORS origin. Or from the Studio folder:

    Terminal
    npx sanity cors add http://localhost:3333 --credentialsnpx sanity cors add https://www.example.com --no-credentialsnpx sanity cors list

    The CLI uses the project ID from sanity.cli.ts; pass -p <id> to target another project. We checked these flags against the CLI's own help in version 8.33.

  4. Step 4: Reload

    The change applies within seconds. Hard-reload the page so the browser doesn't reuse a failed preflight.

Still failing after you added it

  • A trailing slash or a path. https://www.example.com/ or …/studio isn't an origin. Enter the bare origin.
  • The wrong port. The Studio runs on 3333 by default, Next.js on 3000, Astro on 4321, Vite on 5173. If a port was taken and your dev server moved to the next one, that's a new origin.
  • Credentials needed but not allowed. If the request carries a token or a cookie, an origin without credentials is refused. Delete the entry and add it again with credentials.
  • The wrong project. The origin went into project A while the code reads project B. Compare the project ID in the failing URL with the one in the dashboard.
  • Preview deployments. Each Vercel, Netlify or Cloudflare preview URL is a new origin. A wildcard such as https://*.example-previews.dev covers them; the CLI asks you to confirm wildcards (--yes skips that). Keep wildcards narrow and without credentials where you can.

In our Next.js + Sanity templates

The site reads published content on the server, so the public site itself needs no CORS entry. You add one origin during setup: http://localhost:3333 with credentials, for the Studio in development. When you deploy the Studio somewhere other than *.sanity.studio, add that address too, with credentials. The README's "Set up Sanity" section lists it as step 5. Deploying Sanity Studio covers the hosting choices.

Questions

Do I need a CORS origin for my production website?

Only if the browser talks to Sanity directly: client-side fetching, live preview, visual editing. A site that fetches on the server or at build time doesn't.

Can I allow all origins?

* is accepted, but don't. Combined with credentials it lets any site act with a logged-in editor's session. List the origins you use.

Why does the Studio ask for CORS on localhost but not on sanity.studio?

Your local address is new to the project until you add it. The hosted *.sanity.studio address is on Sanity's own hosting, which handles it.

Was this page helpful?

Still stuck? We answer every message ourselves. Write to us

This page is built from UI Pro parts: the docs article block (docs-quernly) with the code, terminal, install command, steps, contents and feedback components. They ship in the next UI Pro release; the install command and the contents rail are free.