How to use YaaS with Node-RED

As you’ve seen in our previous blog post the YaaS Client SDK for Node.js is already available and open sourced. If you haven’t read it yet and are setting up a YaaS project, I recommend to start with it first. It’s a good starting point to setup a new YaaS project, learn how to subscribe to the Product Content (Beta) package and tells you where you can find the Client Credentials. All the things we need for the following Node-RED examples.

Install Node-RED

After you’ve setup a YaaS project with the YaaS Builder you are ready to install Node-RED. You install it with npm, therefore you need Node.js installed. Yaas.js requires a Node.js version 4.0 or higher. For more information on how to install Node.js and Node-RED the Node-RED Installation page explains it very nicely.
For those who are familiar with Node.js and npm, this is what you need:
    sudo npm install -g --unsafe-perm node-red
Node-RED will be installed globally, so you only have to type node-red in a Terminal and the Node-RED server starts.
When Node-RED starts you can see the Node-RED version, Node.js version and also the User directory. The User directory is the path where you install node-red-contrib-yaas

Installing node-red-contrib-yaas

To install node-red-contrib-yaas, you stop Node-RED, change to the Node-RED User directory (see above) and install it with the following command and then start it again:
     cd ~/.node-red
     npm install node-red-contrib-yaas
     node-red
This will create a node_modules directory (if it doesn’t exist) and install all necessary packages, including the YaaS Client SDK.
 
When you startup Node-RED again and open a browser at http://127.0.0.1:1880/ (or refresh the page) you will see the Node-RED application:
 
Node-RED overview with YaaS nodes

This is a Node-RED overview with a YaaS Node.

On the left side is a list of all installed Nodes, grouped by Input, Output, Function and also Yaas. In the middle of the window you define the flows and on the right you can see an Info and Debug tab. When you select a node (on the left side) the Info tab shows information about this node. The Debug tab allows you to show debugging information from the debug node.
 
Lets start.

Our first Node-RED flow – product by query

First we select the “products by query” node and drag & drop it in the flow. Then we add an inject node from the input group and a debug node from the output group.
 
When you double click on the node you can configure it: 
 
YaaS Credentials input

YaaS Credentials with Client Id, Client Secret and Application ID.

To configure the YaaS Node you need the YaaS Credentials and Application ID. You can find this information in your project. Open in the YaaS Builder your project and click on Client of the left side navigation. See also the YaaS Client SDK blog post to learn how to setup a YaaS project.
 
The Application ID (Identifier) is also at the Client section where Client ID and Secret is.
 
Change the inject node to String and the text to sku:"YOUR PRODUCT NUMBER" (e.g.sku:"8700810087"). You can find the Product Number at a defined Product (note: the quotation marks are required).
 
configuring inject node

Edit inject node to set the product number (sku).

Now you connect the inject Node with the “product by query” Node and the end of it with the debug Node. At the end you hit the red Deploy button (top right) and then you press the inject Node. If everything went fine, you should see a JSON output on the debug tab:
[ { "product": { "id": "569fa69c924e1f2bc1724d7b", "sku": "8700810087", "name": "Wizard Queen", "description": "Fight mystic wars on the chessboard! Let the White Witch’s powers guide you to victory!", "published": true, ...
The JSON output contains the product id, the sku (Product Number), name, description, and so one. We need the product id for our next example.

Product by ID Node example

The product id can be used for the “product by ID” node. Therefore Drag & drop the “product by ID” node in the flow and configure it with a double click. In this case the Credentials should already be filled and you only need to hit the Done button.
 
Now add another inject Node with the String of the product Id (e.g. 569fa69c924e1f2bc1724d7b). Connect these nodes and also with the debug node (or add a new debug node). Deploy, clean the debug tab and then press the new inject Node. You should see the same result as before.
 
YaaS nodes

YaaS nodes ‘product by query’ and ‘product by ID’.

We have two different ways to find a product and we are ready to add this product to a shopping cart.

Lets go shopping – add to cart example

Drag & drop the “add to cart” node and configure it. Therefore you need a customer, which you can register on your YaaS shop in a browser window (http://shops.yaas.io/YOUR_CLIENT_IDENTIFIER) . Credentials and the other options are the same as before.
 
Now connect the end of “product by ID” with the “add to cart“, deploy it and hit the inject node.
YaaS node add to cart

Add to cart node connected.

When you open your shop and login with the customer account, you will see the product in the shopping cart. Just hit a few more times the inject Node, refresh the shop page and you’ll see how the shopping cart is filled up.
 
These are the basics to start and play further with:
  • instead of only one product use other products
  • use name instead of sku as search query at product by query
  • and more advanced: take the product id from the product by ID result, change the msg.payload (hint: payload[0].product.id) with a change node and use the result for product by query
Next time I’ll show how a trigger can be used to add a product and to do a checkout.
 
Further information:

7 thoughts on “How to use YaaS with Node-RED

  1. Diksha Gupta says:

    Hi, This is Diksha Gupta from TechGig.com. Can we carry this tutorial on our portal for Hybris community?

  2. Saday says:

    Hi,
    Can i use SAP or hybirs website blog links to share in to my facebook page or any other social media pages?

Comments are closed.