How to set up staging and production environments in Sanity Studio using Vercel.

13 replies
Last updated: Sep 28, 2022
Hey all šŸ‘‹ I took over an existing project that uses Studio and deployments with Vercel.I would like to have a staging and a production environment on Studio since I’m changing stuff in the schema and in the ui.
Is that possible? If so, what do I need to do?
AI Update

Absolutely! Setting up staging and production environments for your Sanity Studio is definitely possible, and it's a smart approach when you're making schema and UI changes. Here's how to do it:

The Basic Approach: Multiple Datasets

The key is to use separate datasets for staging and production. Datasets are completely isolated collections of content within your Sanity project - think of them as separate databases. Your Free plan includes 2 datasets, which is perfect for this setup.

Setting Up Your Environments

1. Create a staging dataset

If you don't already have one, create a staging dataset using the Sanity CLI:

sanity dataset create staging

2. Configure environment variables

Since you're deploying to Vercel, you'll want to use environment variables to control which dataset each deployment uses. The key variable is SANITY_STUDIO_API_DATASET.

In your Studio project, you can create environment-specific files:

  • .env.development (for local work)
  • .env.production (for production builds)

For example, in .env.development:

SANITY_STUDIO_API_DATASET="staging"

3. Set up Vercel deployments

The recommended approach is to deploy separate Studio instances to Vercel:

  • Production Studio: Deploy your main branch to Vercel with SANITY_STUDIO_API_DATASET set to production
  • Staging Studio: Create a separate branch (like staging) and deploy it to Vercel with SANITY_STUDIO_API_DATASET set to staging

This way, you can test schema and UI changes in your staging Studio without affecting production.

Important note: If you're using Sanity's hosted Studio (xxx.sanity.studio), you can only host one version there. For staging, you'll need to host on Vercel or another platform. Many teams host production on Sanity's hosting and staging on Vercel, or both on Vercel.

Configure CORS Origins

Don't forget to add your Vercel deployment URLs to your CORS origins in the Sanity management dashboard (manage.sanity.io):

  • Add your staging domain (e.g., https://studio-staging.vercel.app)
  • Add your production domain
  • For Vercel preview deployments, you might want to add a wildcard like https://*.vercel.app (though be cautious with wildcards in production)

Make sure to check "Allow credentials" if you're using authentication tokens.

Alternative: Workspaces Feature

If you only need to switch between datasets but don't need different UI/schema versions, you could use Workspaces to configure multiple isolated Studio configurations within a single deployment. Each workspace can connect to a different dataset and have its own tools and plugins. However, this won't help with testing breaking schema changes since all workspaces share the same codebase - both datasets would use the same Studio code version.

For your use case where you're actively changing the schema and UI, separate deployments (as described above) are the better approach.

Deploying Changes

When you're ready to deploy Studio changes:

  • Test locally against your staging dataset
  • Deploy to your staging Studio on Vercel (via git push)
  • Once validated, deploy to production using sanity deploy (if using Sanity hosting) or merge to your production branch (if using Vercel)

This setup gives you a safe environment to test changes before your client sees them in production!

Is the Studio deployed to Vercel as well? If so create a separate branch for the staging environment and update the env variable specifying the dataset (
SANITY_STUDIO_API_DATASET
) to whatever your staging dataset is called.
I’ve never used it but you can also try this:
https://www.sanity.io/docs/spaces
I don’t think this has a custom deployment. I believe it’s using the default url that studio creates fora. project (if that makes sense)
Like
xxx.sanity.studio/production/dashboard
You can only host 1 version of the Studio with Sanity, but as
user J
mentioned, you can use the spaces feature to switch between datasets.
I see, so no UI changes, got it
Since I have your attention ( šŸ˜„ ), is it common to deploy new Sanity Studio changes via the terminal with
sanity deploy
?
It is! People will also sometimes use Github actions to deploy new versions of the Studio, but I'm a bit ignorant when it comes to that process.
BTW, if you're looking to have different deployments of the Studio with different schemas and the like, you'll want to use another option for hosting. Or host the production Studio with Sanity and the development Studio with Vercel, etc.
Hmm, hmm, that’s what I was looking it.Kinda scared of breaking changes so definitely will be looking into that
Thank you!
How does that affect the
sanity deploy
part, tho? šŸ¤”
sanity deploy
would then only update the production Studio in the example I gave. If you're hosting w/Vercel or Netlify, you'd need to commit your changes to a repo to deploy.
That said, if you're just running the studio locally, any changes you make will not effect the deployed Studio until you run
sanity deploy
. You can get away with just experimenting locally most of the time!
I’ve been trying out Studio locally but would like to have a staging environment for the client to try out the changes, hence my questions

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?