# Hygraph CLI overview

Learn how the declarative Hygraph CLI works, what teams use it for, and what it doesn't cover yet.

Manage your Hygraph schema as code with the declarative Hygraph CLI. You define the schema you want in a TypeScript config, yourself or with an AI coding agent such as Claude Code. Then you review the plan and apply it.

**Note:**
The Hygraph CLI is in beta. The config format and commands can still change between releases.

## What you can do

**Keep your schema in version control**

Export an existing environment as a TypeScript config and commit it. From then on, every schema change is a change to that file, so it gets the same review and history as your code.

**Review every change before it reaches Hygraph**

The CLI compares your config with the live schema and lists each change it would make, without changing anything. You can save that list, have someone review it, and apply exactly what they approved. If the config or the schema changed in the meantime, the CLI refuses the outdated list. For more information, see [Plan and apply schema changes](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/plan-and-apply-schema-changes-cli).

**Protect content from accidental deletes**

Deleting a model, field, or locale also deletes its content. The CLI stops before any destructive change, lists it, and applies it only when you allow it explicitly. For more information, see [applying destructive changes](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/plan-and-apply-schema-changes-cli#destructive-changes-with-prune).

The CLI only guards changes made through the CLI. Team members can still change or delete schema elements in Studio. To prevent that, give them a role without schema permissions, such as the Editor or Contributor system role, or a [custom role](https://hygraph.com/docs/getting-started/access-and-permissions/user-roles-and-permissions#custom-roles) without the update and delete permissions for schema elements.

**Align environments**

When your environments were changed by hand over time, they no longer match each other. The CLI shows how each one differs from your config, so you can decide what to keep before you apply anything. For more information, see [Adopt the CLI on an existing project](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/plan-and-apply-schema-changes-cli#adopt-the-cli-on-an-existing-project).

**Test in staging, then ship to production**

Apply a change to staging, test it, then apply the same config to `master`. Both environments get the change from one config, so they end up with the same schema. For more information, see [Move a change from staging to master](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/plan-and-apply-schema-changes-cli#move-a-change-from-staging-to-master).

**Automate schema changes in CI**

Let your pipeline apply changes to staging automatically, and apply to `master` only after a person approves the change. For more information, see [Run the Hygraph CLI in CI](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/run-cli-in-ci).

**Keep changes made outside the CLI**

When someone changes the schema outside the CLI, such as in Studio or through an AI assistant, the CLI shows the difference against your config. You can add the change to the config, so the next apply doesn't undo it. For more information, see how to handle a [schema changed outside the CLI](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/plan-and-apply-schema-changes-cli#schema-changed-outside-the-cli). To stop schema changes in Studio, turn on [Schema Lock](https://hygraph.com/docs/developer-guides/project/schema-lock) for the environments the CLI manages.

**Add a project or environment with the same schema**

Add a new environment or project to the config, and apply. The CLI creates the whole schema there from the same config. For more information, see how to [add a project](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/share-schema-across-projects-cli#add-a-project), [target several projects](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/define-your-schema-in-code-cli#projects), and [target several environments](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/define-your-schema-in-code-cli#environments).

**Share one schema across projects**

Run several brands or markets from one shared schema, and change it per project where they differ. A change to the shared schema reaches every project the next time you apply. For more information, see how to [share a schema across projects](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/share-schema-across-projects-cli).

**Work with AI coding agents**

An AI coding agent such as Claude Code or Cursor can design new models from a prompt, change existing ones, or organize a large config into folders and files. You can check the plan before anything is applied to your Hygraph project. For more information, see [Use the Hygraph CLI with AI coding agents](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/use-cli-with-ai-agents).

## Limitations

The Hygraph CLI is in beta, and some parts of a schema are not supported yet. Use these alternatives:

| Not in the CLI yet | What to do instead |
| --- | --- |
| Webhooks, workflows, app installations, and sidebar panels from legacy UI extensions | Use Studio or the [Management SDK](https://hygraph.com/docs/developer-guides/schema-as-code/management-sdk/management-sdk-overview). |
| Custom form settings other than slug settings, rich text toolbar actions, and app field elements | Use Studio. |
| Custom fields on the User model, and on system models and components that apps create | Use Studio. |
| Rich text fields that embed assets, and references to Asset, User, or other system models | Use Studio. To connect a model to assets, use an asset field. |
| Refreshing a GraphQL remote source's schema | [Refetch the remote schema in Studio](https://hygraph.com/docs/developer-guides/remote-data/troubleshooting#refreshing-a-graphql-remote-source), or call [`refreshGraphQLRemoteSourceSchema()`](https://hygraph.com/docs/api-reference/management-sdk/management-sdk-methods-reference#refreshgraphqlremotesourceschema) with the Management SDK. |
| Deleting a stage, deleting the default locale, or changing the default locale | Use Studio. |
| Renaming a remote field, a remote source prefix, or a stage API ID | For a remote field, change its `apiId`. The plan deletes and recreates it, and a remote field holds no content. For a stage, delete it in Studio, then run `plan` again. |
| Writing live changes back to the config | `pull` only prints them. Edit the config, or export again. |
| A dry run against your content | Changes that depend on content, such as making a field required, fail at apply instead. The failed migration applies nothing. |
| Changing or moving content, such as copying values into a new field | The CLI only changes the schema. Use [Content API mutations](https://hygraph.com/docs/api-reference/content-api/mutations). |

## How it works

![Hygraph CLI workflow](https://hygraph.com/images/docs/developer-guides/schema-as-code/hygraph-cli-workflow.svg)

With the Hygraph CLI, your schema lives in the `hygraph.config.ts` file.

1. Export your project's schema to a config file, `hygraph.config.ts`.
2. Commit the `hygraph.config.ts` file to version control.
3. Edit the `hygraph.config.ts` file to change the schema. You can edit it yourself or with an AI coding agent.
4. Compare the config with the live environment and list every change.
5. Apply the planned changes to Hygraph as one migration. If the migration fails, nothing is applied.
6. Commit the updated `hygraph.config.ts` file to version control.

## FAQ

**How do I start using the CLI on an existing project?**

Export the environment closest to the schema you want, usually `master`, and add a target for the other environment. Then plan each one, and review the differences before you apply, as described in [Adopt the CLI on an existing project](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/plan-and-apply-schema-changes-cli#adopt-the-cli-on-an-existing-project).

**How do I manage multiple environments?**

Give each environment its own target in the config, with its own token, as described in [Environments](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/define-your-schema-in-code-cli#environments). Then choose how the environments relate:

- **The same schema in every environment:** [Move each change from `staging` to `master`](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/plan-and-apply-schema-changes-cli#move-a-change-from-staging-to-master). Apply the change to `staging` first, test it, then apply the same config to `master`.
- **A different schema in each environment**, such as a field in `staging` only: Define the shared models once, then [copy a model for each environment](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/define-your-schema-in-code-cli#different-schema-in-each-environment) and add the fields only that environment has.

**Can I compare two environments with the CLI?**

Not directly, but you can [plan one environment's config against another environment](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/plan-and-apply-schema-changes-cli#compare-two-environments). The plan lists what would change to make the second environment match the first. For a direct comparison of two environments in the same project, use [environment diff](https://hygraph.com/docs/developer-guides/schema-as-code/management-sdk/diff-two-environments).

**How do I share a schema across projects?**

Keep the [shared schema in one file](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/share-schema-across-projects-cli), and build each project's config from it. Each project can use the whole schema, change parts of it, or leave parts out.

**How do I change a field's type without losing content?**

Changing a field's type deletes the field and its content. To keep the content, [add a field of the new type, copy the values into it, then remove the old field](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/define-your-schema-in-code-cli#change-a-fields-type-without-losing-content).

**How do I deprecate a field?**

[Hide the field from editors](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/define-your-schema-in-code-cli#deprecate-a-field) with `visibility: "HIDDEN"`, and update the queries that use it. Then remove it from the config, and apply with `--prune`.

**How do I migrate content?**

The CLI only changes the schema. To change or move content, use [Content API mutations](https://hygraph.com/docs/api-reference/content-api/mutations).

## What's next

- [Get started with the Hygraph CLI](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/get-started-cli): Install the CLI, export your schema, and make your first change.
- [Define your schema in code](https://hygraph.com/docs/developer-guides/schema-as-code/hygraph-cli/define-your-schema-in-code-cli): Learn how the config is organized.
- [Hygraph CLI commands reference](https://hygraph.com/docs/api-reference/hygraph-cli/commands-cli-reference): Look up every command and flag.
- [Hygraph CLI config file reference](https://hygraph.com/docs/api-reference/hygraph-cli/config-file-cli-reference): Look up every function and option in `hygraph.config.ts`.
