Connecting ZOHO CRM API using Java

Connecting ZOHO CRM  using Java

Copy and paste the following code into (overwrite the existing code):

import java.util.*; import*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.*; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.StringPart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.PartSource; import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Test { public static void main(String a[]) { try { // To Fetch Authentication Token String ticket = getAuthenticationToken(“ZohoCRM”, “ZOHO USERNAME”, “ZOHO PASSWORD”); System.out.println(ticket);
//TO FETCH ACCOUNTS Records String targetURL = “”; PostMethod post = new PostMethod(targetURL); post.setParameter(“authtoken”, ticket); post.setParameter(“scope”, “crmapi”); post.setParameter(“newFormat”, “2”); post.setParameter(“selectColumns”, “All”); HttpClient httpclient = new HttpClient(); long t1 = System.currentTimeMillis(); int result = httpclient.executeMethod(post); System.out.println(“HTTP Response status code: ” + result); System.out.println(“Time taken to execute :”+ (System.currentTimeMillis() – t1)); // Get response as a string —————- String postResp = post.getResponseBodyAsString(); System.out.println(“postResp=======>” + postResp); } catch (Exception e) { System.out.println(e.getMessage()); } } /** * @param string * @param username * @param password * @return */ private static String getAuthenticationToken(String name, String username, String password) { String strTicket = “”; String url = “” + username + “&PASSWORD=” + password + “”; URL u; try { u = new URL(url); HttpURLConnection c = (HttpURLConnection) u.openConnection(); InputStream in = c.getInputStream(); InputStreamReader ir = new InputStreamReader(in); BufferedReader br = new BufferedReader(ir); String strLine = null; while ((strLine = br.readLine()) != null) { if (strLine != null && strLine.startsWith(“AUTHTOKEN”)) { strTicket = strLine.substring(10); } } in.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return strTicket; } }

Note the the above code fetched Accounts records only. To fetch data from other CRM modules change the name in the targetURL. The other CRM modules are :

  1. Accounts
  2. Contacts
  3. Potentials
  4. Leads
  5. Campaigns
  6. Tasks
  7. Cases
  8. Solutions
  9. Products
  10. PriceBooks
  12. Vendors
  13. PurchaseOrders
  14. SalesOrders
  15. Invoices
  16. Notes
  17. Calls

 According to the  ZOHO CRM edition the access to CRM modules will be differ.



The Zoho CRM API lets you fetch CRM data from your Zoho CRM account. Get your records, add or remove records, search and update your records. Zoho CRM has numerous modules: leads, accounts, contacts, potentials, campaigns, tasks, events, cases, solutions, products, price books, quotes, vendors, purchase orders, sales orders, and invoices. 

Zoho CRM provides API (Application Programming Interface) for integrating CRM modules with any third-party applications. With the Zoho CRM API, you can extract CRM data in 
XML or JSON format and develop new applications or integrate with your existing business applications. As the Zoho CRM API is independent of programming languages, you can develop applications in any programming language (Java, .Net, C, C++, PHP, etc.).
There are Different API methods for Zoho CRM:

Method Name
To fetch data by the owner of the Authentication Token specified in the API request
To fetch all users data specified in the API request
To retrieve individual records by record ID
To fetch data with respect to the Custom View in Zoho CRM
To insert records into the required Zoho CRM module
To update or modify the records in Zoho CRM
To search records by regular expressions of selected columns
To search the values based on pre-defined columns
To delete the selected records
To convert lead to potential, account and contact
To get records related to a primary module
To get details of fields available in a module
To update records related to another record
To get the list of users in your organization
To attach a file to a record
To download a file attached to a record
To delete a file attached to a record
To add a photo to a contact or lead
To download the photo of a contact or lead
To delete a photo of a contact or lead
Until now, the Zoho CRM Developer API Implementation required the API Key and Ticket to be used in all API calls. But now a new method for ZOHO CRM API is the Authentication Token for all API calls. This token will become invalid ONLY if you choose to delete it.

To use the API, you’ll require the Zoho CRM Authentication Token from your CRM account. Please make sure that you have the permission to access the API service. If you do not have permission, please contact your CRM administrator.

To generate the Auth Token, you need to send an authentication request to Zoho Accounts using the URL format below. CRM Username or Email ID&PASSWORD=Your Zoho CRM Password