Unlock seamless workflows and faster delivery with our latest releases – get the details

Issue with 401 Unauthorized error during build process on Vercel

27 replies
Last updated: May 3, 2024
Hello everyone,
I'm currently facing an issue during my build process on Vercel: a 401 Unauthorized error code. Despite my best efforts, i can't seem to figure out what's wrong. The error message is quite lengthy, and I'm unsure which part is most relevant to resolving this issue. I'll leave the error-message down below in this thread. I would greatly appreciate it if someone could take a moment to review it
😃
Mar 19, 2024, 7:35 PM
 Generating static pages (5/11) 
eU [Error]: Unauthorized - Session not found
    at onResponse (/vercel/path0/.next/server/chunks/919.js:10:502)
    at /vercel/path0/.next/server/chunks/919.js:1:116054
    at n (/vercel/path0/.next/server/chunks/919.js:2:2623)
    at /vercel/path0/.next/server/chunks/919.js:2:3070
    at /vercel/path0/.next/server/chunks/919.js:2:1406
    at P.s.onreadystatechange (/vercel/path0/.next/server/chunks/919.js:2:1609)
    at /vercel/path0/.next/server/chunks/919.js:2:190
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  statusCode: 401,
  response: {
    body: {
      errorCode: 'SIO-401-ANF',
      error: 'Unauthorized',
      message: 'Session not found',
      statusCode: 401
    },
    url: '<https://xxxxxxxx.api.sanity.io/v2023-01-07/data/query/production?query=%0A*%5B_type+%3D%3D+%22bedriftSchema%22+%26%26+slug.current+%3D%3D+%22bedrift%22%5D%5B0%5D%7B%0A++title%2C%0A+++slug%2C%0A++hero%7B%0A++++title%2C%0A++++description%2C%0A++++%22heroImageUrl%22%3A+heroImage.image.asset-%3Eurl%2C+%0A++++%22overlayOpacity%22%3A+heroImage.overlayOpacity%2C%0A++++%22overlayColor%22%3A+heroImage.overlayColor%2C+%0A++++%22linkText%22%3A+paameldingseLenke.linkText%2C%0A++++%22linkHref%22%3A+paameldingseLenke.linkHref%2C%0A++++%22logoUrl%22%3A+logo.asset-%3Eurl%2C%0A++++opptaksInfo%0A%0A++%7D%2C%0A++pageBuilder%5B%5D%7B%0A++++%0A++_type%2C%0A++_type+%3D%3D+%27bildeMedTekst%27+%3D%3E+%7B%0A++++heading%2C%0A++++excerpt%2C%0A++++image%7B%0A++++++alt%2C%0A++++++%22imageUrl%22%3A+asset-%3Eurl%0A++++%7D%2C%0A++++%22bildeMedTekstLenke%22%3A+%7B%0A++++++%22lenke%22%3A+bildeMedTekstLenke.lenke%2C%0A++++++%22lenketekst%22%3A+bildeMedTekstLenke.lenketekst%0A++++%7D%0A++%7D%2C%0A++_type+%3D%3D+%27tekst%27+%3D%3E+%7B%0A++++tittel%2C%0A++++innhold%0A++%7D%2C%0A++_type+%3D%3D+%27arrangementKortGruppe%27+%3D%3E+%7B%0A++++tittel%2C%0A++++undertittel%0A++%7D%2C%0A++_type+%3D%3D+%27tidligereArrangementGruppe%27+%3D%3E+%7B%0A++++tittel%2C%0A++%7D%2C%0A++_type+%3D%3D+%27samarbeidsPartnere%27+%3D%3E+%7B%0A++++tittel%2C%0A++++karusellBilder%5B%5D%7B%0A++++++%22imageUrl%22%3A+asset-%3Eurl%2C%0A++++%7D%0A++%7D%2C%0A++_type+%3D%3D+%27infoKortGruppe%27+%3D%3E+%7B%0A++++_key%2C%0A++++title%2C%0A++++%22cards%22%3A+cards%5B%5D%7B%0A++++++_type%2C%0A++++++_id%2C%0A++++++title%2C%0A++++++time%2C%0A++++++location%2C%0A++++++description%2C%0A++++++%22icon%22%3A+%7B%0A++++++++name%2C%0A++++++++provider%2C%0A++++++++%22svg%22%3A+icon.svg+%2F%2F+Henter+SVG-dataen%0A++++++%7D%0A++++%7D%0A++%7D%2C%0A++_type+%3D%3D+%27paamelding%27+%3D%3E+%7B%0A++++title%2C%0A++++%22paameldingsId%22%3A+paameldingsId.current%2C%0A++++%22paameldingsknapper%22%3A+paameldingsknapper%5B%5D%7B%0A++++++tekst%2C%0A++++++%22knappTekst%22%3A+knappTekst%2C%0A++++++%22lenke%22%3A+lenke%0A++++%7D%0A++%7D%0A%0A++%7D%0A%7D&%24slug=%22bedrift%22&perspective=published>',
    method: 'GET',
    headers: {
      'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000',
      'content-length': '97',
      'content-type': 'application/json; charset=utf-8',
      date: 'Tue, 19 Mar 2024 19:25:07 GMT',
      'strict-transport-security': 'max-age=63072000; includeSubDomains; preload',
      vary: 'origin',
      via: '1.1 google',
      xkey: 'project-xxxxxxxx'
    },
    statusCode: 401,
    statusMessage: 'Unauthorized'
  },
  responseBody: '{\n' +
    '  "errorCode": "SIO-401-ANF",\n' +
    '  "error": "Unauthorized",\n' +
    '  "message": "Session not found",\n' +
    '  "statusCode": 401\n' +
    '}',
  details: undefined
