• Cheat sheets
  • Documentation
  • API reference
  • Product updates
  • Sign in
Kontent.ai Learn
  • Try Kontent.ai
  • Plan
  • Set up
  • Model
  • Develop
  • Create
Copyright © 2024 Kontent.ai. All rights reserved.
  • Web
  • Privacy policy
  • Cookies policy
  • Consent settings
  • Security
  • GDPR
  • Overview
  • Manage API keys
  • Hello World
  • Hello Web Spotlight
  • Try sample apps
  • Build apps
  • Decide navigation and URLs
  • Environments
    • Overview
    • Migrate from other systems
    • Automate migrations with CLI
    • Import content via API
  • Get Developer Certification

Import rich text

Aaron Collier, Jan Cerman
16 minutes
Download PDF
  • TypeScript
  • .NET
  • REST
0% complete
In this lesson, you learn how to import rich text content and use it in a content item.

What you'll import

Imagine you want to import a rich text field that looks something like this:
See the example on https://kontent-ai-import-rich-text.netlify.app/
This is composed of a few elements:
  • A header
  • Some paragraphs with:
    • An external link (to Wikipedia)
    • An internal link (to second-page.html within this website)
  • A button
  • An unordered list
  • An image
You want to make sure to capture all of these elements within your rich text in Kontent.ai. The original HTML of what you'll be bringing in is below:
  • HTML

Setting up your content types

To import this rich text, you'll need to have at least two content types in your project. The first is a type to hold the rich text, for which you can experiment with any rich text in your project or create a very simple type in the UI or via Management API, as in the following examples.
  • cURL
The second type will hold your button. You can again do it in the UI or the API, with the API giving you the option to use external IDs, which will make it easier to reference things that you haven't created yet.

Getting your rich text ready

Now that you have a place to put your rich text, it's time to get the text itself ready. The first step to getting it ready to be used as a string is to take the original HTML and escape line breaks and quotes.
  • JSON
This is a good start, but if you were to try to import this directly into a rich text element you'd get an error. Neither <a> nor <img> tags are allowed as top level elements in rich text. For the <img> tag, you can easily convert it to an asset in Kontent.ai by changing it to a <figure> tag with an external ID: <figure data-asset-external-id=\"rich-text-asset\"></figure>. As you know if you've imported linked content, Kontent.ai can store references to external IDs even for items that haven't been created yet. So you can always add in the asset later. For the <a> tag, you'll want to turn this into a component. This gives you the benefit of having a clear structure for your button that you can use repeatedly. You can then use the structured data for any presentation depending on the context instead of having a hard-coded link that can't work in all situations. So instead of an <a> tag, you'll use an <object> tag defined based on components in Kontent.ai with a unique GUID you can generate yourself. The result will be something like <object type=\"application/kenticocloud\" data-type=\"component\" data-id=\"a2ee7bac-15ff-4dce-a244-012b9f98dd7b\"></object>.

Linking to other content items

