Node SDK

This guide explains how to develop applications using the Node SDK.

Before You Begin

Install the following software on your local machine:

  • Node.js—Provides a platform for creating scalable network applications. Includes the npm package manager, which you use in this tutorial to install the Node SDK.
  • Command Line Interface—Provides a console or terminal environment in which to execute commands. If you are developing on a Windows machine, it is recommended to use the Windows command prompt. However, due to known Windows compatibility issues in Node.js, you may encounter issues when using the SDK tools. If you encounter any issues, use a third-party console emulator, such as cmder, which plays nicer with Node.js.

In addition to the listed software, you need access to a Dev Center Account that contains a provisioned sandbox.

Get Started

The following steps guide you through installing the Node SDK, authenticating your project with your Kibo credentials, and making a call to the API. Specifically, the tutorial demonstrates how to create a console application that retrieves the number of customer accounts for a Kibo eCommerce site, teaching you the necessary concepts for then building a fully-fledged application of your own.

Create an application in Dev Center with the appropriate behaviors:

  1. Log in to Dev Center.
  2. Create a new application.
  3. Add the Customer Read behavior to the application. This step is necessary to give your application the necessary permissions to read customer accounts. If you design additional functionality for your application, such as updating an order, add the appropriate behaviors to avoid a permissions error.
  4. Install the application to the sandbox of your choice.
  5. Enable the application in Admin. If you decide to add additional behaviors to your application after this step, you must reinstall the application to your sandbox and re-enable the application in Admin to apply the new behaviors.
  6. Note the application key, shared secret, tenant ID, and site ID. You can obtain the application key and shared secret from the application details page. You can obtain the tenant ID and site ID by viewing your live site through Site Builder and looking at the URL, which has the pattern tTenantID‑ You can obtain the master catalog ID through a GetTenant API call, which also returns the tenant ID and site ID, but the master catalog ID is not required for the API call used in this tutorial.

Create a Node.js application that uses the Node SDK:

  1. Create a new directory on your local machine.
  2. Open a command prompt in the new directory.
  3. Run npm init to create a package.json file in your directory. When prompted, provide a name for your npm package and accept the default values for the remaining prompts, making sure that the entry point for your application is index.js. When you build a fully-fledged application, you can customize these responses, instead of accepting the default values like you do for this tutorial.
  4. Run npm install --save mozu-node-sdk to install the Node SDK.
  5. In your project's root directory, create a file called mozu.config.json. Specify your application configuration data within this file, as shown in the following code block, replacing the placeholder values with your application-specific values. You must remove the comments from the code below. They are provided for instructional purposes, but will error your application because the JSON format does not support comments.
        "appKey": "yourAppKey",
        "sharedSecret": "yourSharedSecret",
        "tenant": 12345,
        "site": 12345,
        "master-catalog": 12345, // not required for tutorial - shown for educational purposes
        "baseUrl": "", // the base url for generating auth tickets - this value is always "" unless you are on an internal server
        "basePciUrl" : "", // used to create credit cards in Mozu. "" for sandboxes, "" for production tenants
        "developerAccountId": 1234, // unique ID of the developer account contacting App Dev - not required if you are not calling App Dev services 
        "developerAccount": { // not required for this tutorial
            "emailAddress": "" // the dev account email for authenticating to Arc.js and theme sync tools

Make a call to the API in your main program file:

  1. In your project root directory, create a file called index.js.
  2. Code index.js to match the following example, which obtains an API context, creates a customer account resource, and uses the getAccountsfunction to return the number of customer accounts on the tenant:
    // generate an API context based on config data by using application.js in the specified SDK directory
    var apiContext = require('mozu-node-sdk/clients/platform/application')();
    // create a customer account resource from the customerAccount.js file in the specified SDK directory
    var customerAccountResource = require('mozu-node-sdk/clients/commerce/customer/customerAccount')(apiContext);
    // log the customer accounts objects and the total number of accounts, using the JavaScript Promise programming structure
    customerAccountResource.getAccounts().then(function (accounts) {
        console.log("Number of Customer Accounts:");
    }).catch(function(error) {
  3. In your project directory, run node index.js. The application logs the number of customer accounts to the console.Log text saying

About the Toolkits

NPM Package NameDescription
mozu-node-sdkThe Node SDK package.

View the source on GitHub

SDK Function Reference

The Node SDK provides dozens of built-in functions that help you interact with the API quickly and easily. To familiarize yourself with these functions:

  1. View the SDK source on GitHub or in the node_modules/mozu-node-sdk/clients directory that gets installed in your project folder.
  2. Dig through the source files or use the repository search box (on GitHub) to find the function you are looking for. The files mirror the structure of the API.

For example, to find the functions related to customer segments, you would view the customerSegment.js file located in the node_modules/clients/commerce/customer directory.