Set Sanity project ID and dataset from .env in sanity.json

5 replies
Last updated: Nov 29, 2025
Is there any way to set the project id and dataset in the sanity.json file via .env?
AI Update

Yes! While you can't directly use environment variables inside the sanity.json file (since JSON is static), you can override the values using special environment variables.

The Sanity CLI recognizes these specific environment variables that take precedence over what's in sanity.json:

  • SANITY_STUDIO_API_PROJECT_ID - Overrides the api.projectId property
  • SANITY_STUDIO_API_DATASET - Overrides the api.dataset property

According to the Studio environment variables documentation, these environment variables have higher precedence than any configuration defined in sanity.json.

How to use them

A common pattern is to set projectId to null in your sanity.json:

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

Then define the values in your .env file:

SANITY_STUDIO_API_PROJECT_ID=your-project-id
SANITY_STUDIO_API_DATASET=production

This approach is particularly useful for multi-site setups where different datasets represent different sites, or when you want to keep project configuration out of version control during development.

Important note: This is specifically for Sanity Studio v2. If you're using Studio v3+, configuration is handled differently through the sanity.config.js or sanity.config.ts file, where you can directly reference environment variables in JavaScript/TypeScript using process.env.SANITY_STUDIO_API_PROJECT_ID.

Show original thread
5 replies

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?