
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeI can see exactly what's causing your CORS issue! The error message about Access-Control-Allow-Credentials being empty when it should be 'true' is a telltale sign.
The problem is that when you pass a token to the Sanity client, it automatically sets withCredentials: true on requests. This requires the "Allow credentials" checkbox to be enabled in your CORS configuration.
Here's how to fix it:
http://localhost:8000 (your Gatsby dev server)The origin you mentioned adding (https://<project-id>.api.sanity.io) is incorrect - that's the Sanity API itself, not your application's origin. You need to add the domain where your Gatsby app runs (http://localhost:8000 for development).
A couple other things to check:
console.log to verify)useEffect is missing a dependency array, so it's running on every render. Add [] as the second argument to run it only once:useEffect(() => {
const client = sanityClient({
projectId: process.env.GATSBY_SANITY_PROJECT_ID,
dataset: process.env.GATSBY_SANITY_DATASET,
token: process.env.SANITY_READ_TOKEN,
useCdn: false
})
client.fetch(`*[_type == "portfolioEntry"]{ title }`).then(res => {
console.log('res', res)
}).catch(err => {
console.error('F', err)
})
}, []) // <-- Add this dependency arrayWhy it works in Postman/Chromium: Those tools don't enforce CORS the same way browsers do with web applications, which is why you're only seeing the issue in your Gatsby app.
Once you enable "Allow credentials" for http://localhost:8000, your requests should work! When you deploy to production, remember to add your production domain with credentials enabled as well.
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store