Created
December 15, 2017 19:03
-
-
Save rhlsthrm/c431052993b609687fdb9e688f9eb2fd to your computer and use it in GitHub Desktop.
Node Express API endpoint code to insert records into Zoho CRM. Use library to generate XML and properly encode.
This file contains 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
const axios = require('axios') | |
const xml = require('xml') | |
const { asyncRequest } = require('../util') | |
const { getModels } = require('../models') | |
const querystring = require('querystring') | |
const handler = async (req, res) => { | |
const { MailingList } = getModels() | |
const { email } = req.body | |
const exists = await MailingList.findOne({ where: { email } }) | |
if (exists) { | |
return res.status(400).json({ error: 'Email address already registered.' }) | |
} | |
try { | |
// add to Zoho CRM | |
const { ZOHO_AUTH_TOKEN } = process.env | |
if (!ZOHO_AUTH_TOKEN) { | |
return res.status(400).json({ error: 'No valid Zoho CRM configuration.' }) | |
} | |
const xmlData = xml({ | |
Leads: [ | |
{ | |
row: [ | |
{ _attr: { no: '1' } }, | |
{ FL: [{ _attr: { val: 'Lead Source' } }, 'Landing Page Sign Up'] }, | |
{ FL: [{ _attr: { val: 'First Name' } }, 'Unknown'] }, | |
{ FL: [{ _attr: { val: 'Last Name' } }, 'Unknown'] }, | |
{ FL: [{ _attr: { val: 'Lead Owner' } }, 'Bob Smith'] }, | |
{ FL: [{ _attr: { val: 'Email' } }, email] } | |
] | |
} | |
] | |
}) | |
const qs = querystring.stringify({ | |
authtoken: ZOHO_AUTH_TOKEN, | |
newFormat: 1, | |
scope: 'crmapi', | |
duplicateCheck: 1, | |
xmlData: xmlData | |
}) | |
const result = await axios.post( | |
`https://crm.zoho.com/crm/private/xml/Leads/insertRecords?${qs}` | |
) | |
const subscriber = await MailingList.build({ | |
}).save() | |
return res.status(200).json({ id: subscriber.id }) | |
} catch (e) { | |
return res.status(400).json({ error: e.toString() }) | |
} | |
} | |
module.exports = asyncRequest.bind(null, handler) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment