Why are Sanity environment variables (SANITY_STUDIO_API_DATASET) undefined?
I understand your frustration! The good news is that SANITY_STUDIO_API_DATASET and other SANITY_STUDIO_* environment variables should work in Sanity Studio. Let me help you troubleshoot why they're showing as undefined.
Common Reasons Why SANITY_STUDIO_* Variables Are Undefined
1. Missing or Incorrectly Named .env Files
Studio looks for environment-specific files:
.env.development- loaded duringsanity start(default).env.production- loaded duringsanity buildandsanity deploy
Create a .env.development file in your Studio root (same directory as sanity.config.ts):
SANITY_STUDIO_API_DATASET=development
SANITY_STUDIO_API_PROJECT_ID=your-project-id2. Missing SANITY_STUDIO_ Prefix
All environment variables that need to be accessible in your Studio code must be prefixed with SANITY_STUDIO_. Without this prefix, the variables won't be passed through to the frontend build.
# ❌ Won't work
API_DATASET=development
# ✅ Will work
SANITY_STUDIO_API_DATASET=development3. Need to Restart the Dev Server
Environment variables are loaded at build time. If you added or changed .env files while the dev server was running, you must stop and restart sanity start.
4. Using process.env Correctly
Access the variables using process.env:
const dataset = process.env.SANITY_STUDIO_API_DATASET
console.log('Dataset:', dataset) // Debug to see if it's loadingBetter Solution: Use Built-in Hooks (Recommended)
For your specific use case of setting a logo based on the current dataset, you don't actually need environment variables. You can access the dataset directly from your Studio configuration using the useClient() hook:
import { useClient } from 'sanity'
export function MyLogo() {
const client = useClient({ apiVersion: '2023-05-24' })
const dataset = client.config().dataset
// Use dataset to determine which logo to show
return dataset === 'production'
? <img src="/logo-prod.svg" alt="Production" />
: <img src="/logo-dev.svg" alt="Development" />
}Why the Hook Approach Is Better
Using useClient() is more reliable because:
- It reads from your actual
sanity.config.tsconfiguration - No risk of environment variables being undefined or misconfigured
- Works consistently across all environments without managing
.envfiles - The dataset value is guaranteed to match what Studio is actually using
If You Still Need Environment Variables
If you have other use cases requiring environment variables, make sure to:
- Create
.env.developmentin your Studio root folder (same directory assanity.config.ts) - Prefix all variables with
SANITY_STUDIO_ - Restart your dev server after creating/modifying
.envfiles - Access them using
process.env.SANITY_STUDIO_YOUR_VAR_NAME
Debugging Steps
If environment variables still aren't working:
- Verify the
.envfile is in the correct location (Studio root, not project root if they're different) - Check that there are no typos in variable names
- Try logging all environment variables to see what's available:
console.log('All env vars:', Object.keys(process.env).filter(key => key.startsWith('SANITY_'))) - Make sure you're not accidentally using a different environment (check
process.env.SANITY_ACTIVE_ENV)
The official environment variables documentation confirms that the SANITY_STUDIO_ prefix is the correct approach for Sanity Studio v3, and variables are accessed via process.env.
Show original thread5 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.