Schema

Retrieve and update the schema of an instance.

Apply Schema Difference

Update the instance's schema by passing the diff previously retrieved via `/schema/diff` endpoint in the JSON request body or a JSON/YAML file. This endpoint is only available to admin users. Only the operations in the diff are applied, so a diff from a partial snapshot leaves everything outside its scope untouched. The `hash` is always checked against the full schema.

Query Parameters

force
boolean

When true, bypasses the hash check and applies the diff regardless of whether the current schema matches the snapshot it was generated from.

Request Body

hash
string
diff
object

Responses

Successful request
import { createDirectus, rest, schemaApply } from '@directus/sdk';

const client = createDirectus('directus_project_url').with(rest());

const result = await client.request(
  schemaApply({
    hash: '7d4499bc1e09bab3558b46cc09cd83fea2c05144',
    diff: {
      collections: [
        {
          collection: 'articles',
          diff: [
            {
              kind: 'N',
              rhs: {
                collection: 'articles',
                meta: {
                  accountability: 'all',
                  archive_app_filter: true,
                  archive_field: null,
                  archive_value: null,
                  collapse: 'open',
                  collection: 'articles',
                  color: null,
                  display_template: null,
                  group: null,
                  hidden: false,
                  icon: null,
                  item_duplication_fields: null,
                  note: null,
                  singleton: false,
                  sort: null,
                  sort_field: null,
                  translations: null,
                  unarchive_value: null,
                },
                schema: {
                  name: 'articles',
                },
              },
            },
          ],
        },
      ],
      fields: [
        {
          collection: 'articles',
          field: 'id',
          diff: [
            {
              kind: 'N',
              rhs: {
                collection: 'articles',
                field: 'id',
                type: 'integer',
                meta: {
                  collection: 'articles',
                  conditions: null,
                  display: null,
                  display_options: null,
                  field: 'id',
                  group: null,
                  hidden: true,
                  interface: 'input',
                  note: null,
                  options: null,
                  readonly: true,
                  required: false,
                  sort: null,
                  special: null,
                  translations: null,
                  validation: null,
                  validation_message: null,
                  width: 'full',
                },
                schema: {
                  name: 'id',
                  table: 'articles',
                  data_type: 'integer',
                  default_value: null,
                  max_length: null,
                  numeric_precision: null,
                  numeric_scale: null,
                  is_nullable: false,
                  is_unique: false,
                  is_primary_key: true,
                  is_generated: false,
                  generation_expression: null,
                  has_auto_increment: true,
                  foreign_key_table: null,
                  foreign_key_column: null,
                },
              },
            },
          ],
        },
        {
          collection: 'articles',
          field: 'title',
          diff: [
            {
              kind: 'N',
              rhs: {
                collection: 'articles',
                field: 'title',
                type: 'string',
                meta: {
                  collection: 'articles',
                  conditions: null,
                  display: null,
                  display_options: null,
                  field: 'title',
                  group: null,
                  hidden: false,
                  interface: 'input',
                  note: null,
                  options: null,
                  readonly: false,
                  required: false,
                  sort: null,
                  special: null,
                  translations: null,
                  validation: null,
                  validation_message: null,
                  width: 'full',
                },
                schema: {
                  name: 'title',
                  table: 'articles',
                  data_type: 'varchar',
                  default_value: null,
                  max_length: 255,
                  numeric_precision: null,
                  numeric_scale: null,
                  is_nullable: true,
                  is_unique: false,
                  is_primary_key: false,
                  is_generated: false,
                  generation_expression: null,
                  has_auto_increment: false,
                  foreign_key_table: null,
                  foreign_key_column: null,
                },
              },
            },
          ],
        },
      ],
      relations: [],
    },
  })
);

Retrieve Schema Difference

Compare the current instance's schema against the schema snapshot in JSON request body and retrieve the difference. This endpoint is only available to admin users. Alternatively, upload a JSON or YAML schema file. Does not allow different Directus versions and database vendors by default. You can opt in to bypass these checks by passing the `force` query parameter. Accepts partial snapshots, in which case the diff is scoped to the collections the snapshot references.

Query Parameters

force
boolean

Bypass version and database vendor restrictions.

mode
string

mirror (default) returns all operations. merge returns an additive diff that excludes deletions.

Request Body

version
integer

1 for a full snapshot, 2 for a partial snapshot.

directus
string
vendor
string
collections
array
fields
array
systemFields
array

Indexes defined on system fields.

relations
array

Responses

Successful request
hash
string
diff
object
::: details **Toggle Open to See Request**

import { createDirectus, rest, schemaDiff } from '@directus/sdk';

const client = createDirectus('directus_project_url').with(rest());

const result = await client.request(
	schemaDiff({
		version: version_num,
		directus: 'directus_version',
		vendor: 'database_type',
		collections: [
			{
				collection: 'collection_name',
				meta: {
					// Meta fields
				},
				schema: {
					// Schema fields
				},
			},
		],
		fields: [
			{
				collection: 'collection_name',
				field: 'field_1_id',
				type: 'field_1_type',
				meta: {
					// Meta fields
				},
				schema: {
					// Meta fields
				},
			},
			{
				collection: 'collection_name',
				field: 'field_2_id',
				type: 'field_2_type',
				meta: {
					// Meta fields
				},
				schema: {
					// Meta fields
				},
			},
			{
				collection: 'collection_name',
				field: 'field_3_id',
				type: 'field_3_type',
				meta: {
					// Meta fields
				},
				schema: {
					// Meta fields
				},
			},
		],
		relations: [],
	}, {
		mode: 'merge',
	})
);

console.dir(result, { depth: null });

:::
Response Example
{
  "diff": {
    "collections": [
      {
        "diff": [
          {}
        ]
      }
    ],
    "fields": [
      {
        "diff": [
          {}
        ]
      }
    ],
    "systemFields": [
      {
        "diff": [
          {}
        ]
      }
    ],
    "relations": [
      {
        "diff": [
          {}
        ]
      }
    ]
  }
}

Retrieve Schema Snapshot

Retrieve the current schema. This endpoint is only available to admin users. Returns a full snapshot (version `1`) by default. Pass `includeCollections` or `excludeCollections` to scope it to a subset of collections, which returns a partial snapshot (version `2`).

Query Parameters

export
string

Saves the API response to a file. Accepts one of csv, csv_utf8, json, xml, yaml.

includeCollections
array

Restrict the snapshot to these collections. Unknown names are ignored. Mutually exclusive with excludeCollections.

excludeCollections
array

Snapshot every collection except these. Mutually exclusive with includeCollections.

Responses

Successful request
version
integer

1 for a full snapshot, 2 for a partial snapshot.

directus
string
vendor
string
collections
array
fields
array
systemFields
array

Indexes defined on system fields.

relations
array
import { createDirectus, rest, schemaSnapshot } from '@directus/sdk';

const client = createDirectus('directus_project_url').with(rest());

const result = await client.request(schemaSnapshot());

// Partial snapshot
const partial = await client.request(
  schemaSnapshot({ includeCollections: ['articles', 'authors'] })
);
Response Example
{
  "version": 1,
  "collections": [
    {
      "collection": "customers",
      "translations": [],
      "item_duplication_fields": []
    }
  ],
  "fields": [
    {
      "collection": "about_us",
      "field": "id",
      "special": [],
      "options": {},
      "translations": []
    }
  ],
  "systemFields": [
    {
      "schema": {}
    }
  ],
  "relations": [
    {
      "id": 1,
      "many_collection": "directus_activity",
      "many_field": "user",
      "one_collection": "directus_users",
      "one_field": null,
      "one_allowed_collections": [],
      "junction_field": null
    }
  ]
}