If you were to import your rich text string as it is (with <a href=\"second-page.html\">), the link would be assumed to be an external link and http:// would be added as a prefix (resulting in <a href=\"http://second-page.html\">). To fix this, you want to change the link from an external link to an internal one. You can again take advantage of references to external IDs while creating your internal link so that you can reference an item you'll create later. Just write your  <a> tag as follows:  <a data-item-external-id=\"second-page\">) (or whatever you want the other page to have as an external ID).

Your prepared rich text string

If you've followed the steps above, the string for your rich text should be something like this:
  • JSON

Filling in your component

You have a reference to a component in your rich text string, but you don't yet have a matching component. You need to create an object that you can enter into the components property of your rich text.  Your button has two elements: its text and the link to be opened. You created a Button content type to hold such components, so now you can use that type to hold your data. Use the external IDs you created when adding the type. A single component object using the GUID from the rich text would look like this:
  • JSON
Now you can see the clear structure of your button, which can be useful when you implement such buttons in your app.

Importing your rich text

With your rich text and components ready, you can create a content item to hold it and then add your content. First, upsert an item of the Simple rich text type (you'll use the Button type for a component, so you don't need to create an item for it). 
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!
Sign in
  • cURL
  • cURL
<h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur <a href="https://wikipedia.org">adipiscing elit</a>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<a href="https://kontent.ai" class="cta">Buy me</a>
<p> Ut enim ad minim veniam, <a href="second-page.html">quis nostrud</a> exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p>
<ul><li>Lorem ipsum dolor sit amet</li><li>Consectetur adipiscing elit</li><li>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</li> </ul>
<img src="https://kontent.ai/img/general/logos/kai-black-text.svg" alt="Kontent.ai" />
"<h1>Lorem Ipsum</h1>\n<p>Lorem ipsum dolor sit amet, consectetur <a href=\"https://wikipedia.org\">adipiscing elit</a>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>\n<a href=\"https://kontent.ai\" class=\"cta\">Buy me</a>\n<p>Ut enim ad minim veniam, <a href=\"second-page.html\">quis nostrud</a> exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>\n<ul>\n  <li>Lorem ipsum dolor sit amet</li>\n  <li>Consectetur adipiscing elit</li>\n  <li>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</li>\n</ul><img src=\"https://kontent.ai/img/general/logos/kai-black-text.svg\" alt=\"Kontent.ai\" />"
"<h1>Lorem Ipsum</h1>\n<p>Lorem ipsum dolor sit amet, consectetur <a href=\"https://wikipedia.org\">adipiscing elit</a>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>\n<object type=\"application/kenticocloud\" data-type=\"component\" data-id=\"a2ee7bac-15ff-4dce-a244-012b9f98dd7b\"></object>\n<p>Ut enim ad minim veniam, <a data-item-external-id=\"second-page\">quis nostrud</a> exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>\n<ul>\n  <li>Lorem ipsum dolor sit amet</li>\n  <li>Consectetur adipiscing elit</li>\n  <li>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</li>\n</ul><figure data-asset-external-id=\"rich-text-asset\"></figure>"
{
  "id": "a2ee7bac-15ff-4dce-a244-012b9f98dd7b",
  "type": {
    "external_id": "button"
  },
  "elements": [
    {
      "element": {
        "external_id":"button-text"
      },
      "value": "Buy me"
    },
    {
      "element": {
        "external_id": "button-link"
      },
      "value": "https://kontent.ai"
    },
  ]
}
  • Content model
  • Content items
  • Linked content
  • Assets
  • Rich text
curl --request POST \
  --url https://manage.kontent.ai/v2/projects/KONTENT_AI_ENVIRONMENT_ID/types \
  --header 'Authorization: Bearer KONTENT_AI_MANAGEMENT_API_KEY' \
  --header 'Content-type: application/json' \
  --data '
{
 "external_id": "simple-rich-text",
 "name": "Simple Rich Text",
 "elements": [
   {
     "name": "Rich Text",
     "external_id": "rich-text",
     "type": "rich_text"
   }
 ]
}'
curl --request POST \
  --url https://manage.kontent.ai/v2/projects/KONTENT_AI_ENVIRONMENT_ID/types \
  --header 'Authorization: Bearer KONTENT_AI_MANAGEMENT_API_KEY' \
  --header 'Content-type: application/json' \
  --data '
{
 "external_id": "button",
 "name": "Button",
 "elements": [
   {
     "name": "Text",
     "external_id": "button-text",
     "type": "text"
   },
   {
     "name": "Link",
     "external_id": "button-link",
     "type": "text"     
   }
 ]
}'
curl --request PUT \
  --url https://manage.kontent.ai/v2/projects/KONTENT_AI_ENVIRONMENT_ID/items/external-id/simple-example \
  --header 'Authorization: Bearer KONTENT_AI_MANAGEMENT_API_KEY' \
  --header 'Content-type: application/json' \
  --data '
{  
  "name":"Simple example",
  "type":{
    "external_id": "simple-rich-text"
  }
}'
Best practice: Upsert by external ID You can use a simple POST to /items request to add the content item. But using an UPSERT operation and defining an external ID for your item has advantages and makes the import process much smoother:
  • You can run the same request repeatedly. If the item doesn't exist, it will be created. If it does, it will be updated.
  • You can reference or link to your item, even if it hasn't been imported yet (and has no internal ID or codename). You might have other content items that reference this one in Rich text or Linked items elements. But if you are using external IDs you don't need to worry about the order in which the content items are imported.
  • What you'll import
  • Setting up your content types
  • Getting your rich text ready
  • Filling in your component
  • Importing your rich text