Process data in the Flow and build custom logic using the API

Use CognigyScript and work with Code Nodes to enhance your Virtual Agent

CognigyScript is a superset of JavaScript which gives you access to the Input and Context objects within text and JSON. CognigyScript can also be used in Code Nodes to execute powerful scripts that make your Virtual Agent smart.

#1 Check if the API call was successful and display results

After the HTTP Request from the previous step, the Flow should check if the API returned one or more results. Add an If Node after the HTTP Request. You can traverse through the Context object to identify the right attribute to check for. For this example, configure the following condition:

context.result_character.result.count > 0

Now add a Say Node in the Else branch as a fallback for the number of results is zero. Use this text:

Never heard that before, sorry.

Add another Say Node in the Then branch and configure it to render the result of the API call. Use the following expression:

Ah, I know {{context.result_character.result.results[0].name}}!

As the search can deliver more than one hit, [0] ensures that only the first element from the array is displayed.


#2 Make an HTTP request based on existing data

Let’s make another HTTP request to fetch the homeworld of a character from the Star Wars API. The URL for this request is already included in the Context object from the previous API call, so you can directly access it. Add another HTTP Request Node under the Say Node in the Then branch and use this expression in the URL field:


As Context Key, use the following term to store the data in the Context object:



#3 Use a Code Node to process data

As a next step, let’s enable the Virtual Agent to use the right preposition "he" or "she" when referring to a character. An easy way to achieve this is processing a character’s gender from the Context object. Add a Code Node after the last HTTP Request and enter the following TypeScript code:

const gender = context.result_character.result.results[0].gender;

switch (gender) {
case "male":
context.preposition = "He";
case "female":
context.preposition = "She";
context.preposition = "He/She";

This code sets a variable "preposition" in the Context object that holds the gramatically correct preposition as a string.



#4 Render the results

You can now add a Say Node after the Code Node. Here you should render the correct preposition and then display the homeworld of a character:

{{context.preposition}} is from {{}}.

In this tutorial, let's instead use the API to generate the output. Any kind of information or action in the UI can be queried or triggered through the API - also from external systems. Add a second Code Node and enter the following code:

actions.output(`${context.preposition} is from ${}.`);

The actions class serves as an entry point into the API.


#5 Try it out!

Try out the experience in the Interaction Panel. Use inputs like Luke, Laia or Gandalf and compare the results





  • "Preposition" is the wrong element of English to refer to He, She, etc.

    The correct term is "Pronoun".

    Additionally, the text refers to a second code node that is is not represented in the graphic.

  • Thanks Ron, much appreciated. We'll fix the points you mentioned in the next content update.


Please sign in to leave a comment.

Was this article helpful?
1 out of 1 found this helpful