Improve your development experience by using strongly typed models based on your Kontent.ai content types.
When getting content directly from the Delivery REST API, you receive content items as JSON objects. Your app then needs to parse the JSON to display your content. For example, to access the Headline element in a content item, you’d use a notation like this
response.data.item.elements.headline.value
.
In real-life scenarios, this approach might be cumbersome because it requires you to remember the JSON structure of your content items, and the codenames of your elements, types, languages, and other entities.
To make this easier, use a Delivery SDK (the JS version or the .NET version) to map the retrieved content items to their strongly typed models.
The video below, by Cameron Tape, our Senior Technical Support Engineer, shows how to set up a TypeScript-based web app using Next.js and install the JavaScript Delivery SDK and the model generator to get and display content from Kontent.ai.
This practice has several advantages:
type safety during compile-time
convenience (IntelliSense remembers content type properties for you)
support of type-dependent functionalities
Each model corresponds to a content type in your Kontent.ai project.
A strongly typed model provides type-safe access to:
All elements defined within a specific content type
System fields such as type, workflow, language, and more
The following example is a strongly typed model of the Homepage content type.
Use the model generator to create strongly typed models from your Kontent.ai project by providing your environment ID.
With your models defined and added to your application, you can use them when retrieving items from Kontent.ai.
Sign in with your Kontent.ai credentials or sign up for free to unlock the full lesson, track your progress, and access exclusive expert insights and tips!
Shows how to use strongly typed models of content types from a sample Kontent.ai project in a TypeScript-based Next.js app.
C#
// This code was generated by a kontent-generators-net tool // (see https://github.com/kontent-ai/model-generator-net).// // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // For further modifications of the class, create a separate file with the partial class.using System;using System.Collections.Generic;using Kontent.Ai.Delivery.Abstractions;namespace KontentAiModels{ public partial class Homepage { public const string Codename = "homepage"; public const string BodyTextCodename = "body_text"; public const string HeadlineCodename = "headline"; public const string PictureCodename = "picture"; public IRichTextContent BodyText { get; set; } public string Headline { get; set; } public IEnumerable<IAsset> Picture { get; set; } public IContentItemSystemAttributes System { get; set; } }}
// Tip: Find more about .NET SDKs at https://kontent.ai/learn/netusing Kontent.Ai.Delivery;using KontentAiModels;// Tip: Use DI to create Delivery client https://kontent.ai/learn/net-register-clientIDeliveryClient client = DeliveryClientBuilder .WithEnvironmentId("8d20758c-d74c-4f59-ae04-ee928c0816b7") .Build();// Gets a content item by codename and maps it to the item's strongly typed modelIDeliveryItemResponse<Homepage> response = await client.GetItemAsync<Homepage>("hello_caas_world");var homepage = response.Item;// Use homepage// Console.WriteLine(homepage.Headline);
Turn your content types into strongly typed models | Kontent.ai Learn