Mar 19, 2024, 7:36 PM
Have you added Vercel to your allowed CORS origins in sanity.io/manage ?
Mar 19, 2024, 7:49 PM
Yup. My page is live, deployed on Vercel. I’ve been doing quite a bit of reworking on the page implementing visual editing 😋, and the upgraded code gets this error. I haven’t touched Vercel since the initial configuration for when I first deployed the old version 🤔
Mar 19, 2024, 7:54 PM
According to the docs , "Unauthorized - Session not found" could mean 1 of 4 things:
    - A temporary issue, please try to run your command again.
    - You have specified an invalid token with the SANITY_AUTH_TOKEN env variable.
    - The session timed out. Try to log out and log in again with the sanity logout and sanity login CLI commands.
    - There was an issue with your logged in user. Try to logout and login again.
I've been getting this error message
for a week now and am stuck because it's so non-specific
Mar 19, 2024, 8:12 PM
Hm, figured out what your issue is yet?
Mar 20, 2024, 5:22 PM
I've tried updating sanity/cli to ^3.35.1", and then running sanity login, but i still get the same error in vercel. These are the variables i have in my .env file, am i missing something?
NEXT_PUBLIC_SANITY_PROJECT_ID
NEXT_PUBLIC_SANITY_DATASET
NEXT_PUBLIC_SANITY_API_VERSION
SANITY_API_READ_TOKEN
SANITY_API_EDITOR_TOKEN
Mar 20, 2024, 6:09 PM
Did you add your vars to your vercel project?
Mar 20, 2024, 6:19 PM
Yes, i think this looks correct?
Mar 20, 2024, 6:23 PM
Do i have to add NEXT_PUBLIC in the two top variables?
Mar 20, 2024, 6:30 PM
You don’t want tokens to be public. Is your Studio embedded in your Next app or is it separate?
Mar 20, 2024, 6:31 PM
Yeah thats's what i tought heh. It's embedded
Mar 20, 2024, 6:33 PM
Can you try adding NEXT_PRIVATE before them?
Mar 20, 2024, 6:40 PM
I tried that, but i still got the same error 😞
Mar 20, 2024, 6:47 PM
Can you share how you’ve configured your client and the code for wherever you’re firing off that query?
Mar 20, 2024, 6:48 PM
Yes, i think this is it. This query is also used in the preview component:
//sanity.client.ts
import {createClient} from '@sanity/client/stega'

export const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID
export const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION

export const client = createClient({
  projectId,
  dataset,
  apiVersion,
  useCdn: false,
  perspective: 'published',
  stega: {
    enabled: false,
    studioUrl: '/studio',
  },
})

// C:\Users\mariu\start\hjemmeside\app\(user)\bedrift\page.tsx
import BedriftPreview from '@/components/siderPreview/BedriftPreview'
import Bedrift from '@/components/sider/BedriftSide'
import {draftMode} from 'next/headers'
import {notFound} from 'next/navigation'
import {SanityDocument} from '@sanity/client'
import {loadQuery} from '@/sanity/lib/store'
import {Metadata} from 'next'

import {BEDRIFT_QUERY} from '@/sanity/lib/queries'

