REST API Functions

REST API Functions

The REST API function provides you the flexibility of triggering it from anywhere - within a function in an extension or from a third-party application. Third-party applications can invoke these API functions as webhook URL. There are two versions of the REST API function (explained in detail in later sections).

Authentication

The REST API function uses the API Key authentication method. The value of the ZAPI key to be used can be obtained using one of the following:

  • Custom Functions
  • Installation scripts

Using Custom Functions

  1. Publish the extension in which the REST API Function is included and install the extension.
  2. Log in to your application.
  3. Go to Setup > Automation > Actions and select the Functions tab.
  4. Click the Configure Function button.
  5. Click the Write your own link.
  6. In the Deluge Script Editor, paste the following code snippet and click Save & Execute Script

    m = {"nameSpace" : "<portal_name.extension_namespace>"};
    apikeyresp = zoho.crm.invokeConnector("crm.zapikey",m);
    zapikey = apikeyresp.get("response");
    info zapikey;

  7. The ZAPI key will be displayed in the Log messages.

Using Installation Scripts

  1. Log in to your Zoho Developer console and click Extensions for Zoho CRM.
  2. Select the extension in which the REST API Function is included, then click Edit.
  3. Select Custom Properties in the left pane.
  4. Click the Create button.
  5. Enter a variable name in  Field Name and API Name fields and click Save.
    This variable will be used in the installation script to store the URL of the REST API function (details are explained in the steps below).
  6. Select Install Actions in the left pane.
  7. Click Extension - On Installation.
  8. In the Deluge Script Editor, paste the below code snippet and click Save.  

    m = { "nameSpace" : "<portal_name.extension_namespace>" };
    apikeyresp = zoho.crm.invokeConnector("crm.zapikey", m);
    zapikey = apikeyresp.get("response");
    url = "https://platform.zoho.com/crm/v2/settings/custom_functions/<extension_namespace.
            function_name>/execute?zapikey=" + zapikey;
    m = { "apiname" : "<extension_namespace.variable_name>", "value" : url };
    r = zoho.crm.invokeConnector("crm.set", m);
    info r;

  9. Publish the extension in which the REST API Function is included and install the extension.
  10. Log in to your application.
  11. Go to Setup > Marketplace > All > Installed and click the extension name.
    The value of the variable "apifunctionurl" will be displayed under the Extension Settings section of the Extension Details page. This variable will have the ZAPI key value.

Version 2.0

Details of the Version 2.0 of the REST API function are given below:

Request Object

The entire request object of the function is stored in crmAPIRequest. The need to create multiple arguments within the function is eliminated as the crmAPIRequest object dynamically stores the information from the request and makes it available inside the function. The request object will be automatically mapped to the crmAPIRequest argument.

Input Types

NameDescription
BodyAny content that is passed as a stream to the request can be fetched using the component "Body" of crmAPIRequest.
request_body = crmAPIRequest.get("body");
ParametersThe parameter values passed in the request (either as key-value pair or as JSON object) can be fetched using the component "params" of crmAPIRequest.
parameters = crmAPIRequest.get("params");
File ContentAny Multipart content like a file can be fetched using the component "file_content" of crmAPIRequest.
filecontent = crmAPIRequest.get("file_content");
User InformationDetails about the user can be fetched using the component "user_info" of crmAPIRequest. If API key method is used, then details of the super admin is obtained.
user_info = crmAPIRequest.get("user_info");
Authentication MethodThe name of the HTTP method (GET, POST, etc.) used in the request can be fetched using the component, "method" of crmAPIRequest.
method = crmAPIRequest.get("method");
Authentication TypeThe name of the authentication method (API Key) used in the request can be fetched using the component "auth_type" of crmAPIRequest.
authtype = crmAPIRequest.get("auth_type");
HeadersThe information that is available in header (e.g. additional information about the request) can be fetched using the component "headers" of crmAPIRequest.
headers = crmAPIRequest.get("headers");

The input for the function can be acquired from the following types:

Body

Sending body using POSTMAN as a stream (raw):

Sending body using POSTMAN as a binary file:

To get the entire body section of the request in a function:

crmAPIRequestMap = crmAPIRequest.toMap();
request_body = crmAPIRequestMap.get("body");
company = request_body.get("Company");
name = request_body.get("last_name");
m = Map();
m.put("Last_Name",name);
m.put("Company",company);
r = zoho.crm.createRecord("Leads",m);
return "Lead created successfully ::: ";

In POSTMAN, the body content can be passed either in the raw or binary.

Parameters

Sending parameters using POSTMAN, within the request URL:

Sending parameters using POSTMAN as keys in form-data:

To get the additional parameters used in the request:

