Hey there, fellow Javascript dev! Ready to supercharge your 123FormBuilder integration with webhooks? Let's dive right in and get those real-time updates flowing.
Webhooks are like the cool kids of API integrations - they notify your app instantly when something happens in 123FormBuilder. No more constant polling or waiting around. It's time to make your app more responsive and efficient!
Before we jump into the code, make sure you've got:
First things first, let's set up a simple Express server to catch those webhooks:
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'));
Now, let's tell 123FormBuilder where to send those sweet, sweet webhooks:
const axios = require('axios'); axios.post('https://api.123formbuilder.com/v2/webhooks', { form_id: 'YOUR_FORM_ID', endpoint_url: 'https://your-server.com/webhook', handshake_key: 'YOUR_HANDSHAKE_KEY' }, { headers: { 'Authorization': 'Bearer YOUR_API_KEY' } }) .then(response => console.log('Webhook registered successfully')) .catch(error => console.error('Error registering webhook:', error));
123FormBuilder sends you a JSON payload that's packed with goodies. It'll include form details, submission data, and more.
Let's make sure we're getting the good stuff:
app.post('/webhook', (req, res) => { const payload = req.body; if (!payload || !payload.form_id) { return res.status(400).send('Invalid payload'); } // Process the payload console.log('Form submission received for form:', payload.form_id); res.sendStatus(200); });
Time to do something cool with that data:
function processSubmission(payload) { const { form_id, submission_id, form_data } = payload; // Do something awesome with the data console.log(`New submission ${submission_id} for form ${form_id}`); console.log('Form data:', form_data); // Maybe send an email, update a database, trigger a notification... }
Trust, but verify. Let's make sure these webhooks are legit:
const crypto = require('crypto'); function verifySignature(payload, signature, secret) { const hmac = crypto.createHmac('sha256', secret); const digest = hmac.update(JSON.stringify(payload)).digest('hex'); return digest === signature; } app.post('/webhook', (req, res) => { const signature = req.headers['x-123formbuilder-signature']; if (!verifySignature(req.body, signature, 'YOUR_WEBHOOK_SECRET')) { return res.status(401).send('Invalid signature'); } // Process the verified webhook });
Always use HTTPS for your webhook endpoint. It's not the 90s anymore, folks!
123FormBuilder will retry failed webhook deliveries, but it's up to you to handle them gracefully. Make sure your endpoint can handle duplicate notifications, and consider implementing a queuing system for processing.
Head over to the 123FormBuilder dashboard and hit that "Test" button. It's like a fire drill for your webhook handler!
console.log
liberally (we won't judge)You can set up multiple webhooks for different events. Maybe you only want to know about form submissions, not form updates?
Consider using a routing system if you're dealing with multiple forms:
app.post('/webhook/:formId', (req, res) => { const { formId } = req.params; // Handle webhook based on formId });
And there you have it! You're now a 123FormBuilder webhook wizard. Remember, with great power comes great responsibility - use these webhooks wisely and watch your integration come to life with real-time goodness.
If things aren't working:
Now go forth and webhook all the things! Happy coding! ๐