# Export schema definition

Use the Management API to export the full schema definition of an environment, then import it into another project or environment, or save it in your own version control system.

You can export the schema definition of an environment in your project with the Management API or the Management SDK. You can then import the schema definition to a project within the same environment or a different one, or save it in your own version control system.

The export always includes your complete project schema. There's no option to export just one model or component. Because importing is additive, you can edit the schema definition to keep only specific elements before you import it, but you're responsible for spotting dependencies and naming conflicts yourself. Use [Import schema elements](https://hygraph.com/docs/developer-guides/schema/import-schema) in Studio instead for a list of elements to pick from, automatic dependency resolution, and automatic conflict resolution that renames the incoming element for you.

## How do you export your schema definition?

## 1. Get the environment name

First, you need the environment name. For this query, you need to provide the project ID. You can find the Project ID in [Project Settings](https://hygraph.com/docs/developer-guides/project/manage-project-info) or in the project URL - `https://app.hygraph.com/<projectId>/<environmentId>/`.

**Warning:**
  In the **API Playground**, use the **API selector** dropdown to select the
  **Management API**.

The query to the `Management Api` looks like this:

```graphql
query MyQuery {
  viewer {
    project(id: "<your-project-id>") {
      environments {
        name
        id
      }
    }
  }
}
```

## 2. Export the schema definition

After getting the environment name, export the schema definition of that environment. Run this query using the **Management API**.

  **Query**

```graphql
query schemaAsCode {
  viewer {
    project(id: "<your-project-id>") {
      environment(name: "<your-environment-name>") {
        schemaDefinition
      }
    }
  }
}
```

  
  **Response**

```json
{
  "data": {
    "viewer": {
      "project": {
        "environment": {
          "schemaDefinition": [
            {
              "createModel": {
                "apiId": "Post",
                "apiIdPlural": "Posts",
                "description": "",
                "displayName": "Post"
              }
            },
            {
              "createRelationalField": {
                "apiId": "image",
                "parentApiId": "Post",
                "type": "ASSET",
                "displayName": "Image",
                "description": null,
                "isList": false,
                "isRequired": false,
                "formRenderer": "GCMS",
                "formExtension": null,
                "formConfig": {},
                "tableRenderer": "GCMS",
                "tableExtension": null,
                "tableConfig": {},
                "reverseField": {
                  "apiId": "imagePost",
                  "modelApiId": "Asset",
                  "displayName": "imagePost",
                  "description": null,
                  "isList": true,
                  "visibility": "API_ONLY",
                  "isUnidirectional": false,
                  "position": 1
                },
                "visibility": "READ_WRITE",
                "visibilityCondition": null,
                "position": 2,
                "isVariantEnabled": false
              }
            },
            {
              "createSimpleField": {
                "apiId": "content",
                "parentApiId": "Post",
                "type": "RICHTEXT",
                "displayName": "Content",
                "description": null,
                "initialValue": null,
                "tableRenderer": "GCMS",
                "formRenderer": "GCMS",
                "tableExtension": null,
                "formExtension": null,
                "formConfig": {},
                "tableConfig": {},
                "isList": false,
                "isLocalized": false,
                "isRequired": false,
                "isUnique": false,
                "isHidden": false,
                "embeddableModels": [
                  "Asset"
                ],
                "visibility": "READ_WRITE",
                "isTitle": false,
                "position": 1,
                "validations": null,
                "embedsEnabled": true,
                "visibilityCondition": null,
                "isVariantEnabled": false
              }
            },
            {
              "createSimpleField": {
                "apiId": "title",
                "parentApiId": "Post",
                "type": "STRING",
                "displayName": "Title",
                "description": null,
                "initialValue": null,
                "tableRenderer": "GCMS_SINGLE_LINE",
                "formRenderer": "GCMS_SINGLE_LINE",
                "tableExtension": null,
                "formExtension": null,
                "formConfig": {},
                "tableConfig": {},
                "isList": false,
                "isLocalized": false,
                "isRequired": false,
                "isUnique": false,
                "isHidden": false,
                "embeddableModels": [],
                "visibility": "READ_WRITE",
                "isTitle": true,
                "position": 0,
                "validations": null,
                "embedsEnabled": null,
                "visibilityCondition": null,
                "isVariantEnabled": false
              }
            }
          ]
        }
      }
    }
  }
}
```


The response lists the complete project schema definition in the specified environment. You can now save this schema definition in your version control system, or import the schema in another project.

## 3. (Optional) Import schema to another project

**Before you import the schema to another project::**

    - Ensure that the project you are importing the schema into does not contain
    any existing entities with the same names as those in the new schema. If any
    such entry exists, your schema import will fail.

    - Verify that the target project has sufficient
    <a href="/docs/developer-guides/project/update-billing#commercial-limits">commercial limits</a> for all the entities to be imported, else your import will fail.

In Studio, navigate to the project that you want to import the schema. This project can be in the same or a different environment. In the **API Playground**, use the **API selector** dropdown to select the **Management API**.

Here's an example mutation in the `ManagementApi`. You can find the target environment ID by using the query in [Step 1](#1-get-the-environment-name).

  **Query**

```graphql
mutation ApplySchemaDefinitionMutation(
  $schemaDefinition: [BatchMigrationChangeInput!]!
) {
  submitBatchChanges(
    data: { environmentId: "<your-environment-id>", changes: $schemaDefinition }
  ) {
    migration {
      id
      status
    }
  }
}
```

  
  **Variable**

```json
{
  "schemaDefinition": [
    {
      "createModel": {
        "apiId": "Post",
        "apiIdPlural": "Posts",
        "description": "",
        "displayName": "Post"
      }
    },
    {
      "createRelationalField": {
        "apiId": "image",
        "parentApiId": "Post",
        "type": "ASSET",
        "displayName": "Image",
        "description": null,
        "isList": false,
        "isRequired": false,
        "formRenderer": "GCMS",
        "formExtension": null,
        "formConfig": {},
        "tableRenderer": "GCMS",
        "tableExtension": null,
        "tableConfig": {},
        "reverseField": {
          "apiId": "imagePost",
          "modelApiId": "Asset",
          "displayName": "imagePost",
          "description": null,
          "isList": true,
          "visibility": "API_ONLY",
          "isUnidirectional": false,
          "position": 1
        },
        "visibility": "READ_WRITE",
        "visibilityCondition": null,
        "position": 2,
        "isVariantEnabled": false
      }
    },
    {
      "createSimpleField": {
        "apiId": "content",
        "parentApiId": "Post",
        "type": "RICHTEXT",
        "displayName": "Content",
        "description": null,
        "initialValue": null,
        "tableRenderer": "GCMS",
        "formRenderer": "GCMS",
        "tableExtension": null,
        "formExtension": null,
        "formConfig": {},
        "tableConfig": {},
        "isList": false,
        "isLocalized": false,
        "isRequired": false,
        "isUnique": false,
        "isHidden": false,
        "embeddableModels": [
          "Asset"
        ],
        "visibility": "READ_WRITE",
        "isTitle": false,
        "position": 1,
        "validations": null,
        "embedsEnabled": true,
        "visibilityCondition": null,
        "isVariantEnabled": false
      }
    },
    {
      "createSimpleField": {
        "apiId": "title",
        "parentApiId": "Post",
        "type": "STRING",
        "displayName": "Title",
        "description": null,
        "initialValue": null,
        "tableRenderer": "GCMS_SINGLE_LINE",
        "formRenderer": "GCMS_SINGLE_LINE",
        "tableExtension": null,
        "formExtension": null,
        "formConfig": {},
        "tableConfig": {},
        "isList": false,
        "isLocalized": false,
        "isRequired": false,
        "isUnique": false,
        "isHidden": false,
        "embeddableModels": [],
        "visibility": "READ_WRITE",
        "isTitle": true,
        "position": 0,
        "validations": null,
        "embedsEnabled": null,
        "visibilityCondition": null,
        "isVariantEnabled": false
      }
    }
  ]
}
```


In the above example, `$schemaDefinition` is the environment variable with the value equal to the `schemaDefinition` object from the query in [Step 2](#2-export-the-schema-definition).

## Supported schema elements

The following schema elements are supported:

    <div className="flex flex-col md:flex-row md:gap-4">
      <div className="flex-1">
        - Models
        - Components
        - Locales
        - Simple fields
        - Conditional visibility in fields
        - Relational fields
        - Enumerations
        - Enumerable fields
        - Initial values in enumeration fields
      </div>
      <div className="flex-1">
        - Stages
        - Union fields
        - Apps
        - App field elements
        - Sidebar elements
        - Remote fields
        - Remote type definitions
        - Remote sources
      </div>
    </div>

**Warning:****Not supported:** UI extensions.

## Limitations

Note the following limitations when you export and import a schema definition.

### Existing schema in new project

Importing a schema in a project is an additive process. This means that the existing project schema is not replaced, nor is it merged. You need to ensure that the schema does not contain any existing entities with the same names as those in the new schema. If any such entry exists, your schema import will fail.

### Remote Sources with OAuth

If a remote source uses `OAuth authentication`, the code will exclude the `clientSecret`. You must manually add the actual secret before importing the new schema.

  **Query**

```graphql
query schemaAsCode {
  viewer {
    project(id: "8845d7cc-668d-4a5b-93d9-66d947b95fe9") {
      environment(name: "master") {
        schemaDefinition
      }
    }
  }
}
```

  
  **Result**

```json
{
  "data": {
    "viewer": {
      "project": {
        "environment": {
          "schemaDefinition": [
              {
                "createRESTRemoteSource": {
                  "debugEnabled": true,
                  "description": "",
                  "displayName": "CommerceLayer",
                  "headers": {},
                  "kind": "CommerceLayer",
                  "oAuth": {
                    "authorizationGrantType": "client_credentials",
                    "authorizationUrl": "https://hyg.commercelayer.io/oauth/token",
                    "clientId": "F5J9l-1q8SwIRRbYuY_h1Vb-pOFzJjOTcO4HfyM_XMI",
                    "clientSecret": "CLIENT_SECRET",
                    "scopes": ""
                  },
                  "prefix": "CommerceLayer",
                  "url": "https://hyg.commercelayer.io/api"
                }
              }
            ]
          }
        }
      }
    }
  }
}
```


You must manually replace `CLIENT_SECRET` with your actual secret before applying the batch mutation.

So, for instance, this:

```json
"clientSecret": "CLIENT_SECRET",
```

Would turn into this:

```json
"clientSecret": "23sad-129132", //actual secret value
```
