Build with Sanity MCP in Replit. Prizes up for grabs! Submit by June 14

Why are Sanity environment variables (SANITY_STUDIO_API_DATASET) undefined?

5 repliesLast updated: Dec 2, 2025

I am trying to set the logo icon based off the current dataset. I am using SANITY_STUDIO_API_DATASET emv variable but it is always undefined. As matter of fact all sanity env variables are unset all the time.
AI Update

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:

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-id

2. 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=development

3. 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 loading

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:

If You Still Need Environment Variables

If you have other use cases requiring environment variables, make sure to:

Debugging Steps

If environment variables still aren't working:

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 thread
5 replies

Was this answer helpful?

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.

Related contributions