How to set environment variables in Sanity for deployment?

6 replies
Last updated: Jun 13, 2023
Hi! I am using the .env environment to save my Project ID. it works fine locally but when I try to deploy it to sanity, the ID is empty. How can I set up the .env values in SAnity so I can read all variables?Thanks
AI Update

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 during sanity start (local development)
  • .env.production - loaded during sanity build and sanity 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_TOKEN environment variable (without the SANITY_STUDIO_ prefix)

You can read more about this in the Sanity Studio environment variables documentation.

Show original thread
6 replies
Are you deploying it on Vercel? have you provided the env variable to the hosting too?
I am deploying it to Netlify, and it works fine. I need it in Sanity Studio
Next js app?
I will try it, Thanks!
As far as I remember ENV must start with NEXT_PUBLIC in Nextjs
I'm pretty sure you can name the environment variables whatever you want. NEXT_PUBLIC_ is just the convention they used. What are you trying to do exactly? If you're trying to get Next.js setup with Sanity, there are several repos , guides , and youtube videos available that walk you through the configuration process.

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?