Hey there, fellow Javascript devs! Ready to supercharge your Paperform integrations with webhooks? Let's dive right in and get those real-time updates flowing.
Before we start, make sure you've got:
Got all that? Great! Let's code.
First things first, we need somewhere to receive those webhook payloads. 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'));
Simple, right? This server listens for POST requests on /webhook
and logs the payload. In a real-world scenario, you'd process this data instead of just logging it.
Now, let's tell Paperform where to send those sweet, sweet webhooks. We'll use the Paperform API to set this up:
const axios = require('axios'); const createWebhook = async () => { try { const response = await axios.post('https://api.paperform.co/v1/webhooks', { url: 'https://your-server.com/webhook', events: ['form.submission.created'] }, { headers: { 'Authorization': 'Bearer YOUR_API_KEY' } }); console.log('Webhook created:', response.data); } catch (error) { console.error('Error creating webhook:', error); } }; createWebhook();
Don't forget to replace YOUR_API_KEY
with your actual Paperform API key. This script creates a webhook that'll fire every time a form submission is created.
When Paperform sends a webhook, you'll get a juicy payload full of data. Here's how you might handle it:
app.post('/webhook', (req, res) => { const { event, data } = req.body; switch(event) { case 'form.submission.created': console.log('New submission:', data.submission); // Process the new submission break; // Handle other event types default: console.log('Unhandled event type:', event); } res.sendStatus(200); });
This code checks the event type and processes the data accordingly. Remember, always send a 200 response quickly to acknowledge receipt!
Security is crucial, folks. Paperform signs its webhook payloads, so let's verify those signatures:
const crypto = require('crypto'); const verifySignature = (payload, signature, secret) => { const hmac = crypto.createHmac('sha256', secret); const digest = hmac.update(JSON.stringify(payload)).digest('hex'); return signature === digest; }; app.post('/webhook', (req, res) => { const signature = req.headers['x-paperform-signature']; if (!verifySignature(req.body, signature, 'YOUR_WEBHOOK_SECRET')) { return res.status(401).send('Invalid signature'); } // Process the webhook payload res.sendStatus(200); });
Replace 'YOUR_WEBHOOK_SECRET'
with the secret Paperform provides when you create the webhook.
Paperform offers a handy webhook testing tool in their dashboard. Use it to send test payloads and make sure everything's working smoothly. If you're not getting the payloads, double-check your server's accessibility and the webhook URL you provided.
And there you have it! You're now ready to start receiving real-time updates from Paperform. Remember, webhooks are powerful tools, so use them wisely and keep your code clean.
Want to dive deeper? Check out Paperform's API docs for more advanced webhook features like filtering and managing multiple subscriptions.
Now go forth and webhook all the things! Happy coding! 🚀