crmAPIRequestMap = crmAPIRequest.toMap();
parameters = crmAPIRequestMap.get("params");
company = parameters.get("Company");
name = parameters.get("last_Name");
m = Map();
m.put("Last_Name",name);
m.put("Company",company);
r = zoho.crm.createRecord("Leads",m);
return "Lead created successfully ::: ";

Sending parameters using POSTMAN, as JSON objects in form-data:

To get the additional parameters used in the request:

crmAPIRequestMap = crmAPIRequest.toMap();
parameters = crmAPIRequestMap.get("params");
args = parameters.get("args");
company = args.get("Company");
name = args.get("last_name");
m = Map();
m.put("Last_Name",name);
m.put("Company",company);
r = zoho.crm.createRecord("Leads",m);
return "Lead created successfully ::: ";

File Content

The file types currently supported are the text files. In order to send the file to the function as a multipart data, send it under the argument name "inputFile".

To get the file uploaded to be used in the function:

crmAPIRequestMap = crmAPIRequest.toMap();
parameters = crmAPIRequestMap.get("file_content");
company = parameters.get("Company");
name = parameters.get("last_name");
m = Map();
m.put("Last_Name",name);
m.put("Company",company);
r = zoho.crm.createRecord("Leads",m);
return "Lead created successfully ::: ";

User Information

To get the info about the users:

crmAPIRequestMap = crmAPIRequest.toMap();
// to get the user info of the request
user_info = crmAPIRequestMap.get("user_info");
/**
Your Business Logic here
**/
return crmAPIRequestMap;

Authentication Type and Method

To get the info about the authentication type:

crmAPIRequestMap = crmAPIRequest.toMap();
// to get the HTTP method of the request
user_info = crmAPIRequestMap.get("method");
// to get the authentication type of the request
user_info = crmAPIRequestMap.get("auth_type");
/**
Your Business Logic here
**/
return crmAPIRequestMap;

Headers

To get the headers of the request:

crmAPIRequestMap = crmAPIRequest.toMap();
// to get the user info of the request
header_request = crmAPIRequestMap.get("headers");
/**
Your Business Logic here
**/
return crmAPIRequestMap;

Return Type

The default return type is String in Version 2.0. You can specify the return message as String or you can define response object by using crmAPIResponse(explained below).

Response Object

In the argument crmAPIResponse, you can define the following specifications about the response:

Status Code: Use this key to define the status of the API call (successful or bad request). The default status code is 200.

response = Map();
response.put("status_code",204);
return {"crmAPIResponse":response};

Content-Type: Use this key to define the format in which you want the response. The default value of this key is application/json;charset=utf-8.

response = Map();
response.put("Content-Type","application/text");
return {"crmAPIResponse":response};

Headers: Use this key to define the values in header. The default value of this key is {"Content-Disposition", "attachment;filename=response.json"}.

response = Map();
headers = Map();
headers.put("X-ZOHO-SOURCE","CRM");
headers.put("X-Frame-Options","SAMEORIGIN");
headers.put("X-RATELIMIT-LIMIT","60");
response.put("headers",headers);
return {"crmAPIResponse":response};

Body: Use this key to pass the information that you need to send to the third-party as a response to their request. The default value of body is empty.

response = Map();
body = "{<xml>}"
response.put("body",body);
return {"crmAPIResponse":response};

Version 1.0

Request Object

KeyValue
parametersThese are the map type entities found in requestMap as key-value pairs. For Example, in the URL, https://platform.zoho.com/crm/v2/settings/custom_functions/
<extension_namespace.function_name>/execute?zapikey=apikey&action=mailreceived
, parameter is received as{"action":"mailreceived"} where "action" is the parameter name
and "mailreceived" is the parameter value. Then the map is got through the following code,
params = requestMap.get("parameters");
tp_event_responseIf the third party service sends data in InputStream, then the map is got through the code,
response = input.requestMap.get("tp_event_response")
fileContentAny Multipart content like a file can be fetched using the component "file_content" of requestMap.
filecontent = input.requestMap.get("fileContent");

Example

The following function code snippet calls the third-party "Twilio" REST API as a webhook and associates it with the application,