export const metadata: Metadata = {
  title: 'For bedrifter | Start NTNU',
}

export default async function BedriftSide() {
  const params = {slug: 'bedrift'}
  const initial = await loadQuery<SanityDocument>(BEDRIFT_QUERY, params, {
    perspective: draftMode().isEnabled ? 'previewDrafts' : 'published',
  })

  try {
    if (!initial) {
      return notFound()
    }

    return draftMode().isEnabled ? (
      <BedriftPreview initial={initial} params={params} />
    ) : (
      <Bedrift bedriftSide={initial.data} />
    )
  } catch (error) {
    console.error(`Error in BedriftSide: ${(error as Error).message}`)
    return notFound()
  }
}
Mar 20, 2024, 6:55 PM
I'm wondering if this issue has something to do with how I've set up my pages, but I'm not sure. My events page, which uses dynamic parameters, works just fine and doesn't cause any errors when I build it on Vercel. The only difference I see is that for the events page, I'm using dynamic parameters, but for other pages like the landing page, members page, and about us page, I've put in the parameters directly into the code for visual editing purposes. However, those pages give me errors on Vercel, just like the example I mentioned. This issue happens on all four pages where I've done this.
My events (arrangement) page:


// C:\Users\mariu\start\hjemmeside\app\(user)\arrangementer\[slug]\page.tsx
import {loadQuery} from '@/sanity/lib/store'
import ArrangementPreview from '@/components/siderPreview/ArrangementPreview'
import Arrangement from '@/components/sider/ArrangementSide'
import {SanityDocument, QueryParams} from '@sanity/client'
import {draftMode} from 'next/headers'
import {ARRANGEMENT_QUERY} from '@/sanity/lib/queries'
import {notFound} from 'next/navigation'
import type {Metadata, ResolvingMetadata} from 'next'
import {client} from '@/lib/sanity.client'

export default async function ArrangementSide({params}: {params: QueryParams}) {
  const initial = await loadQuery<SanityDocument>(ARRANGEMENT_QUERY, params, {
    perspective: draftMode().isEnabled ? 'previewDrafts' : 'published',
  })
  
  const slug = params.slug
  if (!slug) {
    console.error('Error in ArrangementSide: slug is undefined')
    return <h1>Invalid slug</h1>
  }

  try {
    if (!initial) {
      return notFound()
    }

    return draftMode().isEnabled ? (
      <ArrangementPreview initial={initial} params={params} />
    ) : (
      <Arrangement page={initial.data} />
    )
  } catch (error) {
    console.error(`Error in ArrangementSide: ${(error as Error).message}`)
    return notFound()
  }
}
Mar 20, 2024, 8:03 PM
Hello everyone,I'm still struggeling with this issue. Can anyone spot what's wrong?
Mar 23, 2024, 6:49 PM
Hello helpful people. I've been struggling with this for weeks, and I still can't seem to figure out what's wrong. Could anyone please take another look at it? I'm not able to deploy all my fantastic new updates 😢
Apr 8, 2024, 7:16 PM
Looking at this again, it looks like you’re not passing a token to your preview client.
Apr 8, 2024, 8:27 PM
export const client = createClient({
  projectId,
  dataset,
  apiVersion,
  useCdn: false,
  perspective: 'published',
  stega: {
    enabled: false,
    studioUrl: '/studio',
  },
  token: <your-token-here>
})
Apr 8, 2024, 8:28 PM
Added my token, and got even more errors. Now also for my dynamic pages
Apr 8, 2024, 8:43 PM
//sanity.client.ts
import {createClient} from '@sanity/client/stega'

export const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID
export const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION

export const client = createClient({
  projectId,
  dataset,
  apiVersion,
  useCdn: false,
  perspective: 'published',
  stega: {
    enabled: false,
    studioUrl: '/studio',
  },
  token: process.env.NEXT_PRIVATE_SANITY_API_READ_TOKEN,
})
Apr 8, 2024, 8:44 PM
Nevermind, got it working now! Thank you for your patience 🤠
Apr 8, 2024, 8:55 PM
Glad you got it working!
Apr 8, 2024, 9:40 PM
I was just about to post something here, sorry I didn't see it sooner. Glad you got it working in the end 🥳
Apr 8, 2024, 9:43 PM
Having the same errors! what fixed it
May 2, 2024, 7:56 PM
i replaced my private key with a new one. i Suspect there was an extra space at the end of mye key causing the issue
May 3, 2024, 9:41 AM

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?