Managing Development and Production Environments in Sanity Studio

32 replies
Last updated: Aug 10, 2020
I'm trying to figure out how to have different versions in development and production/staging. Please help.
Is the dataset only the data or does it also include the code (the objects and schemas and everything built for the studio)? Do I do
sanity deploy
to two different adresses for my code if I want to deploy to development/production?
If I have for example two different datasets, how do I know which one is managed in the Sanity Studio? Or will there e.g. be two different studios with different addresses?
AI Update

Great questions! Let me break down how Sanity handles different environments and datasets:

What's in a Dataset vs. Your Code

Datasets contain only your content (documents). Your code - including schemas, Studio configuration, plugins, and all the objects you define - lives in your codebase, not in the dataset. This is an important distinction that gives you flexibility in how you structure your environments.

Setting Up Development vs. Production

You have two main approaches:

The most common pattern is to use different datasets for development, staging, and production within the same Sanity project. You configure which dataset to use via environment variables:

// sanity.config.ts
export default defineConfig({
  projectId: 'your-project-id',
  dataset: process.env.SANITY_STUDIO_DATASET || 'production',
  // ... rest of config
})

Then you'd set SANITY_STUDIO_DATASET=development locally and SANITY_STUDIO_DATASET=production in your production environment.

Option 2: Workspaces (Multiple Studios in One)

Workspaces let you configure multiple isolated Studio configurations within a single codebase. Each workspace can point to a different dataset:

// sanity.config.ts
export default defineConfig([
  {
    name: 'development',
    title: 'Development',
    projectId: 'your-project-id',
    dataset: 'development',
    basePath: '/dev',
    // ... schema and plugins
  },
  {
    name: 'production',
    title: 'Production',
    projectId: 'your-project-id',
    dataset: 'production',
    basePath: '/prod',
    // ... schema and plugins
  }
])

With workspaces, you get a single Studio deployment with a dropdown to switch between environments. Each workspace gets its own URL path based on the basePath.

Deploying Your Studio

When you run sanity deploy, you're deploying your Studio code (the UI application) to Sanity's hosting at your-project-name.sanity.studio.

Important: sanity deploy deploys to a single hostname per project. If you want multiple Studio URLs (like dev.sanity.studio and prod.sanity.studio), you'd need to either:

  • Use workspaces with different basePath values (one deployment, multiple paths)
  • Self-host separate Studio instances on your own infrastructure

For most teams, using one Studio deployment with workspaces or environment variables to switch datasets works best. You don't typically need separate Studio deployments - just different datasets for your content.

How the Studio Knows Which Dataset

The Studio knows which dataset to use based on your sanity.config.ts configuration. When you open your Studio:

  • With environment variables: It uses whatever dataset is specified in your config
  • With workspaces: You see a dropdown in the Studio UI to switch between workspaces, and each workspace connects to its configured dataset

The beauty of this setup is that your schemas and Studio code can be identical across environments - only the content differs between datasets.

Show original thread
32 replies
Hi Anna, the dataset only holds your content, not your Studio code. There is no need to deploy separate Studios for different environments. Instead, you could switch between datasets depending on the environment instead.
Let’s say you have two datasets:
development
and
production
. You can then use environment variables to load the correct one. To do so:• Create two files in your Studio root folder:
.env.development
and
.env.production
(mind the dot at the beginning of the filenames)• In each of these files, set
SANITY_STUDIO_API_DATASET
to the appropriate dataset, for example:
SANITY_STUDIO_API_DATASET="production"
• Depending on whether you run
sanity start
(development) or `sanity deploy`/`sanity build` (production), it should now use the correct dataset.In addition to the above, you can also set up so-called
Spaces to easily switch between datasets within the Studio itself: https://www.sanity.io/docs/spaces .
Finally, if you’d like to make it more visible for yourself or other editors what the active environment/dataset is, you can use the env variables mentioned above to update the logo for example:

import React from 'react'

const dataset = process.env.SANITY_STUDIO_API_DATASET

const Logo = () => {
  if(dataset === "staging") {
    return (
      <div>STAGING</div>
    )
  }
  return (
    <svg>
    // ... an svg graphic, omitted here for brevity ...
    </svg>
  )
}

export default Logo
Hope that helps! Let me know if you have any questions.
Thank you for the answer! What if I also have a staging environment? Can I give the
sanity deploy
the environment as a parameter?
You can also override the dataset via the command line. For example:
SANITY_STUDIO_API_DATASET=staging sanity deploy
This above command gives me the error

'SANITY_STUDIO_API_DATASET' is not recognized as an internal or external command, operable program or batch file.
Am I missing something?
I have set up the .env files and the Spaces functionality works.
Are you running the command from your Studio root folder?
Yes from the same folder where the sanity.json is located.
Without quotation marks and such, correct? What’s the exact command you are running?
SANITY_STUDIO_API_DATASET=development sanity deploy
Looks right to me, so this is strange. What versions of Sanity are you on? (
sanity versions
)
Could you try running
sanity upgrade
to see if that makes any difference? I just ran the exact same command locally and it seems to work, so let’s try to narrow down on the possible causes 🙂 Edit: you already have the latest Sanity CLI it seems so no need. Thinking what else the issue could be here.
I ran the upgrade, but still get the same error message 🤔
Where are you running the command? i.e. what application
Where are you running the command? i.e. what application
Where are you running the command? i.e. what application
windows command prompt
Could I use something else that might work? I would really need to deploy this :)
You could set the dataset to staging in your sanity.json file in the meantime for this particular deployment. I think there might be an issue with the PATH environment variable on Windows, but I’m not too familiar with Windows setups to resolve this easily.
Do you mean here:
Indeed, if it’s for this specific deployment that should work, although I understand if you’d like to make this more flexible afterwards.
Hold on, if you still have the .env.production file with a different dataset defined there, that one takes precedence. So you would have to rename that file temporarily OR set
staging
in there instead.
Do I need to remove the "__experimental_spaces": [ part?
No need 🙂
ok, I will change the .env.production file to development as well.
But yes, it would be nice to have it working a bit more smoothly 🙂
*development indeed. Let’s figure out how to resolve the terminal issue afterwards, because the command should work the way you used it.
ok. so now I only run
sanity deploy 
normally (when I have changed the .env. production file), correct?
That’s correct!
That’s correct!
Hmm... it gives an error: Unauthorized - Session not found...
Jep, worked 🙂 Thank you for the help!
Hello again
user M
. I still need to clarify a thing with the deployment.
I now have run
sanity deploy
and have renamed the .env.production to .env.productiontemp (and have in both files
SANITY_STUDIO_API_DATASET="development")
. I have been under the impression that when I run
sanity deploy
, it is deployed only to the development side. But now when I switch to the production dataset with the "spaces" functionality, I can see also there all the new document types and objects etc (not any content though, because we have not put any content there yet). Is this the way it is supposed to work? I thought the idea was to deploy new things first to staging and then to production.
So now I am wondering, after we take the production dataset to use for real and I make a change in the studio code and do
sanity deploy
to staging, can I break something in the production site if it is automatically reflected there as well? If I e.g. change the name of some property or remove some object from a document type or something.
What exactly differs when I deploy to staging/production datasets (if not the documents types, objects and models etc defined in the studio code)?
Hi
user M
, I'm still wondering about this thing in my last comment, and I'm soon to make new changes. I hope you can find the time to give me a quick answer 🤞🤔
Also was the terminal issue solved, that it does not work on windows command prompt?
SANITY_STUDIO_API_DATASET=development sanity deploy

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?