Serverless API With OpenWhisk, IBM API Connect, and Cloudant
This post is going to be based upon a previous post and video that I made that shows you how to quickly build an API with IBM API Connect, Node Red, and Cloudant. In this post we are going to actually create the same API, but instead of using Node Red for our internal API implementation we will be using OpenWhisk from IBM Bluemix for our internal API implementation. OpenWhisk is IBM’s new event driven compute technology for serverless computing that hides the existence of servers from developers. Serverless computing allows you to simply provide the code you want to be executed without having to worry about creating any servers to run your code. If you would like to see short video on how to do this checkout the video on YouTube.
So let’s get started by creating two actions that will connect to Cloudant and then format the response. Here is the code for calling my map/reduce view on Cloudant and here is the code for formatting the response from Cloudant.
Let’s first create an OpenWhisk action called summary for accessing my map/reduce view on Cloudant.
wsk action create summary summary.js
Now Let’s create an OpenWhisk action called formatter that formats the response from calling our map/reduce view on Cloudant.
wsk action create formatter formatter.js
Once we have our two actions defined we can chain them together into a new action called claims so that the output of the summary action becomes the input for the formatter action.
wsk action create claims –sequence summary,formatter
Now we will create an API on API Connect that will call our OpenWhisk chained action. In API Connect all we need to do is to invoke our action by using the OpenWhisk RESTful API endpoint for our action. The endpoint for accessing your action is based on the following pattern:
https://openwhisk.ng.bluemix.net/api/v1/namespaces/{your namespace}/actions/{your action}?blocking=true
So all you need to do is to replace {your namespace} and {your action} with your specific values for your OpenWhisk installation. You will also need to obtain your OpenWhisk username and password that will be needed to call the RESTful endpoint. You can find your OpenWhisk username and password from the OpenWhisk Configure CLI page. Here is a screenshot showing where you can find that on the page.
Once you have created your API on API Connect go to the assemble page and edit the invoke action to call your OpenWhisk action sequence. Be certain to include your OpenWhisk username and password and set the HTTP method to POST. Here is a screenshot showing where to insert the values.
At this point simply publish your API to IBM Bluemix just like you would for any other API. That’s all there is to it. Now you have an API running in the cloud without having deployed any server to run your API.