Hey there, JavaScript wizards! Ready to level up your SignRequest integration? Let's dive into the world of webhooks and see how they can supercharge your app's responsiveness. Buckle up!
Webhooks are like your app's personal news feed from SignRequest. Instead of constantly asking "Hey, any updates?", webhooks let SignRequest tap you on the shoulder when something interesting happens. Cool, right?
Before we jump in, make sure you've got:
First things first, let's tell SignRequest where to send those updates:
Time to create our listener! Here's a quick Express.js server to get you started:
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'));
SignRequest will send you a JSON payload. Let's take a look at how to handle it:
app.post('/webhook', (req, res) => { const { event_type, event_hash, event_time, event_payload } = req.body; // Do something awesome with the data console.log(`Received ${event_type} event at ${event_time}`); res.sendStatus(200); });
Trust, but verify! Here's how to make sure the webhook is really from SignRequest:
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-signrequest-signature']; if (!verifySignature(req.body, signature, 'your_webhook_secret')) { return res.sendStatus(401); } // Process the verified webhook // ... });
Different events might need different handling. Let's set that up:
app.post('/webhook', (req, res) => { const { event_type, event_payload } = req.body; switch(event_type) { case 'document_signed': handleSignedDocument(event_payload); break; case 'document_declined': handleDeclinedDocument(event_payload); break; // Add more cases as needed } res.sendStatus(200); }); function handleSignedDocument(payload) { console.log('Document signed:', payload.document.uuid); // Update your database, notify users, etc. } function handleDeclinedDocument(payload) { console.log('Document declined:', payload.document.uuid); // Handle the decline, maybe notify the document owner }
Sometimes things go wrong. Let's be prepared:
app.post('/webhook', async (req, res) => { try { await processWebhook(req.body); res.sendStatus(200); } catch (error) { console.error('Webhook processing failed:', error); res.sendStatus(500); } }); async function processWebhook(data, retries = 3) { try { // Your webhook processing logic here } catch (error) { if (retries > 0) { console.log(`Retrying... (${retries} attempts left)`); await new Promise(resolve => setTimeout(resolve, 1000)); return processWebhook(data, retries - 1); } throw error; } }
SignRequest provides a handy testing tool in the dashboard. Use it! Also, don't forget to log incoming webhooks during development:
app.post('/webhook', (req, res) => { console.log('Received webhook:', JSON.stringify(req.body, null, 2)); // Your processing logic here res.sendStatus(200); });
And there you have it! You're now equipped to handle SignRequest webhooks like a pro. Remember, webhooks are powerful tools that can make your app more responsive and efficient. Don't be afraid to experiment and expand on what we've covered here.
Happy coding, and may your documents always be signed on time! 🚀📄✍️