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
In order to run an API we must write the API using the fetch function in javascript.
The API is written within the fetch function along with the method and other headers.
The response of the API is stored in a “result” named variable.
According to the api response the result stores { “status”: “OK”} .
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;
To create further flow we return string variables which create nodes to connect further cards.
Hence write return “result”;
To display the response of the API , drag Send a Message card.
Use {{status}} to display the value of the status variable received through the api call
GET- Get a single Book
Use Ask a text card to ask the user the Id of the book he wishes to view.
Create a variable as book_Id and store the user input in this variable.
Drag and utility script card and write the below code to execute the API
Store the response in the result variable as shown below
Use flow variables to store the api responses and display in the further cards
Refer the below send a message card to view the variables to the end user
POST- Submit an Order
In order to place an order create two variables as bookId and customer_name and get their user input values from the user
Once obtained, drag the utility script card and write the below script
Begin by creating a headers method and writing the authentication key as provided in the API doc
Next use the stringify method in json to store the user inputs in the variables as shown below
Now implement the api using the fetch function in json as shown below
Save the results using the flow variables and display them in further cards as shown below