How to set environment variables in Sanity for deployment?
The key issue you're experiencing is that when deploying to Sanity (via sanity deploy), environment variables need to be prefixed with SANITY_STUDIO_ to be accessible in your Studio application.
When you deploy your Studio to Sanity's hosting, the .env file from your local machine isn't automatically uploaded. Instead, you need to ensure your environment variables follow the correct naming convention and are properly configured for the production environment.
Solution
1. Prefix your environment variables with SANITY_STUDIO_
In your .env file (or .env.production for production-specific values), make sure all variables are prefixed:
SANITY_STUDIO_API_PROJECT_ID=your-project-id SANITY_STUDIO_API_DATASET=production SANITY_STUDIO_CUSTOM_VALUE=your-custom-value
2. Access them in your config
Then in your sanity.config.js or sanity.config.ts:
export default defineConfig({
projectId: process.env.SANITY_STUDIO_API_PROJECT_ID,
dataset: process.env.SANITY_STUDIO_API_DATASET,
// other configuration
})3. Environment-specific files
Sanity automatically loads different .env files based on the command:
.env.development- loaded duringsanity start(local development).env.production- loaded duringsanity buildandsanity deploy
The environment is determined by the SANITY_ACTIVE_ENV variable, which defaults to development for sanity start and production for sanity deploy.
Important Notes
- Only variables prefixed with
SANITY_STUDIO_will be available in your Studio application after deployment - Project IDs and dataset names are not secret - they will be exposed in your built application, which is normal and expected
- If you need to use authentication tokens for deployment in CI/CD, use the
SANITY_AUTH_TOKENenvironment variable (without theSANITY_STUDIO_prefix)
You can read more about this in the Sanity Studio environment variables documentation.
Show original thread6 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.