API Trigger Custom Function

API Trigger Custom Function

AlertNote: The API Trigger Custom Function feature is currently in its beta stage with further enhancements planned in the product roadmap. Contact support@zohoiot.com to enable it for your application and for more details .
The API triggered custom function in Zoho IoT lets external or third-party applications execute a custom function through REST APIs. When triggered, the function runs immediately and performs whatever logic you’ve configured.

Inside the custom function, you can handle aggregation of device metrics, comparisons over time, fetching multiple data points, computing derived values, or combining readings across devices. The custom function processes everything and returns only the result required and can be invoked through webhooks.

API Trigger custom functions must be written using the Deluge scripting language.

This document explains how to create a API trigger custom function, how to access its REST API and provides an example code demonstrating its use.

Creating an API Trigger Custom Function

To create an API trigger custom function,
  1. Click Add Custom Function.



  2. Select API Trigger in the Category field.



  3. Provide the required values in the appropriate fields.

    Example -
    Language : Deluge
    Display Name: Fuel Consumption On Demand (Provide a name of your choice)
    Function Name: fuel_consumption_on_demand (Provide a name of your choice)
    Description: An API trigger custom function that aggregates the fuel consumed by two diesel generators from the start of the current month up to the present time.



  4. Click Save. This will open the Code Editor.



  5. Provide the required code.



  6. Click Save and Close.


The API trigger custom function is created. You can now use the RESP API of the custom function to trigger it from an external application.

Obtaining the REST API of the custom function

To obtain the REST API of a custom function,
  1. Hover over the required API Trigger custom function and Select the ellipsis icon that appear. For this illustration, Fuel Consumption On Demand is selected. 



  2. Select View REST API from the context menu.



  3. Hover over the REST API and copy it. 


You can now use this REST API to trigger the custom function.

API Trigger Custom Function Code Example

Below is an example of using a custom function to simplify third party integration. When a Zoho Creator based application requires fuel consumption data from multiple diesel generators, it invokes an API triggered custom function in the Zoho IoT application through a REST call. The function aggregates fuel usage across the relevant generators, processes the required calculations, and returns the consolidated results, allowing the Zoho Creator application to keep its webhook implementation minimal and free of complex processing.

url1 = "https://app12345akyrw.zohoiot.com/iot/v1/datapoints/data?datapoint_name=Fuel%20Consumed& source=DG001&aggregation=sum&time_grouping=month&period=thismonth";
response1 = invokeurl
[
url :url1
type :GET
connection:"datapointcirrus"
];
url2 = "https://app12345akyrw.zohoiot.com/iot/v1/datapoints/data?datapoint_name=Fuel%20Consumed& source=DG002&aggregation=sum&time_grouping=month&period=thismonth";
response2 = invokeurl
[
url :url2
type :GET
connection:"datapoint-iot"
];
info "RESPONSE:: " + response1 + response2;
fuel_consumed_dg1 = response1.get("Fuel Consumed").get(0).get(1);
fuel_consumed_dg2 = response2.get("Fuel Consumed").get(0).get(1);
fuel_consumed_dg_all = fuel_consumed_dg1 + fuel_consumed_dg2;
url3 = "https://www.zohoapis.com/creator/v2.1/data/user.useredu/dg-monitiring/form/Fuel_Data";
payload = Map();
dataList = List();
recordMap = Map();
recordMap.put("Fuel_Consumed",fuel_consumed_dg_all);
dataList.add(recordMap);
payload.put("data",dataList);
response3 = invokeurl
[
url :url3
type :POST
parameters:payload.toString()
connection:"creator-iot-post"
];