Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@kontent-ai/model-generator

kontent-ai7.8kMIT8.1.1TypeScript support: included

This utility generates strongly-typed models for Delivery JS SDK, Migration toolkit or just general scripting to improve the experience when referencing Kontent.ai related objects.

Kontent, Kontent.ai, Kontent.ai typings, Kontent.ai models, Kontent Model Generator, Kontent.ai strongly typed models

readme

npm version Build Test GitHub license

Kontent.ai Model Generator

The Kontent.ai Model Generator is a developer tool that streamlines working with Kontent.ai by generating strongly typed objects and TypeScript models. It supports the generation of four distinct model types, each tailored to specific use cases:

Model type Description Compatibility
delivery-sdk Generates TypeScript models for the JS Delivery SDK. These models include content types, taxonomies, and codename-based types representing elements such as workflow steps, languages, and more. @kontent-ai/delivery-sdk version 16.0.0 or higher
migration-toolkit Creates TypeScript models for the Migration Toolkit. These models help simplify and standardize the process of writing migration scripts. @kontent-ai/migration-toolkit version 2.6.0 or higher
environment Generates JavaScript objects (not TypeScript types) representing the entire structure of your environment — including content types, workflows, languages, and taxonomies. These objects provide comprehensive access to environment metadata. Can be used in any project. No external dependencies are required.
items Produces TypeScript types for all item codenames, along with objects containing the id and codename of each item. This is particularly useful when referencing a set of items in your code, enabling type-safe access instead of relying on hardcoded strings. Can be used in any project. No external dependencies are required.

Installation

You can install the generator globally, add it as a development dependency, or simply run it using npx for convenience.

# Install globally
npm i @kontent-ai/model-generator@latest -g

# Install as dev dependency and use in your code
npm i @kontent-ai/model-generator@latest --save-dev 

# Run with npx
npx @kontent-ai/model-generator@latest --help

CLI Help

# General help
npx @kontent-ai/model-generator@latest  --help

# Or get help for specific command
npx @kontent-ai/model-generator@latest delivery-sdk --help

Delivery SDK Models

[!TIP] Recommended: Using these models is highly encouraged when working with the JavaScript Delivery SDK, as they provide robust type safety and streamline development.

Basic usage

npx @kontent-ai/model-generator@latest delivery-sdk
    --environmentId=<id>
    --managementApiKey=<key>

Usage with options

npx @kontent-ai/model-generator@latest delivery-sdk
    --environmentId=<id>
    --managementApiKey=<key>
    --outputDir=<path>
    --moduleFileExtension=<js | ts | none | mts | mjs>
    --addTimestamp=<true, false>
    --managementBaseUrl=<proxyUrl>
import { generateDeliveryModelsAsync } from '@kontent-ai/model-generator';

await generateDeliveryModelsAsync({
    // required
    environmentId: 'x',
    managementApiKey: 'y',
    moduleFileExtension: 'js',
    addTimestamp: false,
    createFiles: true,
    outputDir: '/', // only required when createFiles is true

    // optional
    fileResolvers: { contentType: 'camelCase', snippet: 'camelCase', taxonomy: 'camelCase' },
    nameResolvers: { contentType: (item) => `Company_${item.codename}`, snippet: 'pascalCase', taxonomy: 'pascalCase' },
    formatOptions: { indentSize: 4, quote: 'single' },
    baseUrl: undefined
});

Configuration

Option Description
environmentId Id of Kontent.ai environment
managementApiKey Management API key
moduleFileExtension Extension used for imports in generated models.
addTimestamp Indicates if models contain timestamp
createFiles If enabled, files will be created on FileSystem. When disabled you may iterate over the result and process the files yourself.
outputDir Output directory path for files. Only available when createFiles is set to true
fileResolvers Can be used to customize the generated filenames
nameResolvers Can be used to customize names of generated types
formatOptions Prettier configuration for formatting generated code
baseUrl Can be used to override default Kontent.ai URLs

Migration toolkit models

Basic usage

npx @kontent-ai/model-generator@latest migration-toolkit
    --environmentId=<id>
    --managementApiKey=<key>

Usage with options

