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.
- 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:
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 policyA 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
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.httpandhttpsare different origins; so areexample.comandwww.example.com, and every port.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.
Step 3: Add it
In the dashboard: sanity.io/manage, your project, API, CORS origins, Add CORS origin. Or from the Studio folder:
Terminalnpx sanity cors add http://localhost:3333 --credentialsnpx sanity cors add https://www.example.com --no-credentialsnpx sanity cors listThe 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.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…/studioisn'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.devcovers them; the CLI asks you to confirm wildcards (--yesskips 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.
Related articles
- SanityHow to deploy Sanity Studio (hosted or self-hosted)npx sanity deploy puts the Studio on *.sanity.studio for free. Set studioHost for unattended deploys, use a deploy token in CI, or host it yourself.
- Using themes.devFrom website template to live site, step by stepGet the theme from GitHub or as a zip, run it locally, replace the demo content, set SITE_URL and deploy to Cloudflare, Netlify or Vercel.
- DeployingDeploy Next.js to Cloudflare Workers: OpenNext or vinextTwo routes, both built and dry-run deployed on Next.js 16: OpenNext for the real Next.js build, vinext for new apps on Vite. Which to pick and the gotchas.
Still stuck? We answer every message ourselves. Write to us