Using ENV variables for projectId in sanity.json

8 replies
Last updated: Jan 24, 2021
Hi, Can someone tell me the proper way to enter the projectID using ENV’s in the sanity.json. If I hard code, works great, anything else and and it’s not finding the projectId. ENV’s are in .env.local
"api": {
    "projectId": "PLACEHOLDER_ENV_VARIABLES_WILL_HANDLE_FOR_US",
    "dataset": "PLACEHOLDER_ENV_VARIABLES_WILL_HANDLE_FOR_US"
  }
AI Update

The issue you're experiencing is that sanity.json doesn't support environment variables directly. This is a static JSON configuration file that gets parsed at build time, and it can't interpolate values from .env.local files.

Here's the proper approach depending on your Sanity Studio version:

For Sanity Studio v3 (Modern Approach)

If you're using Studio v3, you should be configuring your project in sanity.config.ts (or .js) instead of sanity.json. This file does support environment variables:

// sanity.config.ts
import {defineConfig} from 'sanity'

export default defineConfig({
  projectId: process.env.SANITY_STUDIO_PROJECT_ID || '',
  dataset: process.env.SANITY_STUDIO_DATASET || 'production',
  // ... rest of your config
})

In your .env.local:

SANITY_STUDIO_PROJECT_ID=your-project-id
SANITY_STUDIO_DATASET=production

Important: Environment variables in Sanity Studio must be prefixed with SANITY_STUDIO_ to be accessible.

For Sanity Studio v2 (Legacy)

If you're still on v2 with sanity.json, you have two options:

Option 1: Just hardcode the values in sanity.json since project IDs are not sensitive information and will be visible in your production code anyway:

{
  "api": {
    "projectId": "abc123xyz",
    "dataset": "production"
  }
}

Option 2: Use environment variables in your client configuration files (not in sanity.json), like when setting up API clients:

// client.js
import {createClient} from '@sanity/client'

export const client = createClient({
  projectId: process.env.SANITY_STUDIO_PROJECT_ID,
  dataset: process.env.SANITY_STUDIO_DATASET,
  apiVersion: '2023-06-10',
  useCdn: true,
})

The key insight is that your projectId and dataset aren't secrets - they'll be exposed in image URLs, API calls, and client-side code regardless. The real security comes from your dataset permissions and API tokens, not from hiding the project ID.

Show original thread
8 replies
They need to be in studio folder and be named
.env.development
and
.env.production
I think
and should not be referenced in sanity.json file like that. you can delete the api section
ok thanks, I’ll give that a go
https://www.sanity.io/docs/studio-environment-variables#dot-env-files-env-f5e9e3158896 here you can read a bit more about how they should me named etc
Makes sense, production is working, it’s just localhost that is not finding the project ID. I put in the studio folder
Nice, that’s all it was. Cheers
user T
Hey guys, I am facing the same problem, and I followed the instructions of
user T
(removed the api section from sanity.json), and I placed a .env.development in the root folders with the variables SANITY_STUDIO_API_PROJECT_ID and SANITY_STUDIO_API_DATASET defined.However, when I run sanity start, sanity says "Project configuration required before starting studio" and forces me to select a project or create a new one ...

Any ideas how I could fix that?
(found the answer thanks to
user Q
, commented here: https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1611482229244200 )

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?