Hey there, fellow JavaScript dev! Ready to dive into the world of Odoo ERP webhooks? Buckle up, because we're about to turbocharge your integration game. This guide will walk you through setting up webhooks in Odoo ERP, focusing on user-facing integrations. Let's get our hands dirty with some code!
Webhooks are like the cool kids of the API world – they notify your app in real-time when something interesting happens in Odoo. No more constant polling or refreshing. Neat, right?
Make sure you've got:
First things first, let's create 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'));
Choose what you want to trigger the webhook. Let's say we want to know when a new contact is created:
import requests import json def send_webhook(record): url = "http://your-server.com/webhook" payload = { "id": record.id, "name": record.name, "email": record.email } headers = {'Content-Type': 'application/json'} response = requests.post(url, data=json.dumps(payload), headers=headers) if response.status_code != 200: raise Exception("Webhook failed!") model.send_webhook()
Create a new contact in Odoo and watch your Node.js console light up with the webhook data. If it doesn't work right away, don't sweat it. Debugging is half the fun (right?).
Security first! Add some authentication to your webhook endpoint:
app.post('/webhook', (req, res) => { const secret = process.env.WEBHOOK_SECRET; if (req.headers['x-webhook-secret'] !== secret) { return res.sendStatus(403); } // Process webhook... });
Don't forget to set that secret in Odoo too!
Things go wrong. It's a fact of life. Let's add some retry logic:
def send_webhook(record, retry=3): # ... previous code ... try: response = requests.post(url, data=json.dumps(payload), headers=headers) response.raise_for_status() except requests.exceptions.RequestException as e: if retry > 0: time.sleep(2) # Wait 2 seconds before retrying send_webhook(record, retry - 1) else: raise Exception(f"Webhook failed after 3 attempts: {str(e)}")
And there you have it! You've just implemented webhooks in Odoo ERP. Pretty cool, huh? With this setup, your JavaScript app will always be in the loop about what's happening in Odoo.
Remember, this is just the beginning. Feel free to customize and expand on this foundation. The sky's the limit!
Check out these resources:
Now go forth and webhook all the things! Happy coding! 🚀