Hey there, fellow Javascript devs! Ready to supercharge your Process Street integration with webhooks? Let's dive right in and get those real-time updates flowing.
Webhooks are the secret sauce for keeping your app in sync with Process Street. They're like little messengers that ping your app whenever something interesting happens. And with the Process Street API, setting them up is a breeze.
Before we start cooking, make sure you've got these ingredients:
First things first, let's whip 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'));
Easy peasy, right? This little server is now ready to receive webhooks on the /webhook
endpoint.
Process Street isn't just going to trust any old request. They sign their webhooks, and it's up to us to verify that signature. Here's how:
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 signature === digest; } app.post('/webhook', (req, res) => { const signature = req.headers['x-ps-signature']; if (!verifySignature(req.body, signature, process.env.WEBHOOK_SECRET)) { return res.sendStatus(401); } // Process the webhook... });
Time to tell Process Street where to send those sweet, sweet webhooks:
const axios = require('axios'); async function registerWebhook() { try { const response = await axios.post('https://api.process.st/api/v1/webhooks', { url: 'https://your-app.com/webhook', events: ['checklist.completed', 'task.completed'], active: true }, { headers: { 'Authorization': `Bearer ${process.env.PROCESS_ST_API_KEY}` } }); console.log('Webhook registered:', response.data); } catch (error) { console.error('Failed to register webhook:', error.response.data); } } registerWebhook();
Now that we're getting webhooks, let's do something useful with them:
app.post('/webhook', (req, res) => { const { event, payload } = req.body; switch (event) { case 'checklist.completed': handleChecklistCompleted(payload); break; case 'task.completed': handleTaskCompleted(payload); break; default: console.log('Unhandled event:', event); } res.sendStatus(200); }); function handleChecklistCompleted(payload) { // Do something awesome } function handleTaskCompleted(payload) { // Make magic happen }
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('Error processing webhook:', error); res.sendStatus(500); } }); async function processWebhook(data, retries = 3) { try { // Process the webhook data } 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; } }
Process Street has a nifty feature for testing webhooks. Use it! And don't forget to log everything during development:
app.post('/webhook', (req, res) => { console.log('Received webhook:', JSON.stringify(req.body, null, 2)); // Process the webhook... });
Remember:
And there you have it! You're now a Process Street webhook wizard. Remember, with great power comes great responsibility. Use your newfound skills wisely, and may your integrations be ever real-time and your payloads always valid.
Happy coding, and don't forget to high-five your rubber duck for me!