Sanity CLI login fails in GitHub Codespaces due to localhost callback
Good news - you're on the right track with SANITY_AUTH_TOKEN! This is exactly how you authenticate the Sanity CLI in non-interactive environments like GitHub Codespaces. Here's how to solve this:
Creating an Authentication Token
You need to create a robot token (not a personal token) in your Sanity project:
- Go to manage.sanity.io
- Select your project
- Navigate to Settings → API → Tokens
- Click Add new token
- Give it a descriptive name (like "GitHub Codespaces")
- Choose the appropriate permissions:
- For
sanity deploy: You need at least Deploy Studio permissions - For general development: Editor or Administrator depending on your needs
- For
- Copy the token immediately (you won't be able to see it again)
Using the Token in Codespaces
Once you have the token, set it as an environment variable in your Codespace:
export SANITY_AUTH_TOKEN="your-token-here"To make it persist across Codespace sessions, add it to your Codespace secrets at the repository or user level.
Running Commands
Once SANITY_AUTH_TOKEN is set, the CLI should automatically pick it up and skip the interactive login:
bun run sanity deployThe CLI will detect the environment variable and use it for authentication without trying to open the browser-based OAuth flow.
Important Notes
- Token scope: Make sure your token has sufficient permissions for the operations you need (deploy, dataset access, etc.)
- Security: Never commit tokens to your repository - always use environment variables or secrets management
- Token lifetime: Robot tokens last until deleted, unlike personal tokens which expire after one year
- CI/CD: This same approach works for GitHub Actions and other CI/CD environments
According to the Sanity authentication documentation, robot tokens are specifically designed for applications and non-interactive environments like yours, making them perfect for Codespaces where the localhost callback won't work. The docs explain that while personal tokens are for individual users, robot tokens are the right choice for "applications and third-party services" - which includes development environments like Codespaces.
Show original thread3 replies
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.