npx @kontent-ai/model-generator@latest migration-toolkit
    --environmentId=<id>
    --managementApiKey=<key>
    --outputDir=<path>
    --moduleFileExtension=<js | ts | none | mts | mjs>
    --addTimestamp=<true, false>
    --managementBaseUrl=<proxyUrl>
import { generateMigrationModelsAsync } from '@kontent-ai/model-generator';

await generateMigrationModelsAsync({
    // required
    environmentId: 'x',
    managementApiKey: 'y',
    moduleFileExtension: 'js',
    addTimestamp: false,
    createFiles: true,
    outputDir: '/', // only required when createFiles is true

    // optional
    baseUrl: undefined,
    formatOptions: { indentSize: 4, quote: 'single' }
});

Configuration

Option Description
environmentId Id of Kontent.ai environment
managementApiKey Management API key
moduleFileExtension Extension used for imports in generated models.
addTimestamp Indicates if models contain timestamp
createFiles If enabled, files will be created on FileSystem. When disabled you may iterate over the result and process the files yourself.
outputDir Output directory path for files. Only available when createFiles is set to true
formatOptions Prettier configuration for formatting generated code
baseUrl Can be used to override default Kontent.ai URLs

Environment models

[!TIP] Due to their potentially large size, these objects are intended for use in backend/server-side code only. Avoid including them in client-side applications to prevent unnecessary bundle size and exposure of sensitive data.

Basic usage

npx @kontent-ai/model-generator@latest environment
    --environmentId=<id>
    --managementApiKey=<key>

Usage with options

npx @kontent-ai/model-generator@latest environment
    --environmentId=<id>
    --managementApiKey=<key>
    --entities=<contentTypes,taxonomies,languages>
    --outputDir=<path>
    --moduleFileExtension=<js | ts | none | mts | mjs>
    --addTimestamp=<true, false>
    --managementBaseUrl=<proxyUrl>

Available entities

[
    'languages',
    'taxonomies',
    'contentTypes',
    'snippets',
    'webhooks',
    'collections',
    'workflows',
    'assetFolders',
    'roles',
    'customApps',
    'previewUrls',
    'spaces'
];
import { generateEnvironmentModelsAsync } from '@kontent-ai/model-generator';

await generateEnvironmentModelsAsync({
    // required
    environmentId: 'x',
    managementApiKey: 'y',
    entities: [], // all entity types are exported by default
    addTimestamp: false,
    moduleFileExtension: 'js',
    createFiles: true,
    outputDir: '/', // only required when createFiles is true
    // optional
    baseUrl: undefined,
    formatOptions: { indentSize: 4, quote: 'single' }
});

Configuration

Option Description
environmentId Id of Kontent.ai environment
managementApiKey Management API key
entities Array of entity types that will be exported
moduleFileExtension Extension used for imports in generated models.
addTimestamp Indicates if models contain timestamp
createFiles If enabled, files will be created on FileSystem. When disabled you may iterate over the result and process the files yourself.
outputDir Output directory path for files. Only available when createFiles is set to true
formatOptions Prettier configuration for formatting generated code
baseUrl Can be used to override default Kontent.ai URLs

Item models

[!TIP] This option is not recommended for environments with a large volume of content items, as it may lead to performance or scalability issues during code generation.

Basic usage

# 'deliveryApiKey' option is required for 'preview' or 'secure' api modes
# 'contentTypes' option is CSV of content type codenames and can be used to narrow down generated items
npx @kontent-ai/model-generator@latest items
    --environmentId=<id>
    --managementApiKey=<key>

Usage with options

npx @kontent-ai/model-generator@latest items
    --environmentId=<id>
    --managementApiKey=<key>
     -deliveryApiKey=<key>
    --apiMode=<default, preview, secure>
    --generateTypes=<true, false>
    --generateObjects=<true, false>
    --outputDir=<path>
    --moduleFileExtension=<js | ts | none | mts | mjs>
    --addTimestamp=<true, false>
    --filterByTypeCodenames=<codenameA,codenameB>
    --managementBaseUrl=<proxyUrl>
    --deliveryBaseUrl=<proxyUrl>
import { generateItemsAsync } from '@kontent-ai/model-generator';

