How to configure dev environment to fetch data from local dataset and migrate data to production dataset.
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.gzImport to your production dataset:
sanity dataset import backup.tar.gz productionA 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.
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.