Error with Sanity configuration and environment variables
The issue is with your environment variable naming! Sanity Studio v3 uses a specific naming convention for environment variables, and you've got a slight mismatch.
Your environment variables should be named SANITY_STUDIO_PROJECT_ID and SANITY_STUDIO_DATASET (not SANITY_STUDIO_API_PROJECT_ID and SANITY_STUDIO_API_DATASET).
Here's how your .env.development file should look:
# For Next.js
NEXT_PUBLIC_SANITY_PROJECT_ID="#######"
NEXT_PUBLIC_SANITY_DATASET="production"
# For Studio
SANITY_STUDIO_PROJECT_ID="#######"
SANITY_STUDIO_DATASET="production"Then in your sanity.config.js or sanity.config.ts, you can reference them in your defineConfig like this:
import { defineConfig } from 'sanity'
export default defineConfig({
projectId: process.env.SANITY_STUDIO_PROJECT_ID,
dataset: process.env.SANITY_STUDIO_DATASET,
// ... rest of your config
})Important notes:
- All Sanity Studio environment variables must be prefixed with
SANITY_STUDIO_to be accessible in your Studio code - The environment file is determined automatically:
.env.developmentloads when runningsanity start, and.env.productionloads duringsanity buildorsanity deploy - After changing your
.envfile, restart your dev server for the changes to take effect - Project IDs and dataset names aren't secret - they'll be exposed in your built application, so it's safe to use them in environment variables
If you want to use different variable names (like SANITY_STUDIO_API_PROJECT_ID), you'll need to explicitly map them in your config file, but the standard convention as documented in the Sanity environment variables docs is SANITY_STUDIO_PROJECT_ID and SANITY_STUDIO_DATASET.
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.