
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeI can help you troubleshoot environment variable issues with your Sanity & Next.js setup! This is a common pain point because the two systems use different prefixes.
When you have Sanity Studio embedded in a Next.js app, you need to understand that two different prefix systems are at play:
Variables that need to be accessible in the browser must use the NEXT_PUBLIC_ prefix:
NEXT_PUBLIC_SANITY_PROJECT_ID=your-project-id
NEXT_PUBLIC_SANITY_DATASET=productionIf you're running Sanity Studio (either standalone or embedded), variables need the SANITY_STUDIO_ prefix:
SANITY_STUDIO_API_PROJECT_ID=your-project-id
SANITY_STUDIO_API_DATASET=productionProblem 1: Embedded Studio in Next.js
When you embed Sanity Studio in Next.js (like at /studio route), you often need both prefixes for the same values:
# For Next.js pages/components
NEXT_PUBLIC_SANITY_PROJECT_ID=abc123
NEXT_PUBLIC_SANITY_DATASET=production
# For embedded Studio
SANITY_STUDIO_API_PROJECT_ID=abc123
SANITY_STUDIO_API_DATASET=productionProblem 2: Server-side vs Client-side
Remember that NEXT_PUBLIC_ variables are exposed to the browser. For server-only operations (like using tokens), don't use the NEXT_PUBLIC_ prefix:
# Server-side only (never exposed to browser)
SANITY_API_TOKEN=your-secret-token
# Client-side accessible
NEXT_PUBLIC_SANITY_PROJECT_ID=abc123Problem 3: Not Restarting Dev Server
After changing .env files, you must restart your Next.js dev server. The variables are only loaded at startup.
Problem 4: Wrong .env File Make sure you're using the right file:
.env.local - loaded in all environments (gitignored by default).env.development - only in development.env.production - only in production buildsNEXT_PUBLIC_ or SANITY_STUDIO_).env changesconsole.log(process.env.NEXT_PUBLIC_SANITY_PROJECT_ID).env.local file is in the root of your Next.js project.env.local files are in .gitignoreHere's what a standard .env.local file looks like for an embedded Studio:
# For Next.js client-side
NEXT_PUBLIC_SANITY_PROJECT_ID=abc123
NEXT_PUBLIC_SANITY_DATASET=production
# For embedded Sanity Studio
SANITY_STUDIO_API_PROJECT_ID=abc123
SANITY_STUDIO_API_DATASET=production
# For server-side API routes (no NEXT_PUBLIC!)
SANITY_API_READ_TOKEN=your-token-hereThe key thing to remember: project IDs and dataset names aren't secret and will be exposed in your built application anyway, so using NEXT_PUBLIC_ for them is fine. But keep API tokens server-side only without the NEXT_PUBLIC_ prefix!
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store