How to login when `npm create sanity@latest` requires authentication before install?

3 replies
Last updated: May 10, 2024
Hi folks - running
npm create sanity@latest -y
- gives unauthorized error as I am not logged in. How am I supposed to be logged in when package is not even installed?
AI Update

Good news - you don't need to be logged in to run npm create sanity@latest! The "unauthorized" error you're seeing is likely happening at a different step in the process.

Here's what's actually happening: npm create sanity@latest (or npx sanity@latest init) will download and run the @sanity/cli package temporarily without installing it globally. During the initialization process, the CLI will prompt you to log in to Sanity through your browser. This is when authentication happens - not before the package is downloaded.

The typical flow looks like this:

  1. You run npm create sanity@latest -y
  2. npm downloads the latest Sanity CLI package
  3. The CLI starts the project setup wizard
  4. At this point, you'll be prompted to authenticate via browser with sanity login

If you're getting an "unauthorized" error immediately, a few things to try:

Option 1: Let the wizard prompt you naturally

Remove the -y flag and let the setup wizard guide you through the login:

npm create sanity@latest

Option 2: Pre-authenticate with the CLI

If you want to log in first, install the Sanity CLI globally and authenticate:

npm install -g sanity@latest
sanity login

Then run your project creation command.

Option 3: Use npx without auto-yes

The -y flag might be skipping important prompts:

npx sanity@latest init

The Sanity CLI documentation shows that authentication is handled during the initialization wizard, not as a prerequisite for downloading the package. The sanity login command opens your browser to authenticate with Sanity's servers, which happens after the CLI package is already running on your machine.

If you're still seeing issues, it might be an npm registry problem rather than an authentication issue with Sanity itself. Make sure you can access npm packages normally, and check if you're behind a corporate proxy that might be blocking the download.

Show original thread
3 replies
Can you please paste a snippet of exactly what you’re seeing in the terminal? I’m not able to reproduce whether logged in or not and whether the CLI is already installed or not.
greggevorkyan@MacBook-Pro-6 modernbanc-sanity % npm -y create sanity@latest
You're setting up a new project!
We'll make sure you have an account with <http://Sanity.io|Sanity.io>. Then we'll
install an open-source JS content editor that connects to
the real-time hosted API on <http://Sanity.io|Sanity.io>. Hang on.

Press ctrl + C at any time to quit.

Prefer web interfaces to terminals?
You can also set up best practice Sanity projects with
your favorite frontends on <https://www.sanity.io/templates>

Looks like you already have a Sanity-account. Sweet!

✖ Fetching existing projects

Error: Failed to communicate with the Sanity API:
Unauthorized - Session not found. You may need to login again with sanity login.
For more information, see <https://docs.sanity.io/help/cli-errors>.
    at getOrCreateProject (~/.npm/_npx/67a0730928194b24/node_modules/@sanity/cli/lib/_chunks-cjs/cli.js:44559:29)
    at async getProjectDetails (~/.npm/_npx/67a0730928194b24/node_modules/@sanity/cli/lib/_chunks-cjs/cli.js:44521:21)
    at async initSanity (~/.npm/_npx/67a0730928194b24/node_modules/@sanity/cli/lib/_chunks-cjs/cli.js:44316:108)
greggevorkyan@MacBook-Pro-6 modernbanc-sanity % npm create sanity@latest -y
You're setting up a new project!
We'll make sure you have an account with <http://Sanity.io|Sanity.io>. Then we'll
install an open-source JS content editor that connects to
the real-time hosted API on <http://Sanity.io|Sanity.io>. Hang on.

Press ctrl + C at any time to quit.

Prefer web interfaces to terminals?
You can also set up best practice Sanity projects with
your favorite frontends on <https://www.sanity.io/templates>

Looks like you already have a Sanity-account. Sweet!

✖ Fetching existing projects

Error: Failed to communicate with the Sanity API:
Unauthorized - Session not found. You may need to login again with sanity login.
For more information, see <https://docs.sanity.io/help/cli-errors>.
    at getOrCreateProject (~/.npm/_npx/67a0730928194b24/node_modules/@sanity/cli/lib/_chunks-cjs/cli.js:44559:29)
    at async getProjectDetails (~/.npm/_npx/67a0730928194b24/node_modules/@sanity/cli/lib/_chunks-cjs/cli.js:44521:21)
    at async initSanity (~/.npm/_npx/67a0730928194b24/node_modules/@sanity/cli/lib/_chunks-cjs/cli.js:44316:108)
greggevorkyan@MacBook-Pro-6 modernbanc-sanity % npm sanity login           
Unknown command: "sanity"

To see a list of supported npm commands, run:
  npm help
I just solved it by pre-installing sanity globally with
npm install sanity -g
but I wish it was in the docs.

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?