//use this for v1.0
twilioResp = (requestMap.get("parameters")).toMap();
//use this for v2.0
twilioResp = (crmAPIRequest.get("params")).toMap();
//below lines are common for both the versions
fromNumber = twilioResp.get("From");
messageContent = twilioResp.get("Body");
LeadRespList = zoho.crm.searchRecords("Leads", "(Mobile:equals:" + fromNumber + ")");
for each LeadResp in LeadRespList
{
LeadId = LeadResp.get("id");
if ((LeadId == null) || (LeadId == ""))
{
LeadId = "";
}
}
ContactRespList = zoho.crm.searchRecords("Contacts", "(Mobile:equals:" + fromNumber + ")");
for each ContactResp in ContactRespList
{
FinalId = ContactResp.get("id");
if ((FinalId == null) || (FinalId == ""))
{
FinalId = "";
}
}
updateMap = { ("twilioext.Incoming_Message_Content") : messageContent, "twilioext.SMS_Texts_Name" : ("Incoming SMS"), "twilioext.Lead" : LeadId, "twilioext.Contact" : FinalId, "twilioext.Direction" : "Inbound" };
m = map();
l = List();
l.add(updateMap);
m.put("module", "twilio.SMS_Texts");
m.put("data", l);
resp = zoho.crm.invokeConnector("crm.create", m);
signalleadResp = zoho.crm.getRecordById("Leads", LeadId.toLong());
Leademail = signalleadResp.get("Email");
if ((Leademail != "") || (Leademail != null))
{
signalMap = map();
signalMap.put("signal_namespace", ("twilioext.incomingsmssignal"));
signalMap.put("email", Leademail);
signalMap.put("subject", ("Incoming SMS"));
signalMap.put("message", messageContent);
actionsList = List();
actionMap = map();
actionMap.put("type", "link");
actionMap.put("display_name", "View Email");
actionMap.put("url", "www.google.com");
actionsList.add(actionMap);
signalMap.put("actions", actionsList);
result = zoho.crm.invokeConnector("raisesignal", signalMap);
info result;
}
signalcontactResp = zoho.crm.getRecordById("Contacts", FinalId.toLong());
Contactemail = signalcontactResp.get("Email");
if ((Contactemail != "") || (Contactemail != null))
{
signalMapx = map();
signalMapx.put("signal_namespace", ("twilioext.incomingsmssignal"));
signalMapx.put("email", Contactemail);
signalMapx.put("subject", ("Incoming SMS"));
signalMapx.put("message", messageContent);
actionsListx = List();
actionMapx = map();
actionMapx.put("type", "link");
actionMapx.put("display_name", "View Email");
actionMapx.put("url", "www.google.com");
actionsListx.add(actionMapx);
signalMapx.put("actions", actionsListx);
resultx = zoho.crm.invokeConnector("raisesignal", signalMapx);
info resultx;
}

Comparison of Version 2.0 and 1.0

 Version 1.0Version 2.0
Authentication MethodAPI KeyAPI Key
URL Patternhttps://platform.zoho.com/crm/v2/
settings/custom_functions/
{api_name_of_function}/
execute?zapikey="+zapikey;
https://crm.zoho.com/crm/v2/
functions/{api_name_of_function}/actions/
execute?auth_type=apikey&zapikey={zapikey}
Return Typevoid
The user can't return anything in the function.
String
The user can define how the response of the API is going to be. If the user wants to display or get a particular response code as the response of the function, they can specify it within the function. Besides status codes, the user can also choose to get the response in a specific file type, such as a JSON, HTML, Text, etc.
Request ObjectType : map
Name : requestMap
Type : map
Name : crmAPIRequest
Request Parameters
  • parameters
  • tp_event_response
  • fileContent
  • body
  • params
  • file_content
  • user_info
  • method
  • auth_type
  • headers
Changing the response of the API (using return type)Not supported/**
Your Business Logic here
**/
response = Map();
// to override the status code to 204.
response.put("status_code",204);
return {"crmAPIResponse":response};
Response{
"status_code": 200,
"response": "{\"result\":\"success\",\"info\":[<user given info message>]}"
}
{
"code": "success",
"details": {
"output": "",
"output_type": "string",
"id": "103353000000065018"
},
"message": "function executed successfully"
}

Invoke REST API functions from widgets

You can invoke a REST API function from a widget using the ZOHO.CRM.FUNCTIONS.execute() function. Using this function, you can pass user input values to the REST API function.

The API name of the function can be obtained from the Sandbox URL or Production URL (see below) that can be viewed by clicking Invoke as REST API in the function's deluge script editor.

https://platform.zoho.com/crm/v2/functions/function_apiname/actions/execute?auth_type=apikey&zapikey=​<zapikey>

var func_name = "<function_apiname>";

//obtain user inputs
var lastName = $("#last-name").val();
var company = $("#company").val();

var req_data ={
"arguments": JSON.stringify({
"Last_Name" : lastName,
"Company" : company
})
};
ZOHO.CRM.FUNCTIONS.execute(func_name, req_data)
.then(function(data){
        console.log("REST API function invoked from widget."+data);
})

Given below is the code snippet for the REST API function. Here, the user inputs from the widget is obtained through the params argument.

// to get the parameters of the request
params = crmAPIRequest.get("params");
// to fetch the parameters from the arguments key
args = params.get("arguments");
/**
Your Business Logic here
**/
return <function_response>;

Share this post : FacebookTwitter

Still can't find what you're looking for?

Write to us: support@zohodeveloper.com