Connecting Node.js application with HubSpot forms

Hey everyone! I’m having trouble hooking up my Node.js app to HubSpot forms. I’ve tried it on my local machine and on Heroku, but no luck. Here’s a snippet of what I’m working with:

app.post('/reach-out', (req, res) => {
    const axios = require('axios');
    const qs = require('querystring');

    const HUBSPOT_PORTAL = 'MY_PORTAL_ID';
    const FORM_ID = 'MY_FORM_ID';

    const formData = qs.stringify({
        'email': '[email protected]',
        'name': 'John Doe',
        'telephone': '1234567890',
        'comments': 'Testing HubSpot integration',
        'hs_context': JSON.stringify({
            'hutk': '',
            'ip': req.ip,
            'page_url': '/reach-out',
            'page_name': 'Contact Us'
        })
    });

    axios.post(`https://forms.hubspot.com/uploads/form/v2/${HUBSPOT_PORTAL}/${FORM_ID}`, formData)
        .then(response => {
            console.log('Success:', response.data);
            res.json({ 'status': 'Form submitted successfully' });
        })
        .catch(error => {
            console.error('Error:', error);
            res.status(500).json({ 'status': 'Form submission failed' });
        });
});

The weird thing is, I tried a similar setup in PHP and it worked fine. But with Node.js, I keep getting this error:

Error: read ECONNRESET

Any ideas what might be causing this? I’m pretty stumped!

I’ve dealt with similar HubSpot integration issues before. One thing that often gets overlooked is the ‘hutk’ parameter in the hs_context. This cookie is crucial for tracking and should be passed from the client-side. Try modifying your code to capture it:

const hutk = req.cookies['hubspotutk'] || '';

// Then in your hs_context:
'hs_context': JSON.stringify({
    'hutk': hutk,
    // ... other params
})

Also, make sure you’re using the latest HubSpot API version. They occasionally deprecate older ones, which can cause unexpected errors. If you’re still having trouble, try logging the entire response object from HubSpot, not just the data. Sometimes there’s valuable info in the headers or status code that can point you in the right direction.

hey there! ive run into this before. the ECONNRESET error usually means the connection was closed unexpectedly. have u tried adding a timeout to ur axios request? something like:

axios.post(url, formData, { timeout: 10000 })

this might help if hubspot is taking too long to respond. also double check ur PORTAL_ID and FORM_ID are correct. hope this helps!

I encountered a similar issue when integrating HubSpot forms with Node.js. The ECONNRESET error often stems from network instability or firewall interference. Have you considered implementing a retry mechanism? You could use a library like ‘axios-retry’ to automatically retry failed requests. Additionally, ensure your Node.js version is up-to-date, as older versions have known issues with certain HTTPS connections. Lastly, verify that your server’s outbound connections to HubSpot’s API endpoints aren’t being blocked by any security measures. These steps helped resolve the issue in my case.