await generateItemsAsync({
    // required
    environmentId: 'x',
    managementApiKey: 'y',
    deliveryApiKey: 'z', // only required when secure / api mode is used
    addTimestamp: false,
    moduleFileExtension: 'js',
    apiMode: 'default',
    filterByTypeCodenames: [],
    generateObjects: true,
    generateTypes: true,
    createFiles: true,
    outputDir: '/', // only required when createFiles is true
    // optional
    baseUrl: undefined,
    formatOptions: { indentSize: 4, quote: 'single' },
    deliveryBaseUrl: undefined
});

Configuration

Option Description
environmentId Id of Kontent.ai environment
managementApiKey Management API key
deliveryApiKey Delivery API key required when the apiMode is using preview or secure mode
moduleFileExtension Extension used for imports in generated models.
addTimestamp Indicates if models contain timestamp
generateObjects If enabled, javascript objects with codename / id will be generated
generateTypes If enabled, typescript type representing codename will be generated
filterByTypeCodenames Array of content type codenames of which content items will be generated. Useful for narrowing down generated items
apiMode Delivery API mode for fetching content items. By default delivery (public) mode is used
createFiles If enabled, files will be created on FileSystem. When disabled you may iterate over the result and process the files yourself.
outputDir Output directory path for files. Only available when createFiles is set to true
formatOptions Prettier configuration for formatting generated code
baseUrl Can be used to override default Kontent.ai URLs

Sample models

To see how models are generated have a look at following sample generated models:

  1. delivery-sdk -> https://github.com/kontent-ai/model-generator-js/tree/master/sample/delivery
  2. migration-toolkit -> https://github.com/kontent-ai/model-generator-js/tree/master/sample/migration
  3. environment -> https://github.com/kontent-ai/model-generator-js/tree/master/sample/environment
  4. items -> https://github.com/kontent-ai/model-generator-js/tree/master/sample/items

Contribution & Feedback

Contributions are welcomed. Simply make a pull request.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

8.1.1 (2025-04-22)

Bug Fixes

  • Correctly imports CoreContentType in snippet when all types are allowed in RTE, linkes items or subpages element (96f33c7)

8.1.0 (2025-04-08)

Features

Bug Fixes

  • Skips invalid reference to taxonomies. Usually caused by deleting a taxonomy and not updating it's references in content types / snippets (598bad3)

8.0.0-0 (2024-09-16)

Features

  • Adds support for items models - Produces TypeScript types for all item codenames, along with objects containing the id and codename of each item. This is particularly useful when referencing a set of items in your code, enabling type-safe access instead of relying on hardcoded strings.
  • Add support for migration-toolkit models
  • Revamped the structure of delivery-sdk models - the models offer better type safety with the introduction of types representing various environment entities such as languages, content types, workflows, workflow step and more. The required version of @kontent-ai/delivery-sdk is >=16.
  • Separates snippet from the main item in delivery-sdk models
  • Extends environment models to cover all entities available in MAPI
  • Covers the model generator with unit and integration tests
  • Simplifies public API by only exporting meaningful code
  • Updates all dependencies to their latest versions

⚠ BREAKING CHANGES

  • Separates delivery-sdk & environment models which can now be generated independently on each other
  • Both CLI & API is updated, please refer to examples in the Readme.md or in sample directory for more details
  • The delivery-sdk models now require you to use @kontent-ai/delivery-sdk on version >=16. There have been some changes to SDK which are incorporated in the model generator.

Features

  • Separates Delivery & Project models (a28ec19)

7.4.0 (2024-08-15)

Features

  • updates deps (& fixes Axios vulnerability) (a6999aa)
  • updates deps & node.js req to >= 20 (160fd56)

7.3.0 (2024-06-28)

Features

7.2.0 (2024-06-18)

Features

  • adds support for 'nodeNext' module resolution imports (bd222e4)
  • updates deps (300131f)

7.1.0 (2024-04-29)

Features

Bug Fixes

  • replaces all apostrophes in name values (636d772)

7.0.3 (2024-04-15)

Bug Fixes

  • Adds missing declaration files (26e61ab)

7.0.2 (2024-04-13)

Bug Fixes

  • Fixes runtime by avoiding direct package.json imports, instead use version script with metadata file (f212f71)

7.0.1 (2024-04-13)

Bug Fixes

  • Fixes export / types path (558fec7)

7.0.0 (2024-04-13)

