Back

Quick Guide to Implementing Webhooks in Odoo ERP

Aug 18, 20246 minute read

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!

What's the Deal with Webhooks in Odoo?

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?

Before We Jump In

Make sure you've got:

  • Odoo ERP (we're using version 14.0 in this guide)
  • API access and authentication sorted
  • A Node.js environment for our webhook server

Setting Up Your Webhook Endpoint

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'));

Configuring Odoo for Webhooks

  1. Log into Odoo and enable developer mode (it's in the Settings menu).
  2. Head over to the Automation menu.
  3. Create a new Automated Action. This is where the magic happens!

Defining the Webhook Trigger

Choose what you want to trigger the webhook. Let's say we want to know when a new contact is created:

  1. Select the "Contacts" model (res.partner).
  2. Set the trigger to "On Creation".
  3. Now, for the server action, we'll use some Python. Don't worry, it's not too scary:
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()

Testing Your 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?).

Securing Your Webhook

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!

Handling Errors Like a Pro

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)}")

Best Practices

  • Rate Limiting: Be nice to your server. Implement rate limiting in your webhook sender.
  • Payload Size: Keep it slim. Only send what you need.
  • Versioning: Future-proof your webhook API. Include a version in your URL or payload.

Wrapping Up

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!

Want to Learn More?

Check out these resources:

Now go forth and webhook all the things! Happy coding! 🚀