Join live – Get insights, tips, + Q&A from Sanity developers on our latest releases

Issue with authorization token in GROQ requests during Next.js build.

10 replies
Last updated: Jan 12, 2021
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?
Jan 12, 2021, 3:09 PM
(normal http groq request btw)
Jan 12, 2021, 3:10 PM
• creating a new token does not fix it• setting the dataset to public again does not fix it (as long as you’re still sending a token)
Jan 12, 2021, 3:17 PM
(I need auth to access non-root-path-docs )
Jan 12, 2021, 3:18 PM
Hi Jan. How are you sending the GROQ request? Is the authorization token being sent along?
Jan 12, 2021, 3:30 PM
Hi Geoff,It’s during next.js build using
.Pretty sure the token is being sent along (works with public and no token).



export const sanityConfig = {
    process.env.SANITY_STUDIO_API_DATASET ||
  token: process.env.STUDIO_TOKEN,
  useCdn: process.env.NODE_ENV === 'production',

const client = sanityClient(sanityConfig);

export default client;
Jan 12, 2021, 3:36 PM
If you open your query in your browser (e.g.,
), do you get something back (i.e., all the public paths)?
Jan 12, 2021, 3:47 PM
Hi Jan - have you verified that your build environment variables are correctly set up? If you host on a platform like Netlify or Vercel, these are not normally pulled from
files anymore but require additional config.
On Vercel: On Netlify:
Jan 12, 2021, 3:53 PM
Also, just to rule this out, are you sure the token you added belongs to the correct project? Sometimes that exact error message means you've used a different project's token.
Jan 12, 2021, 3:56 PM
Jan 12, 2021, 4:17 PM
Gosh darnit 😄I feel so stupid now. You’re exactly right, i just added the token to the wrong project.


user M
user A
Jan 12, 2021, 4:17 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Categorized in