Issue with authorization token causing "Session does not match project host" error during GROQ request in Next.js build.
This error typically means there's a mismatch between the project ID configured in your client and the project where your authentication token was created. The "session" refers to your authenticated request, and "project host" refers to the Sanity project you're trying to access.
Here are the most common causes and solutions:
Check Your Project ID
The most likely issue is that your client configuration is pointing to a different project than the one where you created the token. Verify that:
- Your token was created for the correct project - Check in Sanity Manage under the API tab
- Your client configuration uses the matching project ID - Look at your
sanity.config.tsor wherever you initialize your Sanity client:
const client = createClient({
projectId: 'your-project-id', // Must match the token's project
dataset: 'your-dataset',
token: process.env.SANITY_API_TOKEN,
useCdn: false
})Common Scenarios That Cause This
- Multiple projects: You created the token in Project A but your code is configured for Project B
- Copy-pasted configuration: You copied config from another project but forgot to update the project ID
- Environment variables: Your
.envfile has the token from one project but the project ID from another
Quick Fix
- Go to Sanity Manage
- Confirm which project your token belongs to (check the URL and project name)
- Copy the correct project ID from the project settings
- Update your client configuration to use that exact project ID
- Ensure your dataset name also matches
If you're working with multiple Sanity projects, it's helpful to store both the project ID and token together in your environment variables to keep them paired correctly:
SANITY_PROJECT_ID=abc123 SANITY_DATASET=production SANITY_API_TOKEN=sk...
This way, you can be sure they're always in sync! Since it worked once and then failed, double-check if you recently switched projects or updated any configuration files.
Sanity – Build the way you think, not the way your CMS thinks
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.