sanity-export
v1.0.6
Published
One-command solution to clone public Sanity datasets with parallel asset uploads and automatic import
Maintainers
Readme
Sanity Export
Truly one-command solution! Export public Sanity datasets and import them to your project with all assets and documents automatically uploaded.
⚠️ IMPORTANT: This CLI tool must be run from your target Sanity project directory (where
sanityis installed as a dependency). The tool uses the official Sanity CLI for importing, so it needs access to thesanitycommand.cd ~/your-sanity-project # Navigate to your Sanity project first npx sanity-export # Then run the tool
Features
- ✅ Fully automated - Export, upload assets, AND import documents in one command
- ✅ Parallel uploads - Upload 10 assets simultaneously for 10x faster performance
- ✅ No manual import needed - Tool handles everything via Sanity API
- ✅ No source auth required - Works with public datasets
- ✅ Assets properly uploaded - All images/files uploaded to your project
- ✅ CLI + npm package - Use with
npxor install globally - ✅ Interactive mode - Just run
npx sanity-exportand answer prompts - ✅ Configurable concurrency - Adjust parallel uploads (1-15) for your network
- ✅ Progress tracking - See download, upload, and import progress in real-time
Prerequisites
- Node.js 18 or higher
- A public Sanity dataset (the source you want to clone from)
- A target Sanity project (where Sanity is installed as a dependency)
- Sanity auth token for your target project
🚀 Quick Start (CLI Method - Recommended)
⚠️ Important: Run from Your Target Sanity Project Directory
The CLI must be run from your target Sanity project directory (where sanity is installed as a dependency). This ensures the sanity dataset import command works correctly.
# Navigate to your Sanity project first
cd ~/your-sanity-project
# Then run the CLI
npx sanity-exportChoose Your Approach:
🎯 Option 1: Interactive Mode (Recommended for Most Users)
The easiest way! Just answer the prompts:
npx sanity-exportThe tool will ask for:
- Source project ID (the public dataset)
- Target project ID (your project)
- Your Sanity auth token
- Dataset names (defaults to 'production')
- Output directory
- Concurrency (defaults to 10)
Then it does everything automatically! ✅
⚡ Option 2: One-Line Command (For Advanced Users/Scripts)
If you prefer command-line flags or need automation:
npx sanity-export clone \
--source-project r7m53vrk \
--target-project your-project-id \
--token sk_your_sanity_tokenBoth approaches do the same thing:
- ✅ Exports all documents from the source
- ✅ Downloads all assets (images/files)
- ✅ Uploads assets in parallel to your target project (10x faster!)
- ✅ Updates all references
- ✅ Runs
sanity dataset importwith your credentials - ✅ Complete! Dataset ready to use!
No manual steps required! The tool sets environment variables and runs the official Sanity CLI for you! 🎉
Get Your Auth Token
- Go to https://sanity.io/manage
- Select your project
- Click API → Tokens → Add API token
- Permissions: Editor
- Copy the token (starts with
sk)
📦 Installation
Use with npx (No Installation)
npx sanity-export clone --source-project abc --target-project xyz --token sk_...Global Installation
npm install -g sanity-export
sanity-export clone --source-project abc --target-project xyz --token sk_...Local Development
git clone https://github.com/ajay-develops/export-import-sanity-public-dataset-without-source-project-api-token.git
cd export-import-sanity-public-dataset-without-source-project-api-token
npm install
npm link # Makes CLI available globally💻 CLI Commands
Default: Interactive Mode ⭐ (Recommended for First-Time Users)
Just run without any arguments:
npx sanity-exportThe tool will prompt you for:
- Source project ID
- Target project ID
- Auth token
- Datasets (defaults provided)
- Output directory
- Concurrency (parallel uploads)
This is the easiest way to use the tool! No need to remember flags or syntax. 🎯
clone - One-Line Complete Solution (Recommended for Automation/Scripts)
For advanced users or CI/CD pipelines. Fully automated with command-line flags:
npx sanity-export clone \
--source-project r7m53vrk \
--target-project yfxnt4n8 \
--token sk_your_tokenWhat it does automatically:
- Exports from source
- Downloads assets
- Uploads assets to target (in parallel!)
- Updates references
- Runs
sanity dataset importautomatically (sets credentials via env vars) - Done! Dataset ready to use! 🎉
Use this if you:
- Want a scriptable/repeatable command
- Are setting up CI/CD automation
- Prefer command-line flags over interactive prompts
Otherwise, use interactive mode (simpler): npx sanity-export ⭐
Options:
-s, --source-project <id>- Source project ID (required)--source-dataset <name>- Source dataset (default: production)-t, --target-project <id>- Target project ID (required)--target-dataset <name>- Target dataset (default: production)-k, --token <token>- Sanity auth token (required)-o, --output <dir>- Output directory (default: current directory)-c, --concurrency <number>- Parallel uploads (default: 10, max: 15)
interactive - Explicit Interactive Mode
Same as running with no arguments:
npx sanity-export interactiveexport - Export Only
Just export without uploading assets:
npx sanity-export export \
--source-project r7m53vrk \
--output ./my-exportprepare-import - Prepare Existing Export
Upload assets and prepare import for an existing export:
npx sanity-export prepare-import \
--target-project yfxnt4n8 \
--token sk_your_token \
--input ./my-export📁 Output
After running clone or export, you'll have:
production.tar.gz- Ready-to-import archivedata-updated.ndjson- Documents with updated asset referencesasset-id-mapping.json- Old → New asset ID mappingassets-metadata.json- Asset metadataimages/- Downloaded image filesfiles/- Downloaded file assets
📖 How It Works
Visual Workflow
┌─────────────────────────────────────────────────────────────────┐
│ SOURCE PROJECT (Public) │
│ r7m53vrk / production │
│ 📄 Documents + 🖼️ Assets │
└────────────┬────────────────────────────────────────────────────┘
│
│ npm run export
│ (downloads via public API)
▼
┌─────────────────────────────────────────────────────────────────┐
│ LOCAL MACHINE │
│ 📁 data.ndjson (163 docs, no asset docs) │
│ 📁 assets-metadata.json (392 asset metadata) │
│ 📁 images/ (389 files) │
│ 📁 files/ (3 files) │
└────────────┬────────────────────────────────────────────────────┘
│
│ npm run prepare-import
│ (uploads assets, updates refs)
│
├──────────────────────┐
▼ ▼
┌─────────────────┐ ┌──────────────────┐
│ Upload Assets │ │ Update Refs │
│ to Target │ │ OLD → NEW IDs │
└────────┬────────┘ └────────┬─────────┘
│ │
▼ ▼
┌─────────────────────────────────────────────────────────────────┐
│ TARGET PROJECT (Yours) │
│ yfxnt4n8 / production │
│ 🖼️ 392 Assets uploaded ✅ │
└─────────────────────────────────────────────────────────────────┘
│
│ Auto-import via Sanity CLI
│ (SANITY_PROJECT_ID=yfxnt4n8 sanity dataset import)
▼
┌─────────────────────────────────────────────────────────────────┐
│ TARGET PROJECT (Complete) │
│ yfxnt4n8 / production │
│ 📄 163 Documents ✅ (auto-imported via CLI) │
│ 🖼️ 392 Assets ✅ (parallel uploaded) │
│ 🔗 All references working ✅ │
│ 🎉 Dataset ready to use! │
└─────────────────────────────────────────────────────────────────┘🛠️ Alternative: npm Scripts Method
If you cloned the repo locally, you can use npm scripts:
# 1. Configure .env.local
cp .env.example .env.local
nano .env.local # Add SOURCE_PROJECT_ID, TARGET_PROJECT_ID, SANITY_AUTH_TOKEN
# 2. Run export
npm run export
# 3. Prepare import
npm run prepare-import
# 4. Import
cd ~/your-sanity-project
pnpm dlx sanity dataset import ./export-import-sanity-public-dataset-without-source-project-api-token/production.tar.gz --replaceSee the npm scripts section for all available commands.
📝 Examples
⚠️ All examples should be run from your target Sanity project directory:
cd ~/your-sanity-project # Navigate to your Sanity project first!Example 1: Interactive Mode (Easiest - Recommended!) 🎯
Just run and answer the prompts:
npx sanity-exportWhen to use:
- First time using the tool
- Don't want to remember command flags
- Want guided step-by-step process
Example 2: One-Line Clone with Flags (Advanced) ⚡
npx sanity-export clone \
--source-project r7m53vrk \
--target-project yfxnt4n8 \
--token sk_your_token_hereWhen to use:
- You need a repeatable command
- Setting up automation/scripts
- Running in CI/CD pipeline
Example 3: Custom Output Directory
npx sanity-export clone \
--source-project r7m53vrk \
--target-project yfxnt4n8 \
--token sk_your_token \
--output ./my-dataset-backupExample 4: Different Source Dataset
npx sanity-export clone \
--source-project r7m53vrk \
--source-dataset staging \
--target-project yfxnt4n8 \
--token sk_your_tokenExample 5: Export Only (No Upload)
npx sanity-export export \
--source-project r7m53vrk \
--output ./backupExample 6: Custom Concurrency (Faster Uploads!)
npx sanity-export clone \
--source-project r7m53vrk \
--target-project yfxnt4n8 \
--token sk_token \
--concurrency 15Performance:
--concurrency 1- Sequential (slowest, ~20 minutes)--concurrency 5- Conservative (safe, ~10-12 minutes)--concurrency 10- Default (fast, ~5-8 minutes) ⭐--concurrency 15- Maximum (fastest, ~3-5 minutes)
Note: Higher concurrency = faster uploads but requires stable network. Start with default (10) and adjust based on your connection.
🔧 npm Scripts
For local development or custom workflows:
npm run export- Export documents and assets from source projectnpm run prepare-import- ⭐ Upload assets, update references, create import packagenpm run upload-assets- Upload assets only (for manual workflows)npm run transform- Re-run transformation on existing datanpm run clean- Remove all generated files
How It Works
Export Process (npm run export)
- Downloads documents from Sanity's public API
- Downloads assets (images and files) from their URLs
- Removes asset documents from data.ndjson and saves metadata
- Packages everything into
production.tar.gz
Import Process (Fully Automated!)
- Uploads assets in parallel (10 concurrent) via Sanity API
- Gets new asset IDs from Sanity's response
- Updates references in documents to use new IDs
- Creates final package with updated documents
- Auto-runs
sanity dataset importwith credentials set via environment variables:SANITY_PROJECT_ID=your-project \ SANITY_AUTH_TOKEN=your-token \ npx sanity dataset import production.tar.gz --replace
Why this works:
- Assets are uploaded FIRST, so all references resolve immediately
- Official Sanity CLI is used for import (reliable and standard)
- Environment variables provide authentication (no prompts needed)
Limitations
- Public datasets only - This tool works without authentication, so it can only access public datasets
- No drafts - Draft documents are private and won't be included
- No private content - Any content marked as private won't be exported
Troubleshooting
⚠️ CLI Must Run from Sanity Project Directory
Issue: "sanity: command not found" or import fails
Solution: Run the CLI from your target Sanity project directory (where sanity is installed):
# ✅ CORRECT - From your Sanity project
cd ~/your-sanity-project
npx sanity-export clone -s SOURCE -t TARGET -k TOKEN
# ❌ WRONG - From random directory
cd ~/Downloads
npx sanity-export # Will fail at import step!The tool needs access to the Sanity CLI to run sanity dataset import.
Memory Issues
Issue: JavaScript heap out of memory
Solution:
- Use the latest version:
npx sanity-export@latest - Reduce concurrency:
--concurrency 5(instead of 10) - Ensure Node.js is up to date:
node --version(18+ required)
During Export
"Dataset not found"
- Ensure dataset is public in source project settings
- Verify
SOURCE_PROJECT_IDin.env.localis correct
"Assets fail to download"
- Check network connection
- Script continues with other assets if some fail
During prepare-import
"TARGET_PROJECT_ID is required"
- Check
.env.localhasTARGET_PROJECT_ID,SANITY_AUTH_TOKEN
"Upload failed with status 401"
- Auth token is invalid or lacks Editor permissions
- Get new token from https://sanity.io/manage
"Upload failed with status 404"
- Dataset doesn't exist in target project
- Create it:
pnpm dlx sanity dataset create production
During Import
Import succeeds but images don't show
- Check
prepare-importcompleted successfully - Verify assets were uploaded (check console output)
- Run GROQ query:
count(*[_type == "sanity.imageAsset"])
Project Structure
export-sanity-public-dataset/
├── package.json # Dependencies and scripts
├── .env.example # Environment variable template
├── .env.local # Your configuration (git-ignored)
├── index.js # Export orchestrator
├── src/
│ ├── download-documents.js # Downloads NDJSON from Sanity
│ ├── download-assets.js # Downloads asset files
│ ├── transform-assets.js # Removes asset docs, saves metadata
│ ├── package-export.js # Creates tar.gz archive
│ ├── prepare-import.js # Uploads assets, updates refs, packages
│ └── upload-assets.js # Alternative asset upload script
└── README.md # This file🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Ideas for contributions:
- Progress bars for uploads
- Retry logic for failed uploads
- Support for private datasets
- Dry-run mode
- Enhanced error messages
📤 Publishing (For Maintainers)
Quick Publish Command
To publish a new version to npm and GitHub:
# For bug fixes (1.0.0 → 1.0.1)
npm version patch && git push origin main --tags
# For new features (1.0.0 → 1.1.0)
npm version minor && git push origin main --tags
# For breaking changes (1.0.0 → 2.0.0)
npm version major && git push origin main --tagsThis will:
- Update version in
package.json - Create a git tag
- Push to GitHub
- Trigger GitHub Actions to auto-publish to npm 🤖
See: GITHUB-ACTIONS-SETUP.md for complete setup guide
📄 License
MIT
