---
title: "Contentstack .NET Model Generator"
description: "Generate models from Contentstack content types via .NET CLI. Supports OAuth 2.0 and API key authentication for fast, secure, automated model creation."
url: "https://www.contentstack.com/docs/developers/sdks/content-delivery-sdk/dot-net/contentstack-dot-net-model-generator"
product: "Contentstack"
doc_type: "guide"
audience:
  - developers
  - admins
version: "current"
last_updated: "2026-07-22"
---

# Contentstack .NET Model Generator

Use the Contentstack .NET Model Generator to create C# model classes from content types in your [stack](/docs/headless-cms/about-stack).

## Requirements

**As of v1.0.0**, the Contentstack Model Generator targets **.NET 10.0** and requires the **.NET 10.0 SDK or later** to be installed before executing the commands. Versions prior to v1.0.0 targeted .NET 7.0.

## Installation

To install Contentstack model generator, run following command:

```
dotnet tool install —-global contentstack.model.generator
```

## Usage

Once you install contentstack.model.generator run the following command to view available commands:

```
Contentstack.model.generator --help
```

## Authentication Methods

The Contentstack Model Generator supports two authentication methods:

*   Authentication using Authtoken (default)
*   Authentication using OAuth 2.0

## Authentication using Authtoken

### Authentication Options

**Short key**

**Long Key**

**Description**

\-a

\--api-key

The stack API key for the [Content Management API](/docs/developers/apis/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.

### Model Generation Options

**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. Default value: MB.

\-g

\--group-prefix

Adds a **custom prefix** to class names generated for **Group** fields. Prevents name collisions when multiple groups share identical names. Default value: Group.

\-p

\--path

Specifies the **output directory** where model files should be created. Defaults to the **current working directory** if not specified.

### Examples for Traditional API Key Authentication

**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/Desktop
```

**Example 3: With Namespace**

To create classes with namespace run the following command:

```
contentstack.model.generator -a <stack_api_key> -A <authtoken> -n YourProject.Models
```

**Example 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> -N
```

**Note:** 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.

## Authentication Using OAuth 2.0

### OAuth 2.0 Commands

**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.

### OAuth 2.0 Setup

#### Prerequisites

*   [Contentstack account](/docs/headless-cms/set-up-your-account) with OAuth enabled.
*   [Create an OAuth application](https://www.contentstack.com/docs/developer-hub/contentstack-oauth) in your Contentstack dashboard

#### OAuth Flow

1.  **Authorization**: The tool displays the Contentstack OAuth authorization URL for you to open manually
2.  **Authentication**: Open the URL in your browser, log in to your Contentstack account and authorize the application
3.  **Callback**: You’ll be redirected to your specified redirect URI with an authorization code
4.  **Code Entry**: Copy the authorization code from the redirect URL and paste it into the tool
5.  **Token Exchange**: The tool automatically exchanges the code for an access token
6.  **Model Generation**: The tool fetches your content types and generates models
7.  **Logout**: The tool automatically logs out and clears tokens

**Note:** Auto-logout after model generation cannot be disabled. Tokens are session-specific and not reused by other SDKs or applications.

### Examples for OAuth 2.0 Authentication

**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.Models
```

### OAuth Command Example

Here’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


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=...&code_challenge_method=S256

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: [paste the code here]

Exchanging authorization code for access token...
OAuth authentication successful!
Access token expires at: 2026-01-15 14:30:00 UTC
Fetching stack details for the provided API key.
No path specified. Generating files in the current working directory: /Users/you/project.
Fetching content types from My Contentstack Stack stack.
Found 5 content types.
Total content types fetched: 5.
Fetching global fields from stack: My Contentstack Stack.
Found 2 global fields.
Total global fields fetched: 2.
Generating files from content types.
Files created successfully.
Opening output directory: /Users/you/project.

Logging out from OAuth...
OAuth logout successful!
```

## Generated Code

**As of v1.0.0**, generated model files use **System.Text.Json** instead of Newtonsoft.Json:

*   Properties are decorated with \[JsonPropertyName("...")\] instead of \[JsonProperty(propertyName: "...")\].
*   Generated files reference System.Text.Json, System.Text.Json.Nodes, and System.Text.Json.Serialization instead of Newtonsoft.Json.
*   Generated converters (for embedded objects and modular blocks) inherit System.Text.Json.Serialization.JsonConverter<T> and implement Read/Write instead of Newtonsoft's JsonConverter<T> with ReadJson/WriteJson.

A generated model file looks like this:

```
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Contentstack.Core.Models;
using Contentstack.Utils.Interfaces;

namespace ContentstackModels.Models
{
    public partial class BlogPost : IEmbeddedObject
    {
        public const string ContentType = "blog_post";
        [JsonPropertyName("uid")]
        public string Uid { get; set; }
        [JsonPropertyName("_content_type_uid")]
        public string ContentTypeUid { get; set; }
        [JsonPropertyName("title")]
        public string Title { get; set; }
    }
}
```

**Breaking change:** if you regenerate models for an existing project using v1.0.0 or later, the attributes and converter base classes in the output will change from the Newtonsoft-based shape to the System.Text.Json-based shape shown above. Update your consuming code and Contentstack .NET SDK versions accordingly before regenerating.

## Security and Troubleshooting

### Security Features

*   **PKCE Support**: Uses Proof Key for Code Exchange for enhanced security
*   **Client Secret Optional**: Supports both confidential and public clients
*   **Automatic Token Management**: Handles token refresh and expiration
*   **Secure Logout**: Automatically clears tokens after model generation

### Troubleshooting OAuth

*   **Invalid Redirect URI**: Ensure the redirect URI matches exactly what’s configured in your OAuth app
*   **Client ID/Secret Issues**: Verify your OAuth app credentials
*   **Network Issues**: Check your internet connection and Contentstack service status
*   **Permission Issues**: Ensure your account has the necessary permissions for the stack  
    

## MIT License

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.