Go to main content

Create content item with Kentico Kontent Content Management API

In this post, I will briefly explain the logic behind creating content items with the Kentico Kontent Content Management API and provide you with a Node.js code example. 

Create content item with Kentico Kontent Content Management API

Kentico launched a beta version of its new Content Management API. Amazing about this API is that it brings the possibility of writing data! Now you can create, update, and delete content items in your code. Here you can read the documentation.

The API works in a very similar manner to the Delivery API. However, there are several differences you need to keep in mind: 

  • It uses other domain for endpoints: https://manage.kenticokontent.ai/.
  • All requests have to be authenticated with the Content Management API key.
  • The Content Management API key is valid for 30 days after you generate it.
  • The primary use-case seems to be for one-off content migration. Not for writing data from your application UI. 
  • Listing  returns only general data about content items so doesn't replace the Delivery API.
  • To create and fill a content item with data you need to make 2 or more requests:
    • The first one creates the content item and fills it with general data.
    • The other ones fill content elements with data. Each language variant of the content item requires one additional request.

To give you an idea of how the API could used I created the following code example in Node.js. It creates a new content item and fills its content elements with data in one language variant.

The code example has 2 dependencies:

npm install --save request
npm install --save request-promise
const requestPromise = require('request-promise');

// Information needed for the content item creation 
var settings = {
    projectId: '548121...4ab0950', // Project ID
    contentManagementAPIKey: 'ew0Ks4LvlH...30Fp23ieA8',  // Content Management API Key
    languageVariantCodename: 'default', // Codename of the content language
    // Complete data for item creation
    data: {
        name: 'Management API test post',
        type: {
            codename: 'blog_post'
        },
        sitemap_locations: [{
            codename: 'posts'
        }],
        elements: {
            heading: 'Management API test post'
        }
    }
}

// Helper function that returns object with specified keys
const getSpecificObjectKeys = (object, keys) => {
    var keysObject = {};

    keys.forEach((element) => {
        if (object[element]) {
            keysObject[element] = object[element];
        }  
    });

    return keysObject;
}

// Function that creates the content item in 2 step
// Step 1. - Adds content item to the Kentico Kontent Storage, https://docs.kontent.ai/reference/management-api-v1#operation/add-a-content-item
// Step 2. - Fills content elements of the content item with data, https://docs.kontent.ai/reference/management-api-v1#operation/upsert-a-content-item
const createItem = (settings) => {

    // Split data into 2 separate objects for each step  
    var addData = getSpecificObjectKeys(settings.data, ['name', 'type', 'sitemap_locations']),
        upsertData = getSpecificObjectKeys(settings.data, ['elements']);

    // Request options for step 1
    var addOptions = {
        method: 'POST',
        uri: 'https://manage.kontent.ai/v1/projects/' + settings.projectId + '/items',
        auth: {
            'bearer': settings.contentManagementAPIKey
        },
        body: addData,
        json: true
    };

    // Perform step 1 
    requestPromise(addOptions)
        .then((response) => {

            // Request options for step 2
            var upsertOptions = {
                method: 'PUT',
                uri: 'https://manage.kontent.ai/v1/projects/' + settings.projectId + '/items/' + response.id + '/variants/codename/' + settings.languageVariantCodename,
                auth: {
                    'bearer': settings.contentManagementAPIKey
                },
                body: upsertData,
                json: true
            };

            // Perform step 2
            requestPromise(upsertOptions)
                .then((response) => {
                    console.log(response);
                })
                .catch((error) => {
                    console.error(error);
                });
        })
        .catch((error) => {
            console.error(error);
        });  
}

// Usage
createItem(settings);

This example could be extended to be able to create:

  • multiple content items
  • in multiple languages

to cover more advanced scenarios. Maybe a topic for another post.

Further reading

all posts