invokeAPI Task
Table of Contents
Overview
The invokeAPI task is an HTTP client that allows you to access and modify the data of one Zoho service from another Zoho service using APIs.
Unlike the InvokeURL task where you specify the complete URL to access a specific resource mentioned in the API document, with invokeAPI, only the path needs to be specified. The domain of the Zoho service is fetched automatically based on its data center for making an API call, ensuring accessibility across all data centers.
Note:
- This task will throw "socket timeout error" if the web resource/ API takes more than 40 seconds to respond.
- The default connection timeout will happen in 10 seconds, and the connection request timeout in 5 seconds.
- In Zoho Creator, this task can download file up to 5 MB. Some older accounts would allow downloads up to 50 MB which will eventually be reduced without affecting the existing scripts. Learn more.
- In services other than Zoho Creator, you can download Zoho domain files of size up to 15 MB and other domain files of size up to 5 MB using the relevant APIs.
Syntax
response = invokeapi [ service:<service_name> path:<path_value> type:<type_value> parameters:<parameters_value> body:<body_value> headers:<headers_value> connection:<connection_name> response-format:<response_format_value> response-decoding:<encoding_format_value> ];
Parameter | Data type | Description |
<response> | KEY-VALUE/ FILE/ TEXT/ LIST | The variable that will contain the response returned. |
<service_name> | TEXT | The name of the Zoho service whose resources need to be accessed. Allowed Values: |
<path_value> | TEXT | The URL path of the service that needs to be accessed, excluding the base URL. For example, if the request URL is "https://www.zohoapis.com/crm/v3/Leads" the path will be "/crm/v3/Leads" |
<type_value> (optional) | Constant | The HTTP request method. Allowed values:
Default value: GET |
<parameters_value> (optional) | TEXT/ KEY-VALUE | Parameters are query strings that will be appended to the end of a URL. They facilitate the transmission of specific data to the server for processing or filtering purposes. |
<body_value> (optional) | TEXT/ FILE/ KEY-VALUE | The body of the request. Send various types of request body using invokeAPI task: text/plain: This type is used to supply values to the API in raw TEXT format without any specific encoding or formatting. application/x-www-form-urlencoded: The data is sent to the API as KEY-VALUE pairs. This encoding is commonly used for sending form data in HTTP requests. multipart/form-data: The data sent to the API is structured as a multipart form in KEY-VALUE format. This format is particularly useful when dealing with file uploads, allowing for the transmission of binary data along with additional metadata. application/json: The data is sent to the API in JSON format. It is commonly used for sending structured data in HTTP requests, allowing for the representation of complex data structures, including objects and arrays. Note: When sending data in JSON format to the API, ensure it is converted to TEXT format using the toString() function. See Example application/octet-stream: This type is employed when sending FILE data. Data is transmitted in its raw, binary format without any specific encoding. Info: Refer here to learn how to send key-value data with this parameter. |
<headers_value> (optional) | KEY-VALUE | The attributes or the header values. |
<connection_name> | TEXT | The connection name of the required service. Note:
|
<response_format_value> (optional) | TEXT | The data type in which the response must be returned. Allowed values:
Default value: DEFAULT Note: If the value of this parameter is set to NONE, the response won't be returned. |
<encoding_format_value> | TEXT | The character encoding scheme based on which the response returned needs to be decoded. Allowed values: Click here to find the list of allowed values for this parameter. Default value: UTF-8 |
The following functions can be used along with the response to get a detailed response.
<response>.status | Returns the response code of the respective request. |
<response>.headers | Returns the header of the respective request |
Format to Send the Key-Value Data
Follow the format below to send the key-value data with the body of the invokeAPI request if the value associated with the key is,
FILE:
Note:
- Only file objects, such as those returned by an invokeAPI or invokeURL task, and data converted to file using the toFile function, can be attached. For example, a text value formatted as CSV can be converted into a CSV file using the toFile function. Please note that this function is currently not applicable to Zoho Creator.
- In Zoho Creator, input text that is converted into a file cannot exceed 5MB. Some older accounts currently allow conversion up to 50MB, but this limit will eventually be reduced without affecting existing scripts.
<variable> = {{"param-name": <key>, "param-value": <File>}};
TEXT/ MAP:
<variable> = {{"param-name": <key>, "param-value": <value>, "content-type":"<content_type_value>", "encoding-type":"<encoding_type_value>"}};
LIST (list of KEY-VALUE data with the associated 'param-values' as FILE, TEXT, or MAP):
list_ = List(); list_variable.add({"param-name": <key>, "param-value": <File_1>}); list_variable.add({"param-name": <key>, "param-value": <File_2>}); list_variable.add({"param-name": <key>, "param-value": <text_value>, "<content_type_value>":, "encoding-type":"<encoding_type_value>"}); list_variable.add({"param-name": <key>, "param-value": <map_value>, "<content_type_value>":, "encoding-type":"<encoding_type_value>"});
Example 1: Fetch Organization Details from Zoho Recruit
The below script fetches the organization details from your Zoho Recruit account. Click here to learn more about the API used in this example.
response = invokeapi [ service: ZohoRecruit path: "/recruit/v2/org" type: GET connection: "recruit_connection" ];
info response;
Where,
"/recruit/v2/org"
GET
"recruit_connection"
Example 2: Create a Sales Order in Zoho Books
The following script creates a sales order with the specified values in Zoho Books. Click here to learn more about the API used in this example.
// Create a map that holds the values of the new salesorder that needs to be created item = Map(); item.put("item_id",205788000000122285); item.put("rate", 120); item.put("name", "Hard Drive"); item.put("description", "500GB, USB 2.0 interface 1400 rpm, protective hard case.");
// Format the data as specified in the Zoho Books API item_list = List(); item_list.add(item);
data = Map(); data.put("customer_id",205788000000127011); data.put("line_item", item_list);
// Supply the parameters to the invokeApi task response = invokeapi [ service : ZohoBooks path : "/books/v3/invoices" type : POST parameters : {"organization_id": "10xxx59"} body : data headers : {"content-type":"application/x-www-form-urlencoded"} connection : "zohobooks_connection" ];
info response.status;
Where,
"/books/v3/invoices?organization_id=10xxx695"
POST
"zohobooks_connection"
Example 3: Upload an Attachment to Zoho CRM Record
The following script uploads an attachment to the specified Zoho CRM record. Click here to learn more about the API used in this example.
//Fetch the file using the invoke URL task. Attachment = invokeurl [ url :"https://www.cmu.edu/blackboard/files/evaluate/tests-example.xls" type :GET ];
//Create a variable to store the record id of the record where the attachment to be uploaded. Record_ID = "2689962xxxxx509150";
//Supply the parameters to the invokeapi task. response = invokeapi [ service :zohocrm path :"/crm/v7/Leads/"+Record_ID+"/Attachments" type :POST body:{{"param-name":"file","param-value":Attachment}} connection:"zohocrm_connection" ]; info response.status;
Where,
Attachment
Record_ID
"/crm/v7/Leads/"
POST
"zohocrm_connection"
Example 4: Update a Contact in Zoho Inventory
The following script updates the billing address of a contact with the specified details in the Zoho Inventory. Click here to learn more about the API used in this example.
//Create a variable to store JSON value. Address = {"attention": "Mr.John","address": "4900 Hopyard Rd","street2": "Suit 310","city": "Pleasanton", "state":"California","zip": 94588,"country": "U.S.A"};
//Map the Address json with the shipping_address parameter as specifies in the Zoho Books API address_map = Map(); address_map.put("shipping_address",Address);
//Supply the parameters to the invokeApi task. response = invokeapi [ service :ZohoInventory path :"/inventory/v1/contacts/4600xxxxx026049" type :PUT parameters : {"organization_id": "10234695"} body :address_map.toString() headers :{"content-type":"application/json"} connection :"zohoinventory_connection" ];
Info response.headers;
Where,
Address
"/inventory/v1/contacts/"
PUT
"zohoinventory_connection"
Response Format
The standard response will be returned in the following format:
"responseText": "Example.png"
}
To return the header of the request use the following script.
<variable> = <response>.headers;
The response for the request to return the header will be in the following format:
{
"date": "Wed, 23 Oct 2019 10:03:31 GMT",
"server": "Apache",
"content-length": "159500",
"expires": "Wed, 23 Oct 2019 16:03:31 GMT",
"x-endurance-cache-level": "2",
"x-cache-lookup": "MISS from 172.30.232.40:3128",
"via": "1.1 172.30.232.40 (squid/4.1)",
"last-modified": "Wed, 04 May 2016 01:44:18 GMT",
"content-type": "image/png",
"connection": "keep-alive",
"x-cache": "MISS from 172.30.232.40",
"accept-ranges": "bytes",
"cache-control": "max-age=21600"
}To return the response code of the request, use the following script:
<variable> = <response>.status;
The response for the request to return the response code will be in the following format:
200