👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Command Line Interface (CLI)

Build, deploy, init plugin boilerplate, run scripts, and wrangle datasets and webhooks, all from the command line

# Installing the CLI globally
npm install --global sanity@latest

# Alternatively
yarn global add sanity@latest
pnpm install --global sanity@latest

# Running the CLI without global installation
npx -y sanity@latest [command]

The sanity Command Line Interface (CLI) is a collection of tools for managing, developing, debugging, and deploying your Sanity Studio projects as well as running scripts to migrate or manipulate your data.

The CLI can be installed as a global dependency in your development environment, locally on a per-project basis, or in many cases, accessed entirely using npx sanity@latest [command], which bypasses the need to install the CLI globally.

Gotcha

Whether you choose to install the sanity CLI or use it only with npx, you will need node and npm installed on your system.

How to install node and npm?

The CLI configuration file

You can add project-specific CLI configuration by adding a file named sanity.cli.js (.ts) in your project‘s root folder.

Minimal example

// sanity.cli.js
import { defineCliConfig } from "sanity/cli";

export default defineCliConfig({
  api: {
    projectId: "<your-project-id>",
    dataset: "production",
  }
});

Advanced example

You can embed further settings in your CLI configuration file, including specifying the local server port for sanity dev, GraphQL deployments, and extending the Vite configuration.

// sanity.cli.js
import { defineCliConfig } from "sanity/cli";

export default defineCliConfig({
  api: {
    projectId: "<your-project-id>",
    dataset: "production",
  },
  server: {
    hostname: "localhost",
    port: 3333,
  },
  graphql: [{
    tag: "default",
    playground: true,
    generation: "gen3",
    nonNullDocumentFields: false,
  }],
  vite: (config) => config,
});

Was this article helpful?