Getting Started with the commercetools API Hub

commercetools Frontend comes with great business tools to help you manage a website; it has user-friendly features such as drag-and-drop. However, we're going to focus on an often overlooked feature in this article, which is the commercetools API Hub.

This article delves into a piece of commercetools, which is commercetools Frontend. commercetools Frontend came from the Frontastic acquisition, but commercetools has rewritten the application and made major improvements. You sometimes hear people refer to it as “CoFe”, which is short for “commercetools Frontend.” Most people think of frontend as a website, built with NextJS and hosted on the cloud, but it is so much more. For starters, commercetools Frontend comes with great business tools to help you manage a website; it has user-friendly features such as drag-and-drop. However, we’re going to focus on an often overlooked feature which is the commercetools API Hub.

The API Hub allows companies to handle API orchestration in a central area. It is also MACH as it is built on microservices. You get multiple environments including production, staging, and review branches. Developers can spin up an API Hub directly from the CLI. Let’s walk through coding in action in CoFe’s API Hub.

Building an Action

Let’s have the commercetools documentation titled “Developing an action extension” pulled up for reference. The first step will be “Implementing the Action.” Under this step in the documentation example, you can see that the namespace is “Star Wars” and “character” is the action name. For our example, we will use “Hello World” as our namespace and “Hello” as our action name.

commercetools API Hub documentation example

Inside the code, scroll to the Backend folder, and create a new folder titled “Hello World.” Create a new file inside the folder called “index.ts.”

creating the file folders in the code

To set up our first action, we enter “export default”, “actions”, “hello-world” as the namespace, and “hello” as the action name. Inside the function, we enter a “return”, “hello world” as the body, and “200” as the statusCode.

first action code

For testing purposes, we will add another action. You may copy and paste the code from the first action or enter it again. Edit the second action’s name to say “world” and edit the body to say “another hello world.”

second action code in the commercetools API Hub


Before testing these actions, we need to import them to the index.ts extensions file folder. Open the folder and enter “import” “HelloWorldextension” from “hello-world”. Enter the extension name “HelloWorldextension” in the list further down as well.

import the actions into the index.ts folder

Testing an Action

Now we move over to Postman to test our actions. Click “Create Collection” and then “Add a request.”

create collection in Postman
add a request in Postman

Name it “CoFe API Hub” at the top of the page.

name it CoFe API Hub in the commercetools API Hub

We’ll need to hunt down a few pieces in order to get the full URL. First, enter “HTTP”. Go to the CLI, copy the “Hostname”, and paste that into Postman next. Second, go to the “Developing an action extension” documentation and copy the test code under “2. Use and test the action.” Paste that last in Postman.

getting the hostname from the CLI
copy the url code example

Edit the test code of the URL to cater to our example; the namespace will be “hello-world” and the first action will be “hello.” Hit Send.

edit the URL and hit send

It’s going to show an error because we’re missing one Key and Value inside the “Headers” tab.

it throws an error in the commercetools API Hub

Back in the “Developing an action extension” documentation, copy this header key and paste it under the “Headers” tab in Postman.

copy the header key
paste the header key in postman

To get the missing Value, go to commercetools, click on the profile icon, and click “Profile” in the drop-down menu.

click on your profile in commercetools

In the box that pops up, you can see the “Developer header username” field. Copy this and paste it in the Value under the “Header” tab in Postman. Click “Send” again.

copy your developer header username in commercetools
paste the key in postman and hit send again in the commercetools API Hub

Now it works! You can see the correct body text, “hello world”, appears.

successful test one

Test the “world” action as well by editing the URL at the top to say “world” instead of “hello.” Hit send a third time, and “Another Hello World” appears as it should in the body.

successful test two

Questions about getting started with commercetools API Hub?

Please reach out to us – we are happy to help! If you prefer the vlog version of this article and to browse other helpful tutorials, be sure to check out our YouTube page! Give us a follow and comment for more helpful content.

One comment

  1. Every time I visit your website, I’m greeted with thought-provoking content and impeccable writing. You truly have a gift for articulating complex ideas in a clear and engaging manner.

Leave a Reply

Your email address will not be published. Required fields are marked *