You’ve chosen Contentful as your traditional CMS solution. Now how do you choose which API to use? Contentful has five different content APIs, each with a different setup. Let’s walk through an example of creating a comment for an entry using the Content Management API.
The Content Management API
To begin, the Content Management API (CMA) is a read-write API that allows you to upload files, query asset tags, leave comments, and much more. The following example will show how to leave a comment on an entry. Following this example closely should allow easy exploration of other CMA calls and facilitate working with the other four Contentful APIs.
Setting up the POST parameters
Here is the POST endpoint for creating a comment:
First, we’ll prepend this string with “https://api.contentful.com”, which is the API base URL for the CMA. In our examples, we’ll refer to this as “API_SERVER”. Next, let’s get the “environment_id”. You can do this by logging into the Contentful web portal, clicking on the hamburger icon in the top left corner, and grabbing the name of the environment in which you wish to leave the comment.
In this case, the text that starts with “v2” is “the environment_id”.
The “entry_id” can be found in the “Info” section of a given content type. Here, our “entry_id” is the string that begins with “42”.
Next, we will get the “space_id” via the Contentful command-line interface (CLI). On macOS, run
brew install contentful-cli, then authenticate with
contentful login. Next run
contentful space list to find the relevant Space id. Finally, we will need a “cma_token” for authorization. After authenticating with the Contentful CLI, find the “cma_token” at
cat ~/.contentfulrc.json to find the token. Please note: In the JSON file the value we want is associated with the key of “managementToken”, but our examples will refer to this as the “cma_token”.
Creating a Comment
We are now ready to make a create comment POST call to the CMA. For testing purposes, we will use Postman. The final call will look like this:
You will also need to include the “cma_token” in the headers as follows:
Lastly, please add a “body” value to the body section of the POST call. This string will be the actual text that shows up as the comment for the entry in the Contentful web portal. For example:
If everything was configured correctly, the body comment will be the return response, along with a “sys” value noting some other metadata about the comment creation.
Head back to the Contentful web portal in the browser, and you should see the comment appear in the associated entry.
Congratulations! You have just successfully created a comment using the CMA!
Comment Using the Contentful Management API
The Contentful Management API (CMA), along with the other Contentful APIs, provides a powerful toolset to create unique experiences. My team is using the CMA to create a custom commenting tool that helps team members collaborate asynchronously on a given entry. I hope you enjoyed a preview of working with the CMA, and please let me know in the comments if you run into any issues!