Node.js

Use Contentstack to power content for your Node.js projects

Create Node.js-based applications and use the Node.js SDK to fetch and deliver content from Contentstack. The SDK uses Content Delivery APIs.

Getting Started

This guide will help you get started with our Node.js SDK to build apps powered by Contentstack.

SDK Installation and Setup

Open the terminal and install the contentstack module via the ‘npm’ command.

$ npm install contentstack --save

To use the module in your application, you need to require the ‘contentstack’ module.

import * as Contentstack from 'contentstack';

Initialize SDK

You will need to specify the API Key, Delivery Token, and Environment Name of your stack to initialize the SDK.

// Initialize the Contentstack Stack
const Stack = Contentstack.Stack("api_key", "delivery_token", "environment_name");

Once you have initialized the SDK, you can start getting content in your app.

Basic Queries

Get a Single Entry

To get a single entry, you need to specify the content type as well as the id of the entry.

const Query = Stack.ContentType('blog').Entry("blta464e9fbd048668c")
Query.fetch()
   .then(function success(entry) {
       console.log(entry.get('title')); // Retrieve field value by providing a field's uid
       console.log(entry.toJSON()); // Convert the entry result object to JSON
   }, function error(err) {
       // err object
   });

Get Multiple Entries

To retrieve multiple entries of a content type, you need to specify the content type uid. You can also specify search parameters to filter results.

const Query = Stack.ContentType('blog').Query();
Query
   .where("title", "welcome")
   .includeSchema()
   .includeCount()
   .toJSON()
   .find()
   .then(function success(result) {
      // result is array where -
      // result[0] => entry objects
      // result[result.length-1] => entry objects count included only when .includeCount() is queried.
      // result[1] => schema of the content type is included when .includeSchema() is queried.
  }, function error(err) {
     // err object
 });

These were example of some of the basic queries of the SDK. For advanced queries, refer to our API reference documentation by visiting the link given below.

API Reference

Go through our SDK API Reference guide to know about the methods that can be used to query your content in Contentstack.

Read Node.js API Reference Guide

Example Apps

To help you get started, we have created some sample applications that are powered by Contentstack Node.js SDK. Click on any of the links below to read the tutorials of the app, view app demo, or download the code from GitHub.  

Was this article helpful?
top-arrow