Sanity CLI
$ sanity Official Med RiskSanity CLI (sanity) manages Sanity Content Lake datasets, executes GROQ queries from the terminal, validates schemas, and exports/imports dataset backups. It relies heavily on a local project directory with sanity.config.ts and SANITY_AUTH_TOKEN for headless runs.
npm install -g @sanity/cli Use sanity CLI for dataset export/import, schema deploy, and local GROQ testing; use Sanity MCP when working remotely without a local Sanity Studio project checkout.
Agent-Readiness Checklist
Static evaluation (not yet probed on this host). Run npm run data:probe locally to capture live verification.
FCRS-1.0 6-Dimension Breakdown
sanity documents query emits clean JSON, though project-scoped commands require running inside a Sanity project directory.
sanity documents queryreturns JSON arrays directly- Most dataset commands require
sanity.config.tsin the working directory
Dataset selection (-d) and read-only SANITY_AUTH_TOKEN scopes protect production content, though dataset delete is destructive.
- Supports scoped API tokens via
SANITY_AUTH_TOKEN sanity dataset import --replacecan overwrite documents if misused
Standard npm package usable globally or via npx sanity@latest across macOS, Linux, and Windows.
- Published as
@sanity/cliand bundled withsanity
Great for GROQ debugging, schema extraction, and dataset backups.
- Native GROQ execution and
schema deployworkflows
Official Sanity monorepo (sanity-io/sanity) maintained under MIT.
- Official Sanity GitHub organization
Documented in Sanity's official CLI reference and --help output.
- Official docs at sanity.io/docs/cli-reference
Security, Auth & Guardrails
Stores auth tokens in ~/.config/sanity/config.json or reads SANITY_AUTH_TOKEN from the environment.
CLI is free (MIT); Sanity includes a free tier with usage-based plans for higher quotas.
# Sanity CLI Agent Rules - Always target non-production datasets with `-d staging` (or `--dataset=staging`) when querying or testing mutations. - Export a backup (`sanity dataset export`) before running migrations or `sanity dataset import --replace`. - Never run `sanity dataset delete` or `sanity documents delete` without explicit human approval.
Agent-Ready Command Recipes
Safe Read-Only Queries
sanity dataset list
Lists all datasets and dataset aliases configured for the current Sanity project.
sanity documents query "*[_type == 'post'][0...5]{_id, title}" -d staging Executes a bounded GROQ query against the staging dataset and outputs JSON.
Controlled Writes & Previews
sanity dataset export staging ./staging-backup.tar.gz
Downloads a full local archive of the staging dataset without mutating remote documents.
Strengths for Agents
- +
sanity documents queryruns GROQ directly from the shell and returns clean JSON. - +
sanity dataset exportmakes pre-migration snapshots effortless. - + Supports headless CI authentication via
SANITY_AUTH_TOKEN.
Caveats & Pitfalls
- ! Most commands fail outside a directory containing
sanity.config.ts/sanity.cli.ts. - ! Missing universal
--jsonflag on administrative commands likedataset list.