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:
- Log in to Dev Center.
- Create a new application.
- 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. - Install the application to the sandbox of your choice.
- 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.
- 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‑sSiteID.sandbox.mozu.com
. 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:
- Create a new directory on your local machine.
- Open a command prompt in the new directory.
- Run
npm init
to create apackage.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 isindex.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. - Run
npm install --save mozu-node-sdk
to install the Node SDK. - 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": "https://home.mozu.com", // the base url for generating auth tickets - this value is always "https://home.mozu.com" unless you are on an internal server "basePciUrl" : "https://pmts.mozu.com/", // used to create credit cards in Mozu. "https://payments-sb.mozu.com" for sandboxes, "https://pmts.mozu.com" 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": "you@email.com" // the dev account email for authenticating to Arc.js and theme sync tools } }
Make a call to the API in your main program file:
- In your project root directory, create a file called index.js.
- Code
index.js
to match the following example, which obtains an API context, creates a customer account resource, and uses thegetAccounts
function 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(accounts); console.log("Number of Customer Accounts:"); console.log(accounts.totalCount); }).catch(function(error) { console.error(error); });
- In your project directory, run node index.js. The application logs the number of customer accounts to the console.
About the Toolkits
NPM Package Name | Description |
---|---|
mozu-node-sdk | The 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:
- View the SDK source on GitHub or in the
node_modules/mozu-node-sdk/clients
directory that gets installed in your project folder. - 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.