Use the Contentstack .NET Model Generator to create C# model classes from content types in your stack.
Note: Ensure .NET version 7.0 or later is installed before executing the commands.
To install Contentstack model generator, run following command:
dotnet tool install —-global contentstack.model.generatorOnce you install contentstack.model.generator run the following command to view available commands:
Contentstack.model.generator --helpThe Contentstack Model Generator supports two authentication methods:
| Short key | Long Key | Description |
| -a | --api-key | The stack API key for the Content Management API. This key is required for both traditional (Authtoken-based) and OAuth 2.0 authentication methods. |
| -A | --authtoken | Specifies the authtoken used for traditional API key authentication with the Content Management API. |
| -b | --branch | Specifies a branch of the stack to fetch content types and generate models. |
| -e | --endpoint | The Contentstack host for the Content Management API. The default value used is api.contentstack.io. |
Note: OAuth 2.0–related flags (such as --oauth, --client-id, --client-secret, etc.) are listed separately in the OAuth 2.0 options table.
| Short key | Long Key | Description |
| -n | --namespace | Specifies the C# namespace to use for generated model classes. Example: MyProject.Models Default value: ContentstackModels. |
| -N | --is-nullable | Enables nullable reference types to help prevent System.NullReferenceException errors. Disable if it disrupts your workflow. |
| -f | --force | Overwrites existing model files without confirmation prompts. |
| -m | --modular-block-prefix | Adds a custom prefix to class names generated for Modular Block fields. Helps differentiate similar block names across content types. |
| -g | --group-prefix | Adds a custom prefix to class names generated for Group fields. Prevents name collisions when multiple groups share identical names. |
| -p | --path | Specifies the output directory where model files should be created. Defaults to the current working directory if not specified. |
Example 1: Basic Usage
To create classes in current directory run the following command:
contentstack.model.generator -a <stack_api_key> -A <authtoken>Example 2: Specific Path
To create classes in specific path run the following command:
contentstack.model.generator -a <stack_api_key> -A <authtoken> -p /User/xxx/DesktopExample 3: With Namespace
To create classes with namespace run the following command:
contentstack.model.generator -a <stack_api_key> -A <authtoken> -n YourProject.ModelsExample 4: With Nullable Annotations
To allow nullable annotation context in model creation run the following command:
contentstack.model.generator -a <stack_api_key> -A <authtoken> -NNote: Running multiple commands in succession overwrites the previously generated files if they share the same output path. Use unique directories or enable the --force flag when you intend to replace existing files.
| Long Key | Description |
| --oauth | Enables OAuth 2.0 authentication for the tool. This option cannot be used together with traditional (Authtoken–based) authentication. |
| --client-id | Specifies the OAuth Client ID used for authentication. Use the default ID unless you're using a custom OAuth application. Default value: Ie0FEfTzlfAHL4xM |
| --client-secret | Specifies the OAuth Client Secret. Optional for public clients using PKCE |
| --redirect-uri | Specifies the Redirect URI. Default value: http://localhost:8184 |
| --app-id | Specifies the default OAuth Application ID for authentication. Provide a custom App ID if using a different OAuth application. Default value: 6400aa06db64de001a31c8a9 |
| --scopes | Defines one or more OAuth scopes for the authentication request. Scopes determine the level of access granted. Use space-separated values when specifying multiple scopes. |
Note: Auto-logout after model generation cannot be disabled. Tokens are session-specific and not reused by other SDKs or applications.
Example 1: OAuth with PKCE (Recommended)
For public clients or enhanced security, use OAuth with PKCE:
contentstack.model.generator --oauth -a <stack_api_key> --client-id <client_id> --redirect-uri http://localhost:8184
Example 2: OAuth with Client Secret
For confidential clients with client secret:
contentstack.model.generator --oauth -a <stack_api_key> --client-id <client_id> --client-secret <client_secret> --redirect-uri http://localhost:8184
Example 3: OAuth with App ID
For OAuth with specific app and scopes:
contentstack.model.generator --oauth -a <stack_api_key> --client-id <client_id> --redirect-uri http://localhost:8184 --app-id <app_id>
Example 4: OAuth with Custom Path and Namespace
contentstack.model.generator --oauth -a <stack_api_key> --client-id <client_id> --redirect-uri http://localhost:8184 -p /path/to/models -n YourProject.ModelsHere’s what you will see when running an OAuth command:
$ contentstack.model.generator --oauth -a <api_key> --client-id myclient123 --redirect-uri http://localhost:8184
Contentstack Model Generator v0.4.6
=====================================
OAuth Authentication Required
=============================
Please open the following URL in your browser to authorize the application:
https://app.contentstack.com/#!/apps/6400aa06db64de001a31c8a9/authorize?response_type=code&client_id=myclient123&redirect_uri=http%3A%2F%2Flocalhost%3A8184&code_challenge=...
After authorization, you will be redirected to a local URL.
Please copy the 'code' parameter from the redirect URL and paste it here:
Authorization code: [User pastes the code here]
Exchanging authorization code for access token...
OAuth authentication successful!
Access token expires at: 2024-01-15 14:30:00 UTC
Fetching stack information...
Stack: My Contentstack Stack
API Key: api_key
Fetching content types...
Found 5 content types:
Generating files from content type
Files successfully created!
Opening <file_path>/Models
Logging out from OAuth...
OAuth logout successful!
Copyright (c) 2012-2025 Contentstack
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.