Code Nodes


Code Nodes enable a Flow creator to execute custom JavaScript or TypeScript code within the Flow. The editor has full IntelliSense and exposes all Cognigy objects and actions. For better performance the TypeScript code will be transpiled on save. If the transpilation fails the code will be saved and an error-icon will appear in the top-right corner of the Code Node, indicating that there is a potential issue with your code.


Code Node without and with error-Icon

The execution of the Code Node will be synchronous, the Flow will continue after the Code Node finished executing.

Just as within other Nodes, ci, cc and cp can be accessed (and modified) within Code Nodes. If the script crashes or takes longer than one second to execute, it will be stopped and throw an error. In case of an uncaught error, the Flow Execution will be stopped.

The actions object provides access to most internal node functions within the Code Node.

For convenience, the lodash ( and moment ( libraries are exposed.

Sending Facebook JSON

You can send Facebook JSON directly from within Code Nodes using the output action. You can do this in two ways:

  • Writing the JSON yourself
// build the faceboook reply
const obj = {
    "_cognigy": {
        "_facebook": {      
          "message": {
            "text": "Hello World",
            "quick_replies": [
                "content_type": "text",
                "condition": "",
                "title": "Hi",
                "image_url": "",
                "payload": "Hi"

// output the reply
actions.output("test", obj);
  • Using the facebook-bot-messenger module (more information on GitHub
// use facebook-bot-messenger to compile reply
const builder = new MessengerPlatform.QuickRepliesMessageBuilder('Pick a color:');
builder.addImageOption('Red', 'DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_RED', '')
       .addImageOption('Green', 'DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_GREEN', '');

// output the reply
actions.output("test", { "_cognigy": { "_facebook": {"message": builder.buildMessage() }}});

What’s Next