Session does not match project host error with private dataset token

12 replies
Last updated: Nov 29, 2025
Hej,
I’ve made a dataset private and created a token. I worked once, the second time around i’m getting this for my requests:


    body: {
      message: 'Session does not match project host',
      statusCode: 401,
      error: 'Unauthorized'
    },
What does that mean?
AI Update

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:

  1. Your token was created for the correct project - Check in Sanity Manage under the API tab
  2. Your client configuration uses the matching project ID - Look at your sanity.config.ts or 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 .env file has the token from one project but the project ID from another

Quick Fix

  1. Go to Sanity Manage
  2. Confirm which project your token belongs to (check the URL and project name)
  3. Copy the correct project ID from the project settings
  4. Update your client configuration to use that exact project ID
  5. 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.

Show original thread
12 replies

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.

Was this answer helpful?