Utility Script Help Guide

In this tutorial we will be guiding you through how to use the utility script to run APIs. 

UTILITY SCRIPT

  • Use Utilities to add custom logic or scripts using JavaScript. You can execute APIs and display responses based on your logic.

  • To create additional nodes in the utility script, you need to include return statements with string variables. For example, writing return "true"; will generate a node named "true." 

  • The fallback node is a default node created to handle the flow when the utility script fails to run at any situation.

  • Multiple nodes can be created as per the requirement. 

  • In order to display the response of the api in further flow, one can use the _flow_variables.variable_name.

  •  To display this variable in any other card one can write the variable as {{variable_name}} and present it to the end user

  • Also kindly use the fetch function to call apis and perform crud operations.



You can refer this link for the API documentation 

GET- Return status of the API


  1. In order to run an API we must write the API using the fetch function in javascript.

  2. The API is written within the fetch function along with the method and other headers.

  3. The response of the API is stored in a “result” named variable.


  4. According to the api response the result stores { “status”: “OK”} .

  5. In order to display the value of the status, create a variable using flow variables and store the value of the key you wish to

    _flow_variables.status = result.status;

  6. To create further flow we return string variables which create nodes to connect further cards.

  7. Hence write return “result”;

  8. To display the response of the API , drag Send a Message card.

  9. Use {{status}} to display the value of the status variable received through the api call

GET- Get a single Book

  1. Use Ask a text card to ask the user the Id of the book he wishes to view.

  2. Create a variable as book_Id and store the user input in this variable.

  3. Drag and utility script card and write the below code to execute the API


  4. Store the response in the result variable as shown below


  5. Use flow variables to store the api responses and display in the further cards

  6. Refer the below send a message card to view the variables to the end user

    POST- Submit an Order

    1. In order to place an order create two variables as bookId and customer_name and get their user input values from the user

    2. Once obtained, drag the utility script card and write the below script

    3. Begin by creating a headers method and writing the authentication key as provided in the API doc

    4. Next use the stringify method in json to store the user inputs in the variables as shown below

    5. Now implement the api using the fetch function in json as shown below

    6. Save the results using the flow variables and display them in further cards as shown below


Prev
Next