DEV Community

Cover image for Sanity CMS Error: Failed to communicate with the Sanity API
Graeme T-Gill
Graeme T-Gill

Posted on

Sanity CMS Error: Failed to communicate with the Sanity API

I’ve recently started using the Sanity Content Management System and came across a particular error.

Running $sanity init in my studio folder I encountered this -

Graemes-MacBook-Pro:studio graeme$ sanity init
You're setting up a new project!
We'll make sure you have an account with Sanity.io. Then we'll
install an open-source JS content editor that connects to
the real-time hosted API on 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://sanity.io/create

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


Error: Failed to communicate with the Sanity API:
Unauthorized - Session not found. For more information, see https://docs.sanity.io/help/cli-errors.
    at /usr/local/lib/node_modules/@sanity/cli/bin/sanity-cli.js:4318:3725
    at async e.default (/usr/local/lib/node_modules/@sanity/cli/bin/sanity-cli.js:4318:3549)
Enter fullscreen mode Exit fullscreen mode

It’s strange because it recognises that you’re logged in but then says it can’t authorize a connection to the Sanity API.

I tried a number of things and I’m still not sure why this was happening.

I fixed it by simply logging out of the Sanity CLI in the terminal and then logging back in again. As always turning something off and then back on again is worth a try.

Graemes-MacBook-Pro:studio graeme$ sanity logout
Logged out
Graemes-MacBook-Pro:studio graeme$ sanity login
Enter fullscreen mode Exit fullscreen mode

Photo by Steve Johnson on Unsplash

Discussion (6)

Collapse
lordvic4real profile image
lordvic4real

thanks so much

Collapse
graemetg profile image
Graeme T-Gill Author

So pleased this is helping people.

Collapse
conorgaughan profile image
conor-gaughan

Thank you!

Collapse
bnsddk profile image
Omar

Thanks, worked for me too

Collapse
jordanmiguel profile image
Jordan Miguel

Thanks mate, I just stumbled across the exact error!

Collapse
graemetg profile image
Graeme T-Gill Author

That's great I'm glad it helped you out!