Skip to content
Dec 14th - Learn how Tecovas roped in success with Sanity and Shopify 🤠
Sanity
    • Platform

      Sanity Studio

      Flexible editing environment

      APIs

      Connect to anything

      Content Lake

      Real-time database

      Try product demo

      Features

      Real-time collaboration

      Fearlessly work with content

      Precise content querying

      Treat content as data with GROQ

      Localization

      Coherent messaging across territories

    • Use cases

      E-commerce

      Richer shopping experiences

      Marketing sites

      Control your story

      Products & services

      Innovate and automate

      Mobile apps

      Content backend for every OS

      View all

      Integrations

      Shopify
      Mux
      Vercel
      Netlify
      Algolia
      Cloudinary
      BigCommerce
      Commerce Layer
      Smartling
      Transifex
      View all
    • Learn

      Documentation
      Studio API Reference
      API reference
      Guides
      GROQ cheat sheet
      Sanity UI
      Get started

      Build and share

      Templates
      Tools and plugins
      Schemas and snippets
      Project showcase
      Share your work
      Browse Exchange

      Frameworks

      React
      Vue
      Next.js
      Nuxt.js
      Svelte
      Remix
      Gatsby
      Astro
      Angular
      Eleventy
      View all
    • Discover

      Blog
      Resource library
      Glossary
      Agency partners
      Become a partner
      Technical support
      Talk to sales

      Case studies

      Puma

      Source of truth for global markets

      Aether

      Unique digital shopping experience

      Morning Brew

      Omnichannel media distribution

      InVision

      Delivering exceptional customer experiences

      View all

      Popular guides

      Headless CMS
      Structured content
      Content modeling
      Headless SEO
      Static websites
      View all
    • Enterprise
    • Pricing
    • Log in
    • Contact sales
    • Get started
Contact salesGet started
Published October 19th 2018

5 neat tricks you can make the Sanity CLI do

The Sanity CLI can do many things, but here are five of them you should check out.

Knut Melvær

Head of Developer Community and Education

At the Sanity Head Quarters, we live and breathe in the terminal. And honestly, we keep developing the Sanity CLI tool because we use it ourselves every day. I want to take this moment to share some of the cool things you can do with it. And yes, I promise that number 5 will be worth your attention 🤯.

1. Query your data

sanity documents query "*[_type == 'post']{_id, title}"

You can do a couple of things with the sanity documents command, but one of those we use the most is the one for querying a dataset. Running this in your project folder will give you your data printed out right in the terminal. If you add --pretty, you'll get some nice highlighting too. This is handy for testing queries and exploring your content, but if you think about it, it's also a way to use those UNIX superpowers on your content. For example, you can use it to export your data to a csv file. I use it so often that I have added alias sdq="sanity documents query" to my shell configuration.

2. Export a dataset in a neat package

sanity dataset export <datasetName>

The whole point of a structured content management system is that it should be easy to get your content out of it. Usually that's by querying the API for the content you want, but sometimes, you want it all, at once, neatly packaged. This command exports all your documents in a .ndjson-file and your assets to a dedicated folder. It downloads a gzipped file, which you also can import to a new dataset with sanity dataset import. You can also, for example, unpack the file, run a find-and-replace-all operation on all your data, and import the dataset.ndjson file to a dataset again.

Getting ALL THE DATA

3. Manage CORS settings

sanity cors add http://localhost:8000

CORS is a thing. It's easy to forget it when you're on a roll building your new frontend. Then it's nice to have an easy way to add a new CORS origin right from the command line. You can delete and list them out too.

4. Get an overview of your projects

sanity projects list

Since you can have as many free developer plans as you desire, and starting a new project (sanity init) is rather easy, it doesn't take long before you have a handful of projects in your account. Then it's nice to have an easy way to list out all the projects with some key information tied to your account.

5. Create and edit a document in your favorite editor

sanity documents create --id myDocId --watch --replace

This command is my favorite. It creates a new document, opens it in the EDITOR you've set in your terminal (mine is export EDITOR='code -w'), and lets you live edit that document while it's being synced to the datastore. If you put an existing document id in the myDocId spot, you'll get that document and be able to edit it. Run sanity documents create --help to learn more about what this command can do.

Page content

  • 1. Query your data
  • 2. Export a dataset in a neat package
  • 3. Manage CORS settings
  • 4. Get an overview of your projects
  • 5. Create and edit a document in your favorite editor

Product

Sanity StudioAPIsContent LakeSecurity & Compliance
  • Sanity vs Contentful
  • Sanity vs Strapi
  • Sanity vs Wordpress
  • Sanity vs Adobe Experience Manager
  • Sanity vs Hygraph
  • Sanity vs Sitecore
  • Sanity vs Storyblok
  • Sanity vs Contentstack
  • Sanity vs Prismic
  • Sanity vs Drupal
  • Sanity vs ButterCMS

Resources

DocumentationBlogResource libraryCase Studies
  • React Blog
  • Gatsby Blog
  • Next.js Landing Pages
  • Making a PWA
  • Single Page Application
  • Svelte & Typescript App
  • Vue & Tailwind Blog
  • Developer Portfolio Templates
  • Form validation with Yup
  • Live Preview with Next.js and Sanity.io
  • Next.js blog
  • Next.js personal website
  • Clean Next.js + Sanity app
  • Clean Remix + Sanity app
  • Clean SvelteKit + Sanity app
  • All Templates
  • Agency partners
  • Technology partners
  • Headless CMS 101
  • Static Sites 101
  • Headless Commerce 101
  • CMS for enterprise
  • Headless SEO
  • Localization
  • Content as a Service
  • What is a DXP?
  • Typescript 101
  • Ecommerce SEO
  • What is a Composable DXP?
  • What is an API?
  • GraphQL vs REST
  • React CMS
  • Next.JS CMS
  • CMS for Shopify
  • API-first CMS
  • Content platform
  • Multilingual CMS
  • Static Site CMS
  • Gatsby CMS
  • Node CMS
  • E-commerce CMS
  • Vue CMS
  • Angular CMS
  • GraphQL CMS
  • Newspaper CMS
  • Magazine CMS
  • CMS for apps
  • Remix CMS
  • Nuxt CMS
  • SvelteKit CMS
  • Agile CMS
  • Eleventy CMS
  • Multisite CMS

Company

Contact SalesEnterpriseCareersTerms of ServicePrivacy PolicyAccessibility Statement

Stay connected

  • GitHub
  • Slack
  • Twitter
  • YouTube
  • Stack Overflow
  • Blog RSS
  • Newsletter
©Sanity 2023