CircleCI authentication error: How to use Sanity Token instead of login?

4 replies
Last updated: Jun 20, 2022
Hello, when I try to integrate it with CircleCI, it gets a login error. is anyone knows how can I pass it through Sanity Token? thanks
  #!/bin/bash -eo pipefail
sanity graphql deploy --dataset=staging --tag default --playground

Error: You must login first - run "sanity login"
    at node_modules/@sanity/cli/bin/sanity-cli.js:707:672
    at deployApiActions (node_modules/@sanity/core/lib/actions/graphql/deployApiAction.js:68:18)
Error: 
Exited with code exit status 1

Step failed
Error: runner failed (exited with 101)
Task failed
AI Update

You're getting this error because the Sanity CLI needs authentication when running in a CI/CD environment like CircleCI. The sanity graphql deploy command requires authentication, but the interactive sanity login doesn't work in automated environments.

The solution is to use the SANITY_AUTH_TOKEN environment variable. Here's how to set it up:

Steps to fix:

  1. Generate a token in your Sanity project management dashboard:

    • Go to your project settings
    • Navigate to API settings
    • Create a new token with appropriate permissions (you'll need deploy permissions for GraphQL)
  2. Add the token to CircleCI:

    • In your CircleCI project settings, go to Environment Variables
    • Add a new variable named SANITY_AUTH_TOKEN
    • Paste your token as the value
  3. The CLI will automatically use it - no code changes needed! When the SANITY_AUTH_TOKEN environment variable is set, the Sanity CLI will use it for authentication instead of requiring an interactive login.

Your CircleCI config should work as-is:

#!/bin/bash -eo pipefail
sanity graphql deploy --dataset=staging --tag default --playground

According to the CLI Configuration documentation:

When you need to run the CLI unattended, like in a CI/CD environment, set the SANITY_AUTH_TOKEN environment variable to a token.

Important: Make sure the token you generate has sufficient permissions for deploying GraphQL schemas. You may need a token with "Deploy Studio" or similar permissions depending on your project setup.

Show original thread
4 replies
I think you can pass it as a
SANITY_AUTH_TOKEN
environment variable.
Thank you, I am trying right now.
Backporting the outcome here: it seems to work. πŸ™‚
Thank you really, it is working πŸ™

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?