Exposing environment variables with SANITY_STUDIO_ prefix confusion

4 replies
Last updated: Mar 25, 2024
I'm having a hard time exposing a environment variable.
Following the
docs , env variables prefixed with
SANITY_STUDIO_
are exposed to Sanity CLI tool, development server and bundler.However, if I want to expose a new env variable to the browser, I
should not prefix it with
SANITY_STUDIO_
?
AI Update

Based on the documentation, you're actually correct to use the SANITY_STUDIO_ prefix! Let me clarify how this works:

Yes, you SHOULD prefix your environment variables with SANITY_STUDIO_ to expose them to the browser in Sanity Studio. This is the correct approach according to the Sanity environment variables documentation.

Here's how it works:

All environment variables that need to be accessible in your Studio code must be prefixed with SANITY_STUDIO_. This prefix ensures the variables are properly passed through to the frontend build. For example:

SANITY_STUDIO_API_PROJECT_ID=your-project-id
SANITY_STUDIO_API_DATASET=development
SANITY_STUDIO_CUSTOM_VALUE=some-value

You can then access these in your Studio configuration:

// sanity.config.js
export default defineConfig({
  projectId: process.env.SANITY_STUDIO_API_PROJECT_ID,
  dataset: process.env.SANITY_STUDIO_API_DATASET,
  // other configuration
})

Important distinction: If you're using Sanity with a frontend framework like Next.js, that's a different story. In your Next.js application (not the Studio), you'd use NEXT_PUBLIC_ prefix instead:

NEXT_PUBLIC_SANITY_PROJECT_ID=your-project-id
NEXT_PUBLIC_SANITY_DATASET=production

So the confusion might be coming from mixing up Studio environment variables (which use SANITY_STUDIO_) with your frontend framework's environment variables (which use their own conventions like NEXT_PUBLIC_ for Next.js).

Security note: Remember that any variable with the SANITY_STUDIO_ prefix will be exposed to the browser, so never put sensitive tokens or secrets in these variables. Project IDs and dataset names are fine to expose, but use server-side authentication for sensitive operations.

Show original thread
4 replies
Is this in a standalone or embedded Studio?
Standalone.
Seems like I need to expose it prefixed...
Yep!

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?