Error when running `sanity graphql deploy` after being logged out

4 replies
Last updated: Feb 26, 2024
Hey! I can't run
sanity graphql deploy
currently... I am getting this error when I try to run it:

Error: Request returned HTTP 401
    at getUrlHeaders (~/sanity-studio/node_modules/sanity/lib/_chunks/deployApiAction-c71b2e2b.js:61:19)
    at async getCurrentSchemaProps (~/sanity-studio/node_modules/sanity/lib/_chunks/deployApiAction-c71b2e2b.js:1865:17)
    at async Object.deployGraphQLApiAction [as default] (~/sanity-studio/node_modules/sanity/lib/_chunks/deployApiAction-c71b2e2b.js:1693:9)
It was working fine a few days ago. Anyone knows what could be the issue?
AI Update

A HTTP 401 error when running sanity graphql deploy typically means your CLI session has expired or isn't properly authenticated. This is a common issue that happens after not using the CLI for a while.

The fix is straightforward - you need to re-authenticate your CLI session. Here's what to do:

sanity logout
sanity login

Then try deploying your GraphQL API again:

sanity graphql deploy

This commonly reported issue happens because the authentication token stored by the CLI expires after a period of inactivity, but the CLI doesn't always recognize it needs refreshing.

Why this happens:

The sanity graphql deploy command requires proper authentication to deploy your GraphQL schema to Sanity's API. When your session expires, the CLI still thinks you're logged in, but the server rejects the request with a 401 Unauthorized error.

Additional context on permissions:

If re-authenticating doesn't solve the issue, it's worth checking that your user account has the necessary permissions to deploy the GraphQL API. You'll need appropriate API tokens and roles assigned to your account. The GraphQL deployment requires write access to your project's API configuration.

If you're working in a team environment, make sure your user role has deployment permissions - not all roles can deploy GraphQL schemas by default. Check with your project administrator if you continue to see authorization errors after logging in again.

What version of the CLI are you using?
3.16.2
I tried upgrading to the latest, but didn't work πŸ˜•
Ha! I had been logged out... Would be great with that as the error message πŸ˜›

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?