- Kontent.ai
Request Kontent.ai Delivery API in Node.js
I've already done a few websites with Kontent.ai in Node.js and I would like to share my experience. In this article, I will show you how to request Kontent.ai Delivery API in Node.js and provide you with a fully functioning code example.
Step by step tutorial
We will use promises in this example. What we need first are npm packages that will help us with that:
- request
- request-promise
Then require both packages in your code:
Loading...Now we define a class with Project ID and Preview API Key that represent your Kontent.ai project:
Loading...The class will contain the getContent method that requests the Kontent.ai storage and gets content from your project:
Loading...We call 2 helper methods in the getContent method:
- getRawData
- getOptions
Let's see what they do:
Loading...In the getOptions method, we call one more helper method - getDeliveryUrl. Let's complete that:
Loading...At this stage, we have the class and all methods in place. See the usage:
Loading...Full code example
Just copy and paste it into your project. Happy requesting!
Loading...Or see the live demo on Runkit.
If you are looking for code that is able to make multiple requests at a time read the following article: Multiple requests to Kontent.ai Delivery API in Node.js