Hey there, fellow JavaScript dev! Ready to supercharge your HubSpot integration with webhooks? Let's dive right in and get those real-time updates flowing.
Webhooks are like the cool kids of the API world – they notify your app instantly when something interesting happens in HubSpot. No more constant polling or wasted API calls. We'll be using HubSpot's API to set these up, so buckle up!
Before we start, make sure you've got:
First things first, let's get our project set up:
mkdir hubspot-webhook-project cd hubspot-webhook-project npm init -y npm install axios express
Now, let's create a basic Express server:
const express = require('express'); const app = express(); app.use(express.json()); const PORT = 3000; app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
To talk to HubSpot, we need to get authenticated. Head over to your HubSpot developer account and grab your API key. If you're building a public app, you'll need to set up OAuth 2.0, but we'll keep it simple for now.
const axios = require('axios'); const API_KEY = 'your-api-key-here'; const baseURL = 'https://api.hubapi.com'; const hubspot = axios.create({ baseURL, headers: { 'Authorization': `Bearer ${API_KEY}` } });
Now for the fun part – let's create a webhook subscription:
async function createWebhookSubscription() { try { const response = await hubspot.post('/webhooks/v3/app/subscriptions', { eventType: 'contact.propertyChange', propertyName: 'email', active: true }); console.log('Webhook created:', response.data); } catch (error) { console.error('Error creating webhook:', error.response.data); } } createWebhookSubscription();
HubSpot offers a bunch of webhook events. Some popular ones include:
contact.creation
deal.propertyChange
company.deletion
Make sure you're only subscribing to events you need – it'll keep things snappy and efficient.
Time to set up an endpoint to catch those sweet, sweet webhook payloads:
app.post('/webhook', (req, res) => { const { eventType, objectId, propertyName, propertyValue } = req.body; console.log(`Received ${eventType} for object ${objectId}`); console.log(`${propertyName} changed to ${propertyValue}`); res.sendStatus(200); });
Security first! Let's make sure these requests are legit:
const crypto = require('crypto'); function verifyWebhookSignature(req, res, next) { const signature = req.headers['x-hubspot-signature']; const requestBody = JSON.stringify(req.body); const hash = crypto.createHash('sha256') .update(`${process.env.CLIENT_SECRET}${requestBody}`) .digest('hex'); if (hash !== signature) { return res.status(401).send('Invalid signature'); } next(); } app.use('/webhook', verifyWebhookSignature);
HubSpot's got some great developer tools for testing. Fire up your endpoint, head to the HubSpot developer portal, and send a test ping. Not working? Double-check your URL and make sure your server's publicly accessible (tools like ngrok are great for this).
And there you have it! You're now a HubSpot webhook wizard. Remember, this is just the beginning – there's so much more you can do with webhooks. Keep experimenting, and happy coding!
If you're feeling adventurous, look into:
Now go forth and build some awesome integrations!