Sanity Session Does Not Match Project Host Error After Login/Logout
The "Session does not match project host" error with gatsby-source-sanity is typically caused by using an authentication token that belongs to a different Sanity project than the one configured in your Gatsby setup.
Running sanity logout and sanity login won't fix this issue because those commands manage your CLI session tokens, not the API tokens used by gatsby-source-sanity. The problem is specifically about a mismatch between:
- The token you're using in your Gatsby configuration
- The project ID you've specified in your gatsby-source-sanity settings
The solution is to generate a new API token for the correct project:
- Go to manage.sanity.io
- Select the correct project (the one matching your
projectIdin gatsby-config.js) - Navigate to Settings → API → Tokens
- Generate a new token with appropriate permissions (usually "Read" is sufficient for gatsby-source-sanity)
- Update your Gatsby configuration with this new token
Your gatsby-config.js should look something like:
{
resolve: 'gatsby-source-sanity',
options: {
projectId: 'abc123', // Make sure this matches your project
dataset: 'production',
token: 'your-new-token-here', // Use the newly generated token
}
}This is a common issue that happens when you're working with multiple Sanity projects and accidentally mix up tokens between them. The error message is thrown from the gatsby-source-sanity plugin when it tries to fetch your GraphQL schema and detects that the authentication token doesn't belong to the project you're trying to access.
Make sure the projectId in your config matches the project for which you generated the token, and you should be good to go!
Show original thread3 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.