How to trigger OpenWhisk Actions on Cloudant Databases Changes

Yesterday I wrote about how to write JavaScript Actions for OpenWhisk on Bluemix. Actions can be triggered manually (as described in previous article) and when certain events occur. Below is a simple sample how to trigger OpenWhisk actions when documents in Cloudant NoSQL databases are changed or added.

Let’s use the same scenario as previously. In a help desk application you want to translate tickets which have not been written in English to English so that support agents understand. Rather than doing this when tickets are stored or opened the OpenWhisk action is triggered when new documents are stored in a Cloudant NoSQL database.

The documentation describes how to build this scenario. Essentially you need in addition to the JavaScript action a trigger and a rule. Invoke the following commands via the CLI and replace your credentials, namespace and database name.

wsk property set --auth <yourOpenWhiskUser>:<yourOpenWhiskPassword> --namespace "<yourOpenWhiskNameSpace>"
wsk package bind /whisk.system/cloudant myCloudant -p username '<yourCloudantUser>' -p password '<yourCloudantPassword>' -p host '<yourCloudantHost>'
wsk package list
wsk package get /<yourOpenWhiskNameSpace>/myCloudant
wsk trigger create myCloudantTrigger --feed /<yourOpenWhiskNameSpace>/myCloudant/changes --param <yourCloudantDBName> testdb --param includeDocs true

To see the trigger in action create a JavaScript action ‘hello.js’ and and rule.

function main() {
   return {payload: 'Hello world'};
wsk action create hello hello.js
wsk rule create --enable sampleRule myCloudantTrigger hello
wsk activation poll

When you change documents or add documents in your Cloudant database you can see logs in your terminal window and the OpenWhisk dashboard.


  • Jo Torsmyr

    Hi Niklas!
    Thanks for this, exciting stuff!
    I want to use the REST API, for doing this, but I don’t have the wsk CLI right now (I am on Windows…).
    Would it be possible for you, or someone, to run the wsk commands with the -v option to output the REST API calls and show them on this page?!


  • Jo Torsmyr

    I think I have the package binding working in REST API.
    But the feed part seems harder, the REST API documentation is not that detailed on that…;-)


    { “binding” : { “name” :”cloudant”, “namespace” :”whisk.system” }, “publish” :true, “parameters” : [ { “value” :”mycloudantuser”, “key” : “username” }, { “value” :”mycloudantpw”, “key” : “password” }, { “value” : “”, “key” : “host” } ], “version” :”1″ }

  • Jo Torsmyr

    Hi again!
    I have installed wsk on my Mac now, and the example seems to work fine :-)

    I think the trigger creation command should be:

    wsk trigger create myCloudantTrigger –feed //myCloudant/changes –param dbname –param includeDoc true

    I.e. dbname is the parameter name (key property in REST API), and includeDoc seems to work. The documentation is somewhat unclear on this, so I tried both includeDocs and includeDoc, and includeDoc works.

    I will try to gather the REST API calls corresponding to the wsk commands and post them here. My goal is to use the REST API to dynamically create triggers and actions.


  • Krishna Chandrasekar

    Nice article…

    I have a basic question…
    From the dashboard I see the trigger’s response is


    “changes”: [
    “rev”: “21-10cc2e5a696af12d9432f907af8e9abf”
    “seq”: “6035-g1AAAARbeJy90sFtwjAUBmCLIJUt4I6a2rGdwqlsUvJiuwkKBpFw4ARM0k7QFegmZYVOkBo7FPWGicTlWbLkX59-vwIh1MsCgZ4EpIuVnAiIwhzmj8uVLKVOZa7L_C2rSoxJmBaLtUh0FWpZFeZhJ0HQr-t6lgWAUNCfm7sHDpTx0dg_8b-CXa-AgZnwcoFsLASUwJIo_9Cb64DJCfJ6gRALSRmNqYjv2cj0BNn-QTo_DsIpxyN5x0Z010y0M4exvDeYvcVIICTGrVvhvpgPhzk0XzS0mEhESpGxf3DLZr4c5rtp5tNiKBOU8-e2zVBfzNFhzjuzdTsDNGYq8Q-e_QI4S2_1”,
    “id”: “60774aec-dae1-4155-af60-8bc22517fbee”

    Is there a way I can capture this response (or make use) in my action associated with this trigger (mySampleCloudantTrigger).

    My intention to capture the ID which is affected in Cloudant database.

    • Jo Torsmyr

      Yes you can get to the ID in an action, after creating a rule which associates the trigger with that action (like the wsk rule command Niklas shows us).

      I do not look for the ID specifically in my action (I look at some other property of the document from Cloudant), but you should be able to do something like in the actions main function:

      function main(params) {

      var id =;


      • Krishna Chandrasekar

        Thanks Jo…I will try that. In addition to “id” i am using multiple other properties as well from the Cloudant.

        • Jo Torsmyr

          Cool. Here is my entire action, with only the name of my other action masked.
          stationfilter is a default parameter injected at creation-time of the action. Then I compare that (static) value with the corresponding property from Cloudant documents.
          If matched, I call another OpenWhisk action to do further processing.

          function main(params) {

          var payload = params.payload || params;

          var stationfilter = params.stationfilter;

          if (stationfilter == payload.station) {

          var name = ‘/myNamespace/anotherAction’;

          var parameters = { station : stationfilter, doc : payload };

          whisk.invoke({ name : name, parameters : parameters });

          return whisk.done({ notify : ‘Station ‘ + stationfilter + ‘ == ‘ + payload.station, payload : payload });

          } else

          return whisk.done({notify : ‘Station ‘ + stationfilter + ‘ != ‘ + payload.station});