Last active
June 10, 2024 00:12
-
-
Save levchenkod/e6dda91bb3e8b875eab2b4c90a6b4b4c to your computer and use it in GitHub Desktop.
Zoho API create Lead example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Based on this article | |
* https://medium.com/geekculture/using-zoho-api-for-leads-in-node-js-ea204e98d41b | |
*/ | |
/** | |
* IMPORTANT: pay attention to the super-domain ("com", "eu", etc). | |
* Must be the same you used for the client registration. | |
* Otherwise will cause: `{ "error": "invalid_client" }` | |
*/ | |
const OAuthEndpoint = 'https://accounts.zoho.eu/oauth/v2'; | |
const APIEndpoint = 'https://www.zohoapis.eu/crm/v2'; | |
/** | |
* Doesn't really needed, by required by API | |
*/ | |
const RedirectURL = 'http://localhost:3000'; | |
const contentType = { | |
json: 'application/json' | |
}; | |
const method = { | |
post: 'POST' | |
}; | |
/** | |
* Step 1 of 2 - get fresh `access_token` using generated on init `ZOHO_REFRESH_TOKEN` | |
*/ | |
const getAccessToken = async ():Promise<string> => { | |
const params: any = { | |
grant_type: 'refresh_token', | |
redirect_uri: RedirectURL, | |
refresh_token: process.env.ZOHO_REFRESH_TOKEN, | |
client_id: process.env.ZOHO_CLIENT_ID, | |
client_secret: process.env.ZOHO_SECRET, | |
}; | |
const URLParams = new URLSearchParams(); | |
for (const key in params) { | |
if(params.hasOwnProperty(key)){ | |
URLParams.append(key, params[key]); | |
} | |
} | |
const requestURL = `${OAuthEndpoint}/token?${URLParams.toString()}`; | |
const options = { | |
method: method.post, | |
headers: { 'Content-Type': contentType.json }, | |
}; | |
const responseRAW = await fetch(requestURL, options) | |
const response = await responseRAW.json(); | |
const result = response?.access_token; | |
return String(result); | |
}; | |
/** | |
* Step 2 of 2 - use `access_token` to create a new Lead | |
*/ | |
const addUserToLeads = async ({ | |
email, | |
company, | |
firstName, | |
lastName, | |
}: { | |
email: string, | |
company: string, | |
firstName: string, | |
lastName: string, | |
}) => { | |
try { | |
const accessToken = await getAccessToken(); | |
const requestURL = `${APIEndpoint}/Leads`; | |
const body = JSON.stringify({ | |
data: [{ | |
Email: email, | |
Company: company, | |
Last_Name: lastName, | |
First_Name: firstName, | |
}], | |
trigger: [ | |
'approval', | |
'workflow', | |
'blueprint', | |
], | |
}); | |
const options = { | |
method: method.post, | |
body, | |
headers: { | |
"Content-Type": contentType.json, | |
Authorization: `Zoho-oauthtoken ${accessToken}` | |
}, | |
}; | |
const responseRaw = await fetch(requestURL, options); | |
const response = await responseRaw.json(); | |
console.log({ response }); | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
export default addUserToLeads; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment