How to manage different datasets in development and production environments in Sanity.io

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?
Jun 12, 2020, 5:11 AM
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.
Jun 12, 2020, 7:55 AM
Thank you for the answer! What if I also have a staging environment? Can I give the
sanity deploy
the environment as a parameter?
Jun 12, 2020, 8:21 AM
You can also override the dataset via the command line. For example:
SANITY_STUDIO_API_DATASET=staging sanity deploy
Jun 12, 2020, 8:24 AM
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.
Jun 16, 2020, 10:51 AM
Are you running the command from your Studio root folder?
Jun 16, 2020, 11:32 AM
Yes from the same folder where the sanity.json is located.
Jun 16, 2020, 11:33 AM
Without quotation marks and such, correct? What’s the exact command you are running?
Jun 16, 2020, 11:43 AM
SANITY_STUDIO_API_DATASET=development sanity deploy
Jun 16, 2020, 11:44 AM
Looks right to me, so this is strange. What versions of Sanity are you on? (
sanity versions
)
Jun 16, 2020, 11:45 AM
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.
Jun 16, 2020, 11:50 AM
I ran the upgrade, but still get the same error message 🤔
Jun 16, 2020, 11:53 AM
Where are you running the command? i.e. what application
Jun 16, 2020, 11:59 AM
Where are you running the command? i.e. what application
Jun 16, 2020, 11:59 AM
Where are you running the command? i.e. what application
Jun 16, 2020, 11:59 AM
windows command prompt
Jun 16, 2020, 12:00 PM
Could I use something else that might work? I would really need to deploy this :)
Jun 16, 2020, 1:19 PM
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.
Jun 16, 2020, 1:27 PM
Do you mean here:
Jun 16, 2020, 1:30 PM
Indeed, if it’s for this specific deployment that should work, although I understand if you’d like to make this more flexible afterwards.
Jun 16, 2020, 1:30 PM
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.
Jun 16, 2020, 1:31 PM
Do I need to remove the "__experimental_spaces": [ part?
Jun 16, 2020, 1:31 PM
No need 🙂
Jun 16, 2020, 1:31 PM
ok, I will change the .env.production file to development as well.
Jun 16, 2020, 1:32 PM
But yes, it would be nice to have it working a bit more smoothly 🙂
Jun 16, 2020, 1:32 PM
*development indeed. Let’s figure out how to resolve the terminal issue afterwards, because the command should work the way you used it.
Jun 16, 2020, 1:33 PM
ok. so now I only run
sanity deploy 
normally (when I have changed the .env. production file), correct?
Jun 16, 2020, 1:34 PM
That’s correct!
Jun 16, 2020, 1:37 PM
That’s correct!
Jun 16, 2020, 1:37 PM
Hmm... it gives an error: Unauthorized - Session not found...
Jun 16, 2020, 1:39 PM
Jep, worked 🙂 Thank you for the help!
Jun 16, 2020, 2:06 PM
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)?
Jul 2, 2020, 11:20 AM
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
Aug 10, 2020, 8:15 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?