Back

Quick Guide to Implementing Webhooks in lemlist

Aug 14, 20246 minute read

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

Introduction

Webhooks are like the cool kids of API integrations - they notify your app instantly when something happens in lemlist. No more constant polling or waiting around. It's time to make your app more responsive and efficient!

Prerequisites

Before we jump in, make sure you've got:

  • A lemlist API key (you can grab this from your account settings)
  • Node.js installed on your machine
  • Some experience with HTTP requests and Express.js

Got all that? Great! Let's roll.

Setting up the webhook endpoint

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

const express = require('express'); const app = express(); app.use(express.json()); app.post('/webhook', (req, res) => { console.log('Webhook received:', req.body); res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook server running on port 3000'));

Boom! You've got a basic webhook endpoint ready to rock.

Configuring webhooks in lemlist

Now, let's tell lemlist where to send those webhooks. We'll use the lemlist API to set this up:

const axios = require('axios'); const createWebhook = async () => { try { const response = await axios.post('https://api.lemlist.com/api/webhooks', { url: 'https://your-app.com/webhook', event: 'email_opened' }, { headers: { 'X-API-KEY': 'your-lemlist-api-key' } }); console.log('Webhook created:', response.data); } catch (error) { console.error('Error creating webhook:', error.response.data); } }; createWebhook();

Replace 'https://your-app.com/webhook' with your actual webhook URL and 'your-lemlist-api-key' with your API key. Run this once, and you're all set!

Handling webhook events

lemlist can send various events, like when an email is opened, clicked, or replied to. Let's handle an email open event:

app.post('/webhook', (req, res) => { const { event, data } = req.body; if (event === 'email_opened') { console.log(`Email opened by ${data.recipient} at ${data.openedAt}`); // Do something cool here, like updating your UI } res.sendStatus(200); });

Remember, always send a 200 response quickly, then process the event asynchronously if needed.

Security considerations

Don't trust just anyone knocking on your webhook door! Let's add some security:

const crypto = require('crypto'); const verifySignature = (req, res, next) => { const signature = req.headers['x-lemlist-signature']; const body = JSON.stringify(req.body); const expectedSignature = crypto .createHmac('sha256', 'your-webhook-secret') .update(body) .digest('hex'); if (signature === expectedSignature) { next(); } else { res.sendStatus(401); } }; app.post('/webhook', verifySignature, (req, res) => { // Your webhook handling code here });

Replace 'your-webhook-secret' with the secret provided by lemlist.

Testing and debugging

Want to test locally? Use ngrok to expose your local server to the internet. It's super handy for debugging!

ngrok http 3000

Then use the ngrok URL when setting up your webhook in lemlist.

Error handling and retries

lemlist will retry failed webhook deliveries, so make sure your endpoint can handle duplicate events. A good practice is to store processed event IDs and skip duplicates.

Best practices

  1. Process events asynchronously to keep your webhook endpoint snappy.
  2. Implement idempotency to handle potential duplicate events.
  3. Store raw webhook data before processing, just in case.

Conclusion

And there you have it! You're now a lemlist webhook wizard. Remember, webhooks are powerful tools, so use them wisely. They'll make your lemlist integration faster, more efficient, and way cooler.

Happy coding, and may your webhooks always deliver on time! 🚀