How to use ZOHO CRM API V2 with The Request Module in Node.js.

Well, If you are frustrated after reading the official Documentation for ZOHO CRM API V2. Then this post is for you. Especially, Considering the mechanism they suggest to store the access keys, which is to use MySQL to store the keys.

I don’t know what others feel but I don’t want to configure and run MySQL for the sole purpose of storing keys. For the use of my application, I could easily get away by storing it in the memory and refreshing the access keys whenever the request was made. Considering the fact that refresh token never expires So, we can keep using this refresh token to generate an access token.

Creating CLIENT ID and Generating a Refresh Token

Go to https://accounts.zoho.com/developerconsole and click on add Client ID, Enter the details asked on the next page and click create. You will get your client id and secret. copy them and store them somewhere.

Now, You will need to generate a Grant code. Click the ellipsis menu on the client id select self client. Enter a Valid Scope. (info on scopes https://www.zoho.com/crm/developer/docs/api/oauth-overview.html#scopes ) We will give ourselves all the available operations in the scope module by using ZohoCRM.modules.ALL as the Scope. The grant code expires in the time frame chosen so be careful although you wouldn’t require more than 1 minute to generate a refresh token that lasts forever with it

So, after you have generated a Grant code with it. Use the url Structure below to send a post request. You will receive a refresh token as the response

let request = require('request');
let uri=`https://accounts.zoho.com/oauth/v2/token?code=${grant_key}&grant_type=authorization_code&client_id=${client_id}&client_secret=${client_secret}&redirect_uri=${redirect_uri}&response_type=code&access_type=offline`

request.post(uri,(err, response, body)=>{
    if(err){
        return console.log("An error Occurred ", err);
    }
    let refresh_token = body.refresh_token;
})

Now, The Refresh token we received is rather a persistent token . That only expire when

  • The Client Id or the app is deleted
  • The accpunt associated with the client id is deleted
  • The refresh token is revoked

In My application’s case, none of these events were ever going to occur. Therefore I decided to store the refresh token in the application’s memory also I decided to generate access_token for every time a lead was going to get created. Ideally, you would want to check whether the access_token expired and then only request for a new access token(You also recieve the token expires_at value).

Now You could construct a config object.

  let config = {
            "client_id": "Your client Id here",
            "client_secret": "Your client Secret here",
            "redirect_url": "Your Redirect URI",
            "refresh_token": "Refresh Token recieved",
            "iamurl": "accounts.zoho.com",
            "base_url": "www.zohoapis.com",
            "mysql_module": "local",
            "user_identifier": "Email id of the user you used to 
                                create the client ID"
        };

Requesting for Access Token

let uri = `https://accounts.zoho.com/oauth/v2/token?refresh_token=${config.refresh_token}&client_id=${config.client_id}&client_secret=${config.client_secret}&grant_type=refresh_token`
        request.post(uri, (err, response, body)=>{
            if(err){
                console.log("Zoho crm response", err, body);
            }
            config.accessToken = JSON.parse(body).access_token;
            });

Create A Lead

The Endpoints to get all the data from Leads modules would be (You might want to change the domain of the API depending upon the location) :

https://www.zohoapis.eu/crm/v2/Leads

To create a lead. We would need to send a post request to the above URL with header data that would contain the access key generated with the refresh key. Ideally, you might not want to nest a callback inside a callback. So you could rather return a promise or more ideally use the request-promise module but I am going with callbacks So after getting the access tokens you would use request module to send a post request :

let headers = {
                'Authorization': 'Zoho-oauthtoken ' + config.accessToken,
                'Content-Type': 'application/json'
            };
request.post({url, body:lead, headers, json: true}, function (err, response, body) {
                console.log("Zoho crm response", err, body);
                if (err) {
                    console.log("Failed to add as lead in Zoho CRM. Error: ", err);
                }
                console.log("TESTING ZOHO API V2",body.data[0].details);              
            });

The URL represents the endpoint to hit. Also, remember whatever the lead data should be in JSON format as v2 API no longer supports XML data.

Get All Leads

let headers = {
                'Authorization': 'Zoho-oauthtoken ' + config.accessToken,
                'Content-Type': 'application/json'
            };
request({url: options.url, headers}, function (err, response, body) {
                console.log("Zoho crm response", err, body);
                if (err) {
                    console.log("Error getting response", err);
                }
                console.log("Leads Data",body.data);              
            });

Simply send a get request to get the list of leads. Additionally, You could specify no of data to fetch by simply passing a query string to URI, for example, To fetch five leads you could use page and per_page as query strings.

https://www.zohoapis.eu/crm/v2/Leads/page=1&per_page=5

Similarly, You could specify search criteria. for example:

https://www.zohoapis.com/crm/v2/Leads/search?criteria=((Last_Name:starts_with:G)

Similarly, You could perform Delete and update operations by passing in the id of the leads.

You could also use this example to do CRUD operation on other modules like Accounts, Contacts, Deals, Forecasts, Activities, etc. Just replace leads in the above URL by the module name you want to work with.

To get more info on the endpoints of particular modules goto official Zoho docs here.

Leave a Reply