Back

Quick Guide to Implementing Webhooks in Intercom

Aug 11, 20247 minute read

Hey there, fellow JavaScript dev! Ready to supercharge your Intercom integration with webhooks? Let's dive right in and get those real-time updates flowing.

Introduction

Webhooks are like the cool kids of the API world – they notify you instantly when something interesting happens in Intercom. No more constant polling or missed updates. For user-facing integrations, they're an absolute game-changer. Let's get you set up!

Prerequisites

Before we start, make sure you've got:

  • An Intercom account with API access (you're probably already sorted here)
  • Node.js installed on your machine
  • A basic grasp of Express.js (we'll use it for our webhook endpoint)

Got all that? Great! Let's code.

Setting Up Webhook Endpoint

First things first, we need somewhere for Intercom to send those juicy updates. Let's whip up a quick Express server:

const express = require('express'); const bodyParser = require('body-parser'); const crypto = require('crypto'); const app = express(); app.use(bodyParser.json()); const WEBHOOK_SECRET = 'your_webhook_secret'; app.post('/webhook', (req, res) => { const signature = req.headers['x-hub-signature']; const computedSignature = 'sha1=' + crypto.createHmac('sha1', WEBHOOK_SECRET) .update(JSON.stringify(req.body)) .digest('hex'); if (signature === computedSignature) { // Process the webhook console.log('Webhook received:', req.body); res.sendStatus(200); } else { res.sendStatus(401); } }); app.listen(3000, () => console.log('Webhook server running on port 3000'));

This sets up a basic server that listens for POST requests on /webhook. It also includes a simple verification step to ensure the webhook is legit.

Configuring Webhooks in Intercom

Now, let's tell Intercom where to send these webhooks:

  1. Head over to the Intercom Developer Hub
  2. Create a new app if you haven't already
  3. Navigate to the Webhooks section
  4. Time to create that webhook! Here's how you can do it programmatically:
const axios = require('axios'); axios.post('https://api.intercom.io/subscriptions', { service_type: 'web', url: 'https://your-server.com/webhook', topics: ['user.created', 'user.updated'] }, { headers: { 'Authorization': 'Bearer YOUR_ACCESS_TOKEN', 'Accept': 'application/json' } }) .then(response => console.log('Webhook created:', response.data)) .catch(error => console.error('Error:', error));

Replace 'YOUR_ACCESS_TOKEN' with your actual Intercom access token, and update the url to point to your webhook endpoint.

Handling Webhook Events

Now for the fun part – actually doing something with these events! Here's a simple example:

app.post('/webhook', (req, res) => { // ... verification code from earlier ... const { topic, data } = req.body; switch (topic) { case 'user.created': console.log('New user created:', data.item.email); // Maybe add them to your mailing list? break; case 'user.updated': console.log('User updated:', data.item.email); // Update your local user database break; // Add more cases as needed } res.sendStatus(200); });

Error Handling and Reliability

Things don't always go smoothly, so let's add some retry logic:

const processWebhook = async (data) => { // Your webhook processing logic here }; app.post('/webhook', async (req, res) => { // ... verification code ... let retries = 3; while (retries > 0) { try { await processWebhook(req.body); break; } catch (error) { console.error('Error processing webhook:', error); retries--; if (retries === 0) { // Store failed webhook for later processing await storeFailedWebhook(req.body); } } } res.sendStatus(200); });

Testing and Debugging

Intercom provides a handy webhook tester in their Developer Hub. Use it! It's a lifesaver for debugging. Also, log everything during development:

app.post('/webhook', (req, res) => { console.log('Received webhook:', JSON.stringify(req.body, null, 2)); // ... rest of your code ... });

Best Practices

  1. Always use HTTPS for your webhook endpoint.
  2. Consider IP whitelisting for added security.
  3. Process webhooks asynchronously if they're computationally expensive.
  4. Keep your webhook processor idempotent – it should handle duplicate events gracefully.

Conclusion

And there you have it! You're now equipped to handle real-time Intercom events like a pro. Remember, webhooks are powerful tools, so use them wisely. Happy coding, and may your integrations be ever smooth and your users ever happy!

Need more? Check out Intercom's official API docs for the nitty-gritty details. Now go forth and webhook all the things! 🚀