CoursesArchitecture & DevOpsSetting Up Your Environments
Architecture & DevOps
Markdown Version

Setting Up Your Environments

Separate development and production environments ensure isolated testing, stable workflows, and safe content migrations without disrupting editors.
Log in to mark your progress for each Lesson and Task
This course assumes you have already initialized a Sanity Studio as described in Day One Content Operations.

When developing new features, the code changes made by developers to schemas and other Studio configuration shouldn't impact content editors working in the production environment. That's why it's a best practice to have separate datasets and Studio deployments for development and production environments.

By provisioning a dedicated development dataset, developers can freely iterate and test code changes without worrying about interrupting the day-to-day content operations. This clean separation allows both content and development workflows to proceed in parallel, while keeping the production environment stable. As new features are validated in the development environment, the code changes can be promoted to production, and any necessary content migrations can be performed in a controlled manner. Developing in a separate environment can also ensure that any schema changes they make will have migration scripts. Meanwhile, content editors can continue their work in the production dataset, insulated from any development activities.

First, to complement the production dataset you should already have, create a development dataset using the CLI:

npx sanity dataset create development --visibility private

You should now see two datasets when you run npx sanity dataset list and in Manage (npx sanity manage).

Environment variables allow your Studio configuration to adapt to each deployment without modifying the codebase—making them essential for managing different environments. Rather than hardcoding the project ID and dataset, for example, you can instead use environment variables to statically replace them at build time.

First, initialize a new environment file by running:

npx sanity init --env --project [your-project-id] --dataset production

You'll see a new .env file in your workspace:

# Warning: Do not add secrets (API keys and similar) to this file, as it is source controlled!
# Use `.env.local` for any secrets, and ensure it is not added to source control
SANITY_STUDIO_PROJECT_ID="[your-project-id]"
SANITY_STUDIO_DATASET="production"
.env by default won't be ignored by Git; however, these two environment variables aren't considered sensitive. If you'd rather they weren't checked into source control, you can use --env .env.local.

Now duplicate .env and name it .env.development, and remove the project ID so you just have the following:

# Warning: Do not add secrets (API keys and similar) to this file, as it is source controlled!
# Use `.env.local` for any secrets, and ensure it is not added to source control
SANITY_STUDIO_DATASET="development"
In this example, we'll be checking these into source control. If you needed to override a variable on your local machine, you could add a .env[.mode].local file with your override(s).

Finally, let's update our configuration files to read from environment variables:

import {defineConfig} from 'sanity'
export default defineConfig({
// ...
projectId: process.env.SANITY_STUDIO_PROJECT_ID!,
dataset: process.env.SANITY_STUDIO_DATASET!,
// ...
})
import {defineCliConfig} from 'sanity/cli'
export default defineCliConfig({
api: {
projectId: process.env.SANITY_STUDIO_PROJECT_ID!,
dataset: process.env.SANITY_STUDIO_DATASET!,
},
// ...
})

In this lesson, we covered how to set up separate development and production datasets in your Sanity project. By creating dedicated datasets and configuring environment variables, you can establish a clean separation between ongoing development work and the stable production environment used by content editors.

With this foundation in place, you're ready to deploy separate Sanity Studios for each environment. In the next lesson, we'll walk through the process of deploying a development Studio and how to set CORS origins for each environment.

Mark lesson as complete
You have 1 uncompleted task in this lesson
0 of 1