USING ZOHO API FOR LEADS IN NODE JS

Zoho CRM is a popular customer relationship management software that allows businesses to manage their sales, marketing, and customer support activities. The Zoho CRM API allows developers to integrate Zoho CRM with other applications and services.

Emmanuel Unyime
Geek Culture
Published in
4 min readMar 21, 2023

--

ZOHO

In this tutorial, we will walk you through the process of setting up the Zoho API and adding leads to your Zoho CRM account using Node.js and the Axios library.

Prerequisites

Before you start, you will need the following:

  • A Zoho CRM account
  • A Zoho API Console account
  • Node.js installed on your system
  • Basic knowledge of Node.js and RESTful APIs

GETTING STARTED

Step 1: Create a Self Client in Zoho API Console

To use the Zoho API, you will need to create a Self Client in the Zoho API Console. Follow these steps:

  1. Log in to your Zoho API Console account.
  2. Click on the “Add Client” button and choose “Self Client”.
  3. Give your Self Client a name and description.
  4. Under “Scopes”, add “ZohoCRM.modules.all”.
  5. Choose a time duration for the token and add a description.
  6. Click on the “Generate Code” button.

These steps should look like this

  • Copy the “Client ID” and “Client Secret” values.

Step 2: Getting the Refresh Token

To get the Refresh Token, you can use Postman or any other API platform of your choice. Follow these steps:

  1. Create a new request in Postman.
  2. Set the request method to “POST” and the URL to https://accounts.zoho.com/oauth/v2/token"
  3. Configure the Headers type as such:

Set the header to “Content-Type: application/x-www-form-urlencoded”.

  1. In the “Body” section, add the following values:
  • code: The code generated in the previous step.
  • grant_type: authorization_code
  • client_id: The client ID generated in the previous step.
  • client_secret: The client secret generated in the previous step.
  • redirect_uri: The redirect URI of your choosing

Click on the “Send” button to make the request.

The response will contain a “refresh_token” value. Copy this value and store it somewhere safe as you will need it later.

Alternatively (not recommended), you can use the following code with Axios to get the refresh token:

const addUserToLeads = async () => {

const options = {
url: 'https://accounts.zoho.com/oauth/v2/token',
method: 'post',
params: {
code: '1000.xxxxxxxxxxxxxxxxxxxxxxxxxxx',
grant_type: 'authorization_code',
client_id: '1000.xxxxxxxxxxxxxxxxx',
client_secret: 'c2cfexxxxxxxxxxxxxxxxxxxxxxxxxx',
redirect_uri: 'http://localhost:8000',
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
};
const response = await axios(options);
const refreshToken = response.data.refresh_token;
}

addUserToLeads();

I do not advise this since you only need to run this code once and get the refresh token seeing as the generated code has a limited time for being valid

Step 3: Generate Access Token

Now that you have the refresh token, you can use it to generate an access token. Follow these steps:

The request looks very similar to the previous one, save for a few changes

const addUserToLeads = async () => {
...
const options = {
url: ‘https://accounts.zoho.com/oauth/v2/token',
method: ‘post’,
params: {
refresh_token: 1000.xxxxxxxxxxxxxxxxxxxx,
grant_type: ‘refresh_token’,
client_id: ‘1000.xxxxxxxxxxxxxxxxx’,
client_secret: ‘c2cfexxxxxxxxxxxxxxxxxxxxxxxxxx’,
},
headers: { ‘Content-Type’: ‘application/x-www-form-urlencoded’ },
};
const responseRefreshToken = await axios(options);
...
}

const accessToken = responseRefreshToken.data.access_token

Upon success, we get an access_token from this as initialized above, i.e accessToken

Step 4: Adding the Lead to Zoho CRM using the Access Token

Now that we have obtained the access token, we can use it to add a lead to Zoho CRM. We will make a POST request to the Zoho CRM API with the lead data in the request body.

Here’s the code to add a lead to Zoho CRM using the access token:

const addUserToLeads = async () => {
...
const response = await axios.post(
'https://www.zohoapis.com/crm/v2/Leads',
{
data: [{
Company: company,
Last_Name: lastName,
First_Name: firstName,
Email: email,
}],
trigger: [
'approval',
'workflow',
'blueprint',
],
},
{
headers: {
Authorization: `Zoho-oauthtoken ${accessToken}`
},
}
);
console.log(response.data, 'Added to ZOHO CRM');
...
}

In the request body, we are sending the lead data in JSON format. We are setting the Company, Last_Name, First_Name, and Email fields to the values we obtained earlier. The trigger field is an array of triggers that will be executed when the lead is added to Zoho CRM. In this case, we are triggering approval, workflow, and blueprint actions.

We are also setting the Authorization header to include the accessToken. This header tells Zoho CRM that the request is authorized and authenticated.

If the lead is successfully added to Zoho CRM, we will get a response from the API with the details of the lead. We are logging this response to the console so that we can verify that the lead was added successfully.

That’s it! With these three steps, you can set up the Zoho API and add leads to Zoho CRM using Node.js and Axios.

Open to any questions on this, it took me a while to fully finish this, and would be willing to help anyone with this.

--

--

Emmanuel Unyime
Geek Culture

I’m a Software Engineer && Technical Writer, I've had the TypeScript epiphany!. Oh, I play Chess too!