search

How to Call OpenWhisk Actions from Node Red


This post is to help you get started with calling your OpenWhisk actions from Node Red on IBM Bluemix. I will explain how you can quickly invoke your action using OpenWhisk’s RESTful API from a Node Red flow using the HTTP Request Node. To get started you need to define your action using the wsk command line utility. For instructions on how to define wsk actions follow this link.

To get started first define your action using the wsk command line. Here is a sample action that I have written in JavaScript. This action simply looks at a customer record and then decides if they should be offered a discount and then inserts a message into the customer record. To define the action simply issue the create command:

wsk action create offer offer.js

Now that we have our action defined we need to obtain the credentials and URL to access the action as a RESTful API. To obtain this information we are going to invoke the action from the command line using the wsk utility with the -v option so that we can see all the details of what occurred when we invoked the action.

wsk -v action invoke –blocking –result –param monthsSinceLastClaim 25 –param numberOfPolicies 3 offer

Here is a screenshot of a portion of the output after we issue the command. The key elements that we will copy are the URL and the Headers.

OpenWhisk Credentials

OpenWhisk Credentials

Now we are ready to setup the call to our RESTful action from a Node Red flow. The first thing that we will do is to create a Function Node and set the url and headers parameters that we will use when we invoke the Node Red HTTP Request Node. Here is a screen shot that shows how I defined my Function Node.

Node Red Function Setup

Node Red Function Setup

The next step is to wire the output of the Function Node to an HTTP Request Node. Here is how I configured my HTTP Request Node. Be certain to use the POST method and indicate that the return type is a parsed JSON object.

HTTP Request Node

HTTP Request Node

The next step is to extract the JSON result from the call to the RESTful action. You need to do this as you cannot indicate that you only want to have the result returned when calling an OpenWhisk action from the RESTful API. All you need to do is to create a Function Node to extract the result. Here is a screenshot of my Function Node.

Extract OpenWhisk Result

Extract OpenWhisk Result

The last step is to simply wire up your Node Red flow. Here is how I wired up my flow.

OpenWhisk Node Red Flow

OpenWhisk Node Red Flow

I hope this post has helped you get started with calling OpenWhisk actions from Node Red.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

close
search
Latest Tweets

Hi, guest!

settings

menu