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

Top comments (12)

Collapse
 
jasoncruzdev profile image
Jason Cruz •

Wow i cant believe this worked for me, thanks a million!

Collapse
 
mizanur919 profile image
Md. Mizanur Rahaman •

thanks
It works

Collapse
 
marksikaundi profile image
Mark Sikaundi •

this really helping Thanks

Collapse
 
bahmanworld profile image
Bahman World • • Edited

even sanity logout doesn't work for me.
Is there another way to logout?

Failed to communicate with the Sanity API:
POST-request to https://api.sanity.io/v1/auth/logout resulted in HTTP 403 Forbidden
Enter fullscreen mode Exit fullscreen mode
Collapse
 
graemetg profile image
Graeme T-Gill •

It may have changed now - I haven't tried it recently.

Collapse
 
conorgaughan profile image
conor-gaughan •

Thank you!

Collapse
 
bnsddk profile image
Omar •

Thanks, worked for me too

Collapse
 
lordvic4real profile image
lordvic4real •

thanks so much

Collapse
 
graemetg profile image
Graeme T-Gill •

So pleased this is helping people.

Collapse
 
jordanmiguel profile image
Jordan Miguel •

Thanks mate, I just stumbled across the exact error!

Collapse
 
graemetg profile image
Graeme T-Gill •

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

Collapse
 
huzaifachaudary profile image
HuzaifaChaudary •

YES It woked for me