Hey there, fellow JavaScript dev! Ready to supercharge your LearnWorlds integration with some webhook magic? 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 interesting happens in LearnWorlds. No more constant polling or refreshing. We're talking real-time, folks!
Before we start, make sure you've got:
Got all that? Great! Let's roll.
First things first, we need somewhere for LearnWorlds to send those juicy webhook events. Let's whip up a quick Express server:
const express = require('express'); const bodyParser = require('body-parser'); const crypto = require('crypto'); const app = express(); app.use(bodyParser.json()); const SECRET_KEY = 'your_learnworlds_webhook_secret'; app.post('/webhook', (req, res) => { const signature = req.headers['x-lw-signature']; const payload = JSON.stringify(req.body); const hmac = crypto.createHmac('sha256', SECRET_KEY); const digest = hmac.update(payload).digest('hex'); if (signature === digest) { console.log('Webhook received:', req.body); res.sendStatus(200); } else { console.error('Invalid signature'); res.sendStatus(403); } }); app.listen(3000, () => console.log('Webhook server running on port 3000'));
This little snippet sets up a secure endpoint at /webhook
. It validates the incoming webhook using the signature provided by LearnWorlds. Safety first, right?
Now, hop over to your LearnWorlds dashboard and find the webhook configuration section. It's usually hiding in the API or integrations area.
You'll see a list of events you can subscribe to. For user-facing stuff, you might want to focus on events like:
user.registered
course.completed
certificate.issued
When you set up a webhook, LearnWorlds will send you payloads that look something like this:
{ "event": "course.completed", "user_id": "123456", "course_id": "789012", "completion_date": "2023-06-15T14:30:00Z" }
Pretty neat, huh?
Now for the fun part - handling those events! Let's add some logic to our webhook endpoint:
app.post('/webhook', (req, res) => { // ... (previous validation code) const { event, user_id, course_id, completion_date } = req.body; switch (event) { case 'course.completed': handleCourseCompletion(user_id, course_id, completion_date); break; // Add more cases as needed default: console.log(`Unhandled event type: ${event}`); } res.sendStatus(200); }); function handleCourseCompletion(userId, courseId, completionDate) { console.log(`User ${userId} completed course ${courseId} on ${completionDate}`); // Add your business logic here (e.g., update database, send congratulations email) }
This switch statement lets you handle different event types with ease. Sky's the limit on what you can do here!
Want to test locally? ngrok is your new best friend. It creates a secure tunnel to your local server:
ngrok http 3000
Use the ngrok URL as your webhook URL in LearnWorlds, and you're good to go!
Don't forget to check the webhook logs in your LearnWorlds dashboard. They're a goldmine for debugging.
A few pro tips to keep in mind:
And there you have it! You're now ready to create some awesome, real-time integrations with LearnWorlds. Remember, webhooks are powerful tools - use them wisely, and they'll take your app to the next level.
Happy coding, and may your integrations be ever responsive!