Sanity development best practices for schema design, GROQ queries, TypeGen, Visual Editing, images, Portable Text, Studio structure, localization, migrations, Sanity Functions, webhooks, Blueprints, and framework integrations such as Next.js, Nuxt, Astro, Remix, SvelteKit, Angular, Hydrogen, the App SDK, and Studios or apps running in the Sanity Dashboard (defineApplication). Use this skill whenever starting a new Sanity project (with or without a Sanity account via sanity new / sanity.new), working with Sanity schemas, defineType or defineField, GROQ or defineQuery, content modeling, Presentation or preview setups, Sanity-powered frontend integrations, event-driven content automation, documentEventHandler, defineDocumentFunction, defineMediaLibraryAssetFunction, @sanity/functions, @sanity/blueprints, sanity.blueprint.ts, event-driven content automation, building links that open a document or field in Sanity Studio, minting Sanity API or robot tokens, or when reviewing and fixing a San
Getting started
Full instructionsInstall the skill, then ask your AI tool for the task in plain words. The full SKILL.md below shows what it covers.
Instructions: SKILL.mdv1.0.11
Sanity Best Practices
Comprehensive best practices and integration guides for Sanity development, maintained by Sanity. Use the quick reference below to load only the one or two topic files that match the task.
When to Apply
Reference these guidelines when:
- Setting up a new Sanity project or onboarding (including without a Sanity account via
sanity new) - Integrating Sanity with a frontend framework (Next.js, Nuxt, Astro, Remix, SvelteKit, Hydrogen)
- Writing GROQ queries or optimizing performance
- Designing content schemas
- Implementing Visual Editing and live preview
- Working with images, Portable Text, or page builders
- Configuring Sanity Studio structure
- Setting up TypeGen for type safety
- Implementing localization
- Migrating content from other systems
- Building custom apps with the Sanity App SDK
- Developing or deploying a Studio or app in the Sanity Dashboard
- Managing infrastructure with Blueprints
- Automating content workflows with Sanity Functions or webhooks
Starting Without a Sanity Account
- If the user asks to build or set up a new Sanity project and there is no Sanity login or Sanity MCP session (check with
npx sanity@latest debug), do not stop and ask the user to log in or create a project manually. - Run
npx sanity@latest new --instructionsfirst and follow it to create an unclaimed project immediately (npx sanity@latest new "<project name>" --yes), scaffold or connect the Studio and frontend, seed sample content with the CLI (npx sanity@latest documents create seed.jsonfrom the Studio folder), and verify the app end to end. - Give the user the claim link and 72-hour expiry immediately so they can claim ownership while you keep building. Claiming does not interrupt work.
- Keep the claim link out of git, issues, PRs, and shared channels. Leave
SANITY_AUTH_TOKENinsanity/.env.local: never print it, pass it inline, commit it, or put it in frontend env files or browser-exposed variables (NEXT_PUBLIC_*,SANITY_STUDIO_*). - For the full logged-in (
sanity init) and no-account (sanity new) workflow, readreferences/get-started.md.
Global Rules
- Let Sanity generate
_idvalues for ordinary documents. Do not create deterministic UUIDs, slug-derived IDs, or legacy-system IDs when creating documents. - Model relationships with
referencefields, then resolve related documents with GROQ lookups, source-key fields, or returned_idvalues from created documents. - Use explicit document IDs mainly for singleton documents controlled by Studio Structure, including localized singletons such as
homePage-en. - Get the user's go-ahead before commands that change shared or production resources, can't be undone, or edit configuration outside the project: minting tokens,
sanity deploy,blueprints deployto a shared Stack,blueprints destroy,blueprints promote(one-way), CORS changes,mcp configure, and anydeleteorundeploycommand (datasets, documents, tokens, CORS origins, GraphQL APIs, deployed Studios). Schema deploys and read-only commands such asblueprints plandon't need one during development.
Video
- Do not store or serve video from Sanity
fileassets for production playback. File assets are delivered as raw downloads with no transcoding or adaptive streaming, and video traffic drives very high bandwidth usage and unexpectedly large bills. - On Enterprise plans with the video add-on, use Sanity Media Library for video: uploads are transcoded and streamed adaptively via Mux. Model video fields with
defineVideoField()fromsanity/media-libraryand play them with@mux/mux-player-reactusing the asset's playback ID. - On other plans, use a dedicated video service: install
sanity-plugin-mux-inputto upload and manage videos in your Mux account from the Studio, or host video on a platform such as YouTube or Vimeo and store only the embed URL in Sanity. - Small clips and short previews in a
filefield are acceptable, but any user-facing video at scale must go through Media Library or a streaming service.
Studio Edit Links
- Before building a URL that opens a document or a field in Studio (an "Edit in Studio" button, a link in a message or report), read https://www.sanity.io/docs/visual-editing/studio-edit-intent-links.md. Use the intent form,
{studioUrl}/intent/edit/id={id};type={type};path={path}: it opens the document wherever it sits in the Structure tree, andpathfocuses one field. - Encode the
pathvalue withencodeURIComponent. Studio splits each parameter on=, so an unencodedbody[_key=="abc"]is cut short atbody[_keyand the link focuses the wrong field. - Select array items by
_key, not by index, in links that are stored or sent. Path syntax: https://www.sanity.io/docs/studio/how-form-paths-work.md - With only a Content Source Map and no document ID, use
resolveEditUrlfrom@sanity/client/csminstead of building the URL by hand.
API & Robot Tokens
- When a task needs a Sanity project or organization token (CI deploys, dataset writes, App SDK deploys) and you already have Sanity authentication (CLI login or Sanity MCP), offer to mint a scoped robot token instead of asking the user to copy one from Manage. Confirm the label, role, and where the token will be stored, then mint it. Only ask the user to create one themselves if you are not authenticated or lack token-creation permission.
- Prefer the local CLI (
@sanity/cli8.19.0+; docs: https://www.sanity.io/docs/cli-reference/tokens.md) so you can pipe stdout JSON straight into the target secret store (such asgh secret set) without exposing the secret in agent output, logs, or committed files:- Project token:
npx sanity@latest tokens create "<label>" --project <projectId> --role=<role> --yes --json(e.g.viewer,editor,deploy-studio; neveradministrator). - Organization token:
npx sanity@latest tokens create "<label>" --organization <orgId> --grant <permission> --yes --json(e.g.--grant sdk-apps,--grant blueprints,--grant project:<projectId>=editor). - Add
--expires-at <date>when the token is only needed for a one-off task. Tokens never expire by default.
- Project token:
- When
tokensis unavailable (for example, through Sanity MCPrun_sanity_cli), usesanity api(docs: https://www.sanity.io/docs/cli-reference/cli-api.md;-fflags select POST automatically):sanity api projects/<projectId>/tokens -f label="<label>" -f roleName=<role>(returnskey), orsanity api v2025-07-11/access/<resourceType>/<resourceId>/robotswith bracket-fflags for nestedmembershipswhen--inputis unavailable (returnstoken; docs: https://www.sanity.io/docs/http-reference/access-api.md). - MCP
run_sanity_clicannot pipe to a shell, so the raw secret lands in your tool output. Only mint through MCP when the local CLI is unavailable, and tell the user the token will pass through the conversation before you do. Always grant the narrowest role required and never echo or commit the token.
Quick Reference
Integration Guides
get-started- Setting up a new Sanity project with an account (sanity init) or without one (sanity new/sanity.new)nextjs- Next.js App Router, Live Content API, standalone Studionuxt- Nuxt integration with @nuxtjs/sanityangular- Angular integration with @sanity/client, signals, resource API, Visual Editingastro- Astro integration with @sanity/astroremix- React Router / Remix integrationsvelte- SvelteKit integration with @sanity/svelte-loaderhydrogen- Shopify Hydrogen with Sanityproject-structure- Standalone Studio and monorepo patternsapp-sdk- Custom applications with Sanity App SDKdashboard- Studios and apps in the Sanity Dashboard: defineApplication, local dev, deploys, classic Studio registration, Dashboard hooksblueprints- Infrastructure as Code: blueprint files, stacks, plan/deploy workflow, error recovery, CI deploysfunctions- Automating content workflows with Sanity Functions and webhooks
Topic Guides
groq- GROQ query patterns, type safety, performance optimizationschema- Schema design, field definitions, validation, deprecation patternsvisual-editing- Presentation Tool, Stega, overlays, live previewpage-builder- Page Builder arrays, block components, live editingportable-text- Rich text rendering and custom componentsimage- Image schema, URL builder, hotspots, LQIP, Next.js Imagestudio-structure- Desk structure, singletons, navigationtypegen- TypeGen configuration, workflow, type utilitiesseo- Metadata, sitemaps, Open Graph, JSON-LDlocalization- i18n patterns, document vs field-level, locale managementmigration- Content import overview (see alsomigration-html-import)migration-html-import- HTML to Portable Text with @portabletext/block-tools
How to Use
Start with the single framework or topic guide that best matches the request, then read additional references only when the task crosses concerns. Use these reference files for detailed explanations and code examples:
references/groq.md
references/schema.md
references/nextjs.md
Each reference file contains:
- Comprehensive topic or integration coverage
- Incorrect and correct code examples
- Decision matrices and workflow guidance
- Framework-specific patterns where applicable
Explore more skills
Browse all- Sanity MigrationPlans, implements, and reviews migrations from other CMSes and content systems into Sanity. Use when migrating or…
- Sanity Studio ListenersKeep Sanity Studio customizations (custom tools, inputs, document views, structure and plugins) from opening a…
- Sanity SingletonsConfigure singleton documents in a Sanity Studio using the document.singletons registry. Use when a developer wants a…
- Sanity Studio UpgradeProduces a tailored Sanity Studio upgrade plan by inspecting the repository's installed versions, config, and source,…
- Senior FrontendBuild and review React, Next.js, TypeScript or Tailwind frontends: components, scaffolding, performance and bundle…
- UI Design SystemUse when building a design system: tokens, component docs, responsive scales or developer handoff. Generates tokens…
Security
Reviews (0)
More from Sanity
View source →Frequently asked questions about sanity-best-practices
What does the sanity-best-practices skill do?
Sanity development best practices for schema design, GROQ queries, TypeGen, Visual Editing, images, Portable Text, Studio structure, localization, migrations, Sanity Functions, webhooks, Blueprints, and framework integrations such as Next.js, Nuxt, Astro, Remix, SvelteKit, Angular, Hydrogen, the App SDK, and Studios or apps running in the Sanity Dashboard (defineApplication). Use this skill whenever starting a new Sanity project (with or without a Sanity account via sanity new / sanity.new), working with Sanity schemas, defineType or defineField, GROQ or defineQuery, content modeling, Presentation or preview setups, Sanity-powered frontend integrations, event-driven content automation, documentEventHandler, defineDocumentFunction, defineMediaLibraryAssetFunction, @sanity/functions, @sanity/blueprints, sanity.blueprint.ts, event-driven content automation, building links that open a document or field in Sanity Studio, minting Sanity API or robot tokens, or when reviewing and fixing a San It's a reusable SKILL.md instruction set that loads into your AI coding assistant on demand, no prompt engineering, no copy-pasting every session.
How do I install the sanity-best-practices skill?
Run `npx @skills-hub-ai/cli install sanity-best-practices` from your terminal. The CLI writes the SKILL.md to the correct location for your AI tool (e.g. ~/.claude/skills/sanity-best-practices/ for Claude Code or ~/.cursor/skills/ for Cursor with --target cursor) and adds it to your project's .skills.json lockfile.
Which AI tools does sanity-best-practices work with?
sanity-best-practices runs in Claude Code. It follows the open Agent Skills standard (SKILL.md), so the same skill works in every supported tool without modification.
Is the sanity-best-practices skill free?
Yes. Every skill on skills-hub.ai is free and open-source. There are no premium tiers, paywalls, or usage limits. You only pay for whatever AI assistant you're already using.
How do I use sanity-best-practices after installing it?
In Claude Code, type `/sanity-best-practices` (or whatever slash command the skill registers) and the AI follows the skill's instructions immediately. You can also reference it by name in natural language, your AI loads the skill into context when relevant.
Can I share the sanity-best-practices skill with my team?
Yes. Commit your project's .skills.json lockfile and teammates run `npx @skills-hub-ai/cli install` (no args) to install every skill at the exact version you pinned. Organization-scoped installs work via skills-hub.ai organizations.
Badge for your README
Show that this skill is on Skills Hub. The badge links back to this page and shows the live install count.
[](https://skills-hub.ai/skills/sanity-best-practices)<a href="https://skills-hub.ai/skills/sanity-best-practices"><img src="https://skills-hub.ai/api/badges/skill/sanity-best-practices" alt="Install on Skills Hub"></a>More options in the badge docs.