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

5 replies
Last updated: May 9, 2022
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
Mmh, not really since JSON is static by definition. What is your use case? 🙂
you can’t set them there, but you can override them

https://www.sanity.io/docs/studio-environment-variables#cc239c4841f0
SANITY_STUDIO_API_PROJECT_ID - Sets the project ID for the current build. This overrides the sanity.json property api.projectId. This environment variable has higher precedence than any env configuration defined in sanity.json.

SANITY_STUDIO_API_DATASET - Sets the dataset for the current build. This overrides the sanity.json property api.dataset. This environment variable has higher precedence than any env configuration defined in sanity.json.
So I guess you could avoid setting them in
sanity.json
, and pass them as environment variables to your Studio if that works?
yeah i just set projectId to null usually

  "api": {
    "projectId": null
Great, thanks!
Use case is a wierd "multisite" thing where different datasets will be different sites

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?