⚠ BREAKING CHANGES

  • Removes CommonJs export in favor of ES2022. Also reworks scripts / package.json and tsconfig

Features

  • Handle errors in file formatting with prettier, display better info message and store unformatted file (c2e1265)
  • Removes CommonJs export in favor of ES2022. Also reworks scripts / package.json and tsconfig (29dfa39)

Bug Fixes

  • Removes unsupported characters in object names (e606f39)

6.5.1 (2024-02-27)

6.5.0 (2023-09-12)

Features

6.4.0 (2023-08-14)

Features

6.3.1 (2023-08-04)

Bug Fixes

6.3.0 (2023-08-02)

Features

  • adds option to disable sorting on taxonomy terms (6707d4b)

6.2.0 (2023-07-31)

Features

  • updates deps, improves test generator script, migrates to eslint, refactors file creation (internal) (fe15ddc)

6.1.0 (2023-06-26)

Features

  • generates strongly typed multiple choice options, convert object names to safe variants in comments, updates dependencies, updates default code formatting (7119a94)

6.0.0 (2023-05-31)

⚠ BREAKING CHANGES

  • Uses 'environment' naming as used with latest version of SDKs, updates all dependencies

Features

  • Uses 'environment' naming as used with latest version of SDKs, updates all dependencies (131b798)

5.10.0 (2023-02-20)

Features

  • improves error logging for failed content types & snippets (786e076)
  • updates dependencies (3926a5b)
  • updates deps (da5d171)

Bug Fixes

  • escapes asset folder name value (2b0708e)

5.9.0 (2022-12-20)

Features

5.8.0 (2022-10-07)

Features

5.7.0 (2022-09-15)

Features

  • adds support for generating content type snippet project model (generates contentTypeSnippets.ts) (259d969)

5.6.0 (2022-09-13)

Features

5.5.1 (2022-09-02)

Bug Fixes

  • Fixes import path from content type snippet to content type model (4707a05)

5.5.0 (2022-09-01)

Features

5.4.1 (2022-08-02)

Bug Fixes

5.4.0 (2022-08-01)

Features

5.3.0 (2022-07-28)

Features

Bug Fixes

  • Adds default import to barrel files in case there are no files to import (2020022)

5.2.0 (2022-07-26)

Features

  • Adds new 'isEnterpriseSubscription' option which disables endpoint that required Enterprise subscription plans (76e9784)

5.1.1 (2022-07-21)

Bug Fixes

  • Adds missing 'taxonomy' element to project's content type structure (8a165a3)
  • fixes missing usage of 'addExtension' (b99dcd7)

5.1.0 (2022-07-21)

Features

  • updates dependencies & readme, makes 'exportProjectSettings' property optional and exports all objects by default (e1c50fa)

5.0.0 (2022-07-14)

5.0.0-4 (2022-07-14)

5.0.0-3 (2022-05-30)

Features

5.0.0-2 (2022-05-24)

Features

5.0.0-1 (2022-05-19)

⚠ BREAKING CHANGES

  • Refactors generator to use Management API for fetching source data. This change now requires you to use 'apiKey' option when calling the generator.
  • changes the default generator file names, renames fileResolver to contentTypeFileResolver, adds support for generating taxonomy types

Features

  • Adds 'help' cli command with options list and descriptions (f67916b)
  • adds 'id' to all objects within project structure (7123509)
  • adds additional language properties (fallback language, default & active states) (5659c65)
  • Adds comments to all generated _project items (fba932b)
  • Adds comments to each element / content type / taxonomy with extra metadata information (1b6a6f1)
  • Adds metadata to generated project model, logs project information including environment to console (ae9d4de)
  • Adds more properties to selected project objects and adjust comments to prevent data duplication (23ffc62)
  • adds name property to asset folders (c559f17)
  • Adds support for asset folders in project structure (e73fd46)
  • adds support for collections in project model (8404230)
  • adds support for disabled export of certain project objects, updates readme and samples (b376564)
  • adds support for project roles, use camelCase for language property identifier (022f7c7)
  • adds support for project webhooks (4c2a99f)
  • Adds support for strongly typed taxonomy elements (cab91bf)
  • changes the default generator file names, renames fileResolver to contentTypeFileResolver, adds support for generating taxonomy types (011ffa8)
  • Generates barrel export from all created files (915d149)
  • Refactors generator to use Management API for fetching source data. This change now requires you to use 'apiKey' option when calling the generator. (47270b1)
  • Refactors internal generator and adds support for content type restrictions in linked items element (d6afc18)
  • Separates content type / taxonomy barrel export (d346533)
  • separates project model into distinct files & properties, uses barrel export for project files (06067c0)
  • updates deps (abaa4f6)
  • updates deps & entry modules (a9582b8)

5.0.0-0 (2022-05-09)

⚠ BREAKING CHANGES

  • Refactors generator to use Management API for fetching source data. This change now requires you to use 'apiKey' option when calling the generator.
  • changes the default generator file names, renames fileResolver to contentTypeFileResolver, adds support for generating taxonomy types

Features

  • Adds 'help' cli command with options list and descriptions (f67916b)
  • adds 'id' to all objects within project structure (7123509)
  • Adds comments to all generated _project items (fba932b)
  • Adds comments to each element / content type / taxonomy with extra metadata information (1b6a6f1)
  • Adds metadata to generated project model, logs project information including environment to console (ae9d4de)
  • Adds more properties to selected project objects and adjust comments to prevent data duplication (23ffc62)
  • Adds support for asset folders in project structure (e73fd46)
  • adds support for collections in project model (8404230)
  • adds support for disabled export of certain project objects, updates readme and samples (b376564)
  • adds support for project roles, use camelCase for language property identifier (022f7c7)
  • adds support for project webhooks (4c2a99f)
  • Adds support for strongly typed taxonomy elements (cab91bf)
  • Generates barrel export from all created files (915d149)
  • changes the default generator file names, renames fileResolver to contentTypeFileResolver, adds support for generating taxonomy types (011ffa8)
  • Refactors generator to use Management API for fetching source data. This change now requires you to use 'apiKey' option when calling the generator. (47270b1)
  • Refactors internal generator and adds support for content type restrictions in linked items element (d6afc18)
  • Separates content type / taxonomy barrel export (d346533)
  • separates project model into distinct files & properties, uses barrel export for project files (06067c0)
  • updates deps (abaa4f6)
  • updates deps & entry modules (a9582b8)

4.1.0 (2021-11-24)

4.1.0-0 (2021-11-24)

Features

  • adds support for customizing content type names (d63e007)
  • adds support for generating custom filenames (d4e638b)
  • renames 'nameResolver' to 'elementResolver' and unifies it with customElementResolver option (a18fd00)

4.0.0 (2021-11-16)

4.0.0-7 (2021-11-16)

Bug Fixes

  • converts language property name to alphanumeric string (68272a2)

4.0.0-6 (2021-11-16)

Bug Fixes

  • use camel case codename for language property name (in case there are unsupported object property chars in codename) (fc9d8e0)

4.0.0-5 (2021-11-10)

Bug Fixes

4.0.0-4 (2021-11-10)

Bug Fixes

  • move kontent-delivery dependency (9747799)

4.0.0-3 (2021-11-08)

Features

  • refactors generator code to prepare for separate delivery / management models (3127473)

4.0.0-2 (2021-11-04)

4.0.0-1 (2021-11-04)

Features

  • adds support for generating project structure for content types, languages & taxonomies (7dfdab9)
  • use pascal case for type class names (322888e)

4.0.0-0 (2021-10-20)

Features

  • use peer deps for kontent delivery, updates all deps (9b02551)

3.3.0-0 (2021-10-11)

Features

  • adds support for custom name resolver, fixes script paths, exports public API (af50d20)
  • adds support for specifying built-in name resolvers (22881db)
  • capitalize type name in model (76106c7)
  • implements timestamp disable option, adds colors for better console output, adds prettier and beautifies output with the ability to pass custom configuration (df13368)
  • updates build & distribution process, updates dev deps (e6dfb8d)
  • updates code generator for new version of Delivery SDK (f6aca8f)

3.2.0 (2020-02-12)

Features

3.1.0 (2020-01-06)

Features

  • updates dependencies and uses @kentico/kontent-delivery:8.2.0 (befe7f0)
  • uses standard-version & fixes github link in package.json (dc15768)