Sanity Session Does Not Match Project Host Error After Login/Logout

3 replies
Last updated: Apr 26, 2020
I have stumble upon a weird bug about my session not matching project host. Anyone knows why? I have used
sanity logout
and
sanity login
and it still doesn’t work
Error: Session does not match project host
    at Object.getRemoteGraphQLSchema (.../node_modules/gatsby-source-sanity/lib/util/remoteGraphQLSchema.js:25:15)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
AI Update

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:

  1. The token you're using in your Gatsby configuration
  2. 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:

  1. Go to manage.sanity.io
  2. Select the correct project (the one matching your projectId in gatsby-config.js)
  3. Navigate to SettingsAPITokens
  4. Generate a new token with appropriate permissions (usually "Read" is sufficient for gatsby-source-sanity)
  5. 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 thread
3 replies
It sounds like you're using a token that is generated for a different project than what you've configured for Gatsby
Eg the token was generated for project
abc123
but the Gatsby configuration tells it to use project
xyz987
Ah, seems I have mixed up the tokens from different sites. It is solved by generating new token. Thank you for the help!

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?