Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. All you have to do is build your application frontend, and Contentstack will take care of the rest. Read More.
This SDK uses the Content Management API (CMA). The CMA is used to manage the content of your Contentstack account. This includes creating, updating, deleting, and fetching content of your account. To use the CMA, you will need to authenticate your users with a Management Token or an Authtoken. Read more about it in Authentication.
Note: By using CMA, you can execute GET requests for fetching content. However, we strongly recommend that you always use the Content Delivery API to deliver content to your web or mobile properties.
You need .NET 10 or above installed on your machine to use the Contentstack .NET CMA SDK.
Migrating from v0.x? Version 1.0.0 replaces Newtonsoft.Json with System.Text.Json and changes several public API signatures (e.g.
OpenJObjectResponse()→OpenJsonObjectResponse(),SerializerSettings→SerializerOptions). See the migration guide before upgrading.
Open the terminal and install the contentstack module via ‘Package Manager’ command
PM> Install-Package Contentstack.Management.Core
And via ‘.Net CLI’
dotnet add package Contentstack.Management.Core
To use the module in your application, you need to first Add Namespace to your class
usingContentstack.Management.Core;// ContentstackClient, ContentstackClientOptions, ContentstackResponse usingContentstack.Management.Core.Models;// Stack, ContentType, Entry, AssetTo use this SDK, you need to authenticate your users by using the Authtoken, credentials, or Management Token (stack-level token).
An Authtoken is a read-write token used to make authorized CMA requests, and it is a user-specific token.
varoptions=newContentstackOptions(){Host="<API_HOST>",Authtoken="<AUTHTOKEN>"}ContentstackClientclient=newContentstackClient(newOptionsWrapper<ContentstackClientOptions>(options));To Login to Contentstack by using credentials, you can use the following lines of code:
ContentstackClientclient=newContentstackClient();NetworkCredentialcredentials=newNetworkCredential("<EMAIL>","<PASSWORD>");// Sync callContentstackResponsecontentstackResponse=client.Login(credentials);// Async callclient.LoginAsync(credentials).ContinueWith((t)=>{if(t.IsCompleted&&t.Status!=System.Threading.Tasks.TaskStatus.Faulted){// success}});Management Tokens are stack-level tokens, with no users attached to them.
Stackstack=client.Stack("<API_KEY>","<MANAGEMENT_TOKEN>");To use the .Net CMA SDK, you need to first initialize it. To do this, use the following code:
usingContentstack.Management.Core;varoptions=newContentstackOptions(){Host="<API_HOST>",Authtoken="<AUTHTOKEN>"}ContentstackClientclient=newContentstackClient(newOptionsWrapper<ContentstackClientOptions>(options));If you want to initialize the SDK in a particular branch, use the following code:
usingContentstack.Management.CoreContentstackClient client =newContentstackClient();client.Stack("API_KEY","MANAGEMENT_TOKEN","BRANCH");Contentstack allows you to define HTTP proxy for your requests with the .NET Management SDK. A proxied request allows you to anonymously access public URLs even from within a corporate firewall through a proxy server.
Here is the basic syntax of the proxy settings that you can pass within fetchOptions of the .NET Management SDK:
usingSystem.Net;varcontentstackConfig=newContentstackClientOptions();contentstackConfig.ProxyHost="http://127.0.0.1";contentstackConfig.ProxyPort=9000;contentstackConfig.ProxyCredentials=newNetworkCredential(userName:"username",password:"password");ContentstackClientclient=newContentstackClient(newOptionsWrapper<ContentstackClientOptions>(options));As an alternative to Authtoken/Management Token authentication, you can use OAuth 2.0:
ContentstackClientclient=newContentstackClient();varoauthOptions=newOAuthOptions{AppId="your-app-id",ClientId="your-client-id",RedirectUri="http://localhost:8184"};OAuthHandleroauthHandler=client.OAuth(oauthOptions);// Get authorization URLstringauthUrl=oauthHandler.GetAuthorizationUrl();// After user authorization, exchange code for tokensvartokens=awaitoauthHandler.ExchangeCodeForTokenAsync("authorization_code");Note: Once authenticated, the SDK automatically refreshes expired OAuth tokens before making API calls — no manual token refresh required.
Use the Branch model to create, fetch, delete, and query branches for a stack:
ContentstackClientclient=newContentstackClient("<AUTHTOKEN>","<API_HOST>");// Create a branchBranchModelmodel=newBranchModel(){Uid="my-branch",Source="main"};ContentstackResponsecreateResponse=client.Stack("<API_KEY>").Branch().Create(model);// Fetch a branchContentstackResponsefetchResponse=client.Stack("<API_KEY>").Branch("my-branch").Fetch();// Query all branchesContentstackResponsequeryResponse=client.Stack("<API_KEY>").Branch().Query().Find();// Delete a branchContentstackResponsedeleteResponse=client.Stack("<API_KEY>").Branch("my-branch").Delete();Use PreviewToken to create or delete a Preview Token for a Delivery Token (compatible only with the rest-preview.contentstack.com endpoint):
ContentstackClientclient=newContentstackClient("<AUTHTOKEN>","<API_HOST>");// Create a preview tokenPreviewTokenModelmodel=newPreviewTokenModel(){Name="My Preview Token"};ContentstackResponsecreateResponse=client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>").Create(model);// Delete a preview tokenContentstackResponsedeleteResponse=client.Stack("<API_KEY>").PreviewToken("<DELIVERY_TOKEN_UID>").Delete();Use the Endpoint class to resolve Contentstack service URLs for any supported region without hardcoding hosts:
usingContentstack.Management.Core.Endpoints;// Endpoint// Resolve the Content Management endpoint for a regionstringurl=Endpoint.GetContentstackEndpoint("us","contentManagement");// Strip the https:// scheme — useful when passing the host directly to SDK configurationvaroptions=newContentstackClientOptions{Host=Endpoint.GetContentstackEndpoint("eu","contentManagement",omitHttps:true)};Use the following lines of code to fetch your stack detail using this SDK:
ContentstackResponsecontentstackResponse=client.Stack("<API_KEY>").Fetch();varresponse=contentstackResponse.OpenJsonObjectResponse();stringtitle=response["title"]?.GetValue<string>();// orStackResponsemodel=contentstackResponse.OpenTResponse<StackResponse>();You can use the following code to create an entry in a specific content type of a stack through the SDK:
EntryModelentry=newEntryModel(){Title:"Sample Entry",Url:"/sampleEntry"}ContentstackClientclient=newContentstackClient();Stackstack=client.Stack("API_KEY");ContentstackResponsecontentstackResponse=stack.ContentType("CONTENT_TYPE_UID").Entry().Create(entry);Use the following code snippet to upload assets to your stack through the SDK:
ContentstackClientclient=newContentstackClient();Stackstack=client.Stack("API_KEY");varpath=Path.Combine(Environment.CurrentDirectory,"path/to/file");AssetModelasset=newAssetModel("Asset Title",path,"application/json");ContentstackResponseresponse=stack.Asset().Create(asset);