Why are Sanity environment variables (SANITY_STUDIO_API_DATASET) undefined?

5 replies
Last updated: Sep 7, 2021
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:

  • .env.development - loaded during sanity start (default)
  • .env.production - loaded during sanity build and sanity 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-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:

  • It reads from your actual sanity.config.ts configuration
  • No risk of environment variables being undefined or misconfigured
  • Works consistently across all environments without managing .env files
  • 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:

  1. Create .env.development in your Studio root folder (same directory as sanity.config.ts)
  2. Prefix all variables with SANITY_STUDIO_
  3. Restart your dev server after creating/modifying .env files
  4. Access them using process.env.SANITY_STUDIO_YOUR_VAR_NAME

Debugging Steps

If environment variables still aren't working:

  1. Verify the .env file is in the correct location (Studio root, not project root if they're different)
  2. Check that there are no typos in variable names
  3. Try logging all environment variables to see what's available:
    console.log('All env vars:', Object.keys(process.env).filter(key => key.startsWith('SANITY_')))
  4. 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 thread
5 replies
Hey Petar! The fact that they're all coming through undefined makes me think of this section of the docs:
Environment variables don't inherit their values from
sanity.json
and will be
undefined
until explicitly set.
Thanks Rachel. I was searching for a way to define a theme based off the active dataset.
Thanks Rachel. I was searching for a way to define a theme based off the active dataset.
Hi Petar. I just gave the following a try and it worked, so maybe you can step through it and confirm you did everything the same. If you did, we’ll continue to troubleshoot from there.
First, you’ll want to have two files in the root of your studio folder:
.env.development
and
.env.production
. It’s important they’re at the root—at the same level as your
sanity.json
file. Edit the following to align with the names of your datasets, but you’ll want something like:

// .env.production

SANITY_STUDIO_API_DATASET="production"

// .env.development

SANITY_STUDIO_API_DATASET="development"
Next, you’ll want to make a file to handle the logic of which dataset you’re in, and then output the correct “logo” (or, in my case, I’ll just put text). This file can be named whatever you want and can live wherever you want, but I’ll use
Logo.js
in the root of the studio folder:

// Logo.js

import React from 'react'

const dataset = process.env.SANITY_STUDIO_API_DATASET

const Logo = () => dataset === "development" ? <div>DEV</div> : <div>PROD</div>

export default Logo
Finally, you’ll want to tell your studio that it needs to implement that Logo file. You can open your
sanity.json
file and add the following to `"parts"`:

{
  "implements": "part:@sanity/base/brand-logo",
  "path": "./Logo.js"
}
Now, your
sanity.json
file might look something like this:

// sanity.json

{
  "root": true,
  "project": {
    "name": "env-logo"
  },
  "api": {
    "projectId": "g8axdcb3",
    "dataset": "production"
  },
  "plugins": [
    "@sanity/base",
    "@sanity/components",
    "@sanity/default-layout",
    "@sanity/default-login",
    "@sanity/desk-tool"
  ],
  "env": {
    "development": {
      "plugins": [
        "@sanity/vision"
      ]
    }
  },
  "parts": [
    {
      "name": "part:@sanity/base/schema",
      "path": "./schemas/schema"
    },
    {
      "implements": "part:@sanity/base/brand-logo",
      "path": "./Logo.js"
    }
  ]
}
There are additional steps you can take if you want, such as implementing Spaces or defining a theme based on your environment. However, you can test this out by running your studio as such:


sanity start
// Should start in your development environment
SANITY_ACTIVE_ENV=production sanity start
// Should start in your production environment
Hi Petar. Did you have any luck with this? Wanted to follow up and make sure you got things working as expected.

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?