In modern applications, systems need to talk to each other in real-time. Sometimes you need to be able to send out a payment confirmation, order update, or data sync from an external service without having to wait for scheduled jobs or manual triggers.
That’s where webhooks come in.
Instead of repeatedly polling an external system for updates, webhooks enable external services to push data to your application instantly when an event occurs. In Odoo 19, it is common to consume webhooks when integrating with third-party services like payment gateways, CRMs, or logistics platforms.
What is a Webhook?
A webhook is merely an HTTP callback.
When something specific happens in an external system, it sends a POST request to a known URL on your application. This request will normally contain data on the event.
For example:
- Payment gateway > sends payment status
- Delivery service > sends shipment updates
- External app > sends new data
In Odoo, you create a controller endpoint to receive and process this data.
Why Use Webhooks Instead of Polling?
Without webhooks, you might write a cron job to poll for updates every few minutes.
The problem?
- Delays in data updates
- Unnecessary API calls
- Increased load
With webhooks:
- Data is received instantly
- No repeated requests
- More efficient and real-time
Creating a Webhook Endpoint in Odoo 19
To consume a webhook, you need to create an HTTP controller.
Basic Example
from odoo import http
from odoo.http import request
class WebhookController(http.Controller):
@http.route('/webhook/payment', type='json', auth='public', methods=['POST'], csrf=False)
def handle_payment_webhook(self, **kwargs):
data = request.jsonrequest
# Process incoming data
payment_id = data.get('payment_id')
status = data.get('status')
order = request.env['sale.order'].sudo().search([
('payment_id', '=', payment_id)
], limit=1)
if order:
order.write({'state': status})
return {"status": "success"}
This endpoint receives webhook data and updates the corresponding order.
Important: Disable CSRF for Webhooks
Webhooks come from external systems, so CSRF protection must be disabled:
csrf=False
Otherwise, Odoo will reject the request.
Handling Raw JSON Data
Sometimes webhooks send raw JSON payloads.
data = request.httprequest.data
Or:
data = request.jsonrequest
Always validate the data before processing.
Securing Your Webhook
One important thing many developers overlook is security.
Since your endpoint is public, anyone could potentially send requests.
Basic Validation Example
secret = request.httprequest.headers.get('X-Webhook-Secret')
if secret != "my_secret_key":
return {"error": "Unauthorized"}You can also:
- Validate signatures
- Restrict IP addresses
- Use tokens
Avoid Heavy Processing Inside Webhooks
A common mistake is doing too much work directly inside the webhook.
Problem:
def handle_webhook(self):
# heavy processing here
self.process_large_data()
If processing takes too long, the request may timeout.
Better Approach: Use Queue or Cron
def handle_webhook(self):
request.env['my.queue'].sudo().create({
'data': request.jsonrequest
})
If processing takes too long, the request may timeout.
Better Approach: Use Queue or Cron
def handle_webhook(self):
request.env['my.queue'].sudo().create({
'data': request.jsonrequest
})
Then process it later using a cron job or queue system.
Logging for Debugging
Webhook issues can be hard to debug because they come from external systems.
Always log incoming data:
import logging
_logger = logging.getLogger(__name__)
_logger.info("Webhook received: %s", request.jsonrequest)
This helps track failures and debug issues.
Common Use Cases in Odoo
Webhooks are widely used for:
- Payment confirmations
- Order status updates
- Inventory sync
- CRM lead creation
- Third-party integrations
Webhooks in Odoo 19 are a very powerful way to consume real-time integrations. With webhooks, your system can react to events in real time; there's no need to run scheduled jobs or poll an API.
But with power comes great responsibility. Webhook endpoints are public, so security and handling them right are crucial.
Properly implemented webhooks can help you make your system faster, more efficient, and better integrated with external services.
To read more about A Complete Overview of Webhooks in Odoo 19, refer to our blog A Complete Overview of Webhooks in Odoo 19.