How to configure dev environment to fetch data from local dataset and migrate data to production dataset.

4 replies
Last updated: Oct 29, 2021
Two newbie questions in one go:1/ How do I tell my dev environment I want to fetch data from my localhost studio instead of the deployed studio.
2/ How would I go about migrating data from my local studio to the deployed version.

Appreciating the help here very much :)
AI Update

Great questions! Let me help you with both of these.

1. Pointing your dev environment to localhost Studio

Actually, there's a common misconception here: your frontend application doesn't connect to your Studio - it connects directly to Sanity's Content Lake (the hosted API). Whether your Studio is running locally or deployed doesn't affect where your frontend fetches data from.

What you actually want to configure is which dataset your frontend uses. You can set up different datasets for development and production, then use environment variables to control which one your app connects to.

Here's how to set this up:

In your frontend application (e.g., Next.js), create environment files:

.env.local (for local development):

NEXT_PUBLIC_SANITY_PROJECT_ID=your-project-id
NEXT_PUBLIC_SANITY_DATASET=development

.env.production:

NEXT_PUBLIC_SANITY_PROJECT_ID=your-project-id
NEXT_PUBLIC_SANITY_DATASET=production

Then in your Sanity client configuration:

import { createClient } from '@sanity/client'

export const client = createClient({
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
  apiVersion: '2024-01-01',
  useCdn: false, // Use false for development to get fresh data
})

In your Studio, you can also use environment-specific configs with .env.development and .env.production files using the SANITY_STUDIO_ prefix as described in the environment variables documentation.

Don't forget to add http://localhost:3000 (or your dev port) to your CORS origins in your project settings!

2. Migrating data from local to deployed

You'll use the dataset export and import commands for this. Here's the workflow:

Export from your local dataset:

sanity dataset export development ./backup.tar.gz

Import to your production dataset:

sanity dataset import backup.tar.gz production

A few important notes:

  • The export includes all documents AND assets (images, files)
  • Import merges with existing content rather than replacing it
  • Your schema isn't included in the export (it lives in your Studio code)
  • Always back up your target dataset first if it has existing data: sanity dataset export production backup-before-import.tar.gz

If you only want to migrate specific documents, you can export to NDJSON format and use GROQ queries to filter what you're exporting, as shown in this ad-hoc migration recipe.

Hope this helps! Let me know if you need clarification on any of these steps.

Hi Nicolas. One thing to distinguish is between studio code and datasets. Your studio code is what gets installed with
sanity install
and deployed with
sanity deploy
(or with
sanity build
followed by outputting the files to a hosting environment). This studio is the structure where your content lake is displayed and manipulated, but which studio you use (local or deployed) has no impact on your data in the content lake. It is the vehicle rather than the passengers.
What I think you’re asking is more about a distinction between datasets. More specifically, your project can have multiple datasets with separate content (though, as you hinted at, they can be synchronized). Some people might have separate staging and production datasets, or development and staging datasets. These datasets house your content lake and they are what you specify when you’re sourcing your data. They’re the passengers that will sit in your studio “vehicle”.

If that’s indeed the case, then question 1 might be “how do I tell my dev environment I want to fetch data from my development dataset instead of the production dataset?” There are a few ways to do this, with one being
configuring your sanity.json file to select a dataset based on your environment. You can use environment variables to override your environment (so, for example, you could test your sanity.json file is set up correctly by running
SANITY_ACTIVE_ENV=production sanity start
, which should run your production dataset locally).
For question 2, the question that follows may be “how would I migrate my data from a development dataset to a production dataset?” If that’s the case, there are a few different ways, with some exclusive to enterprise plans. However, on every plan there is the option to export (i.e., from dev) and import (i.e., to production). Details of that are
here .
If I’ve misunderstood your questions or you have additional ones, please let me know!
Great answer, cleared up some confusion. Thanks!
Great answer, cleared up some confusion. Thanks!
No problem!

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?