The integration of Odoo with external platforms and third-party apps often requires sending some kind of notification based on the trigger of a particular event. With Odoo 20, there is no need to create a custom HTTP controller or develop API connectors. The automation module takes care of sending and receiving webhooks based on automation rules and server actions.
In this blog, we will explain how to use outbound and inbound webhooks in Odoo 20 through XML and Python files.
Module Structure
Our custom module name will be sale_webhook_automation
sale_webhook_automation/
+-- __init__.py
+-- __manifest__.py
+-- data/
¦ +-- outbound_webhook_data.xml
¦ +-- inbound_webhook_data.xml
+-- models/
+-- __init__.py
+-- sale_order.py
We need to add base_automation and sale to the dependencies so that Odoo automatically installs these modules with our custom module installation.
# __manifest__.py
{
'name': 'Sale Webhook Automation',
'version': '20.0.1.0.0',
'category': 'Sales',
'summary': 'Send and receive webhooks using automation rules in Odoo 20',
'depends': ['base_automation', 'sale'],
'data': [
'data/outbound_webhook_data.xml',
'data/inbound_webhook_data.xml',
],
'license': 'LGPL-3',
'installable': True,
'application': False,
}
Sending Webhooks (Outbound)
For sending a webhook, we have to set the state of the server action to state="webhook" and connect it with an automation rule. Once the automation rule is triggered, Odoo will serialize the specified fields into JSON and send an HTTP POST request to the external URL.
Create data/outbound_webhook_data.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<!-- Server Action: Sends the Webhook -->
<record id="server_action_send_order_webhook" model="ir.actions.server">
<field name="name">Send Sales Order Webhook</field>
<field name="model_id" ref="sale.model_sale_order"/>
<field name="state">webhook</field>
<field name="usage">base_automation</field>
<field name="webhook_url">https://api.external-service.com/orders</field>
<field name="webhook_field_ids" eval="[
Command.set([
ref('sale.field_sale_order__name'),
ref('sale.field_sale_order__partner_id'),
ref('sale.field_sale_order__amount_total'),
ref('sale.field_sale_order__client_order_ref'),
])
]"/>
</record>
<!-- Automation Rule: Triggers when an order is confirmed -->
<record id="automation_rule_send_order_webhook" model="base.automation">
<field name="name">Send Webhook on Order Confirmation</field>
<field name="model_id" ref="sale.model_sale_order"/>
<field name="trigger">on_create_or_write</field>
<field name="trigger_field_ids" eval="[
Command.set([ref('sale.field_sale_order__state')])
]"/>
<field name="filter_pre_domain">[('state', '!=', 'sale')]</field>
<field name="filter_domain">[('state', '=', 'sale')]</field>
<field name="action_server_ids" eval="[
Command.link(ref('server_action_send_order_webhook'))
]"/>
</record>
</data>
</odoo>
How It Works:
- state="webhook": This specifies to Odoo that the server action will send an HTTP request.
- webhook_url: This is the target URL to which the JSON payload will be sent through the HTTP POST method.
- webhook_field_ids: This is the list of fields that will be part of the payload. Odoo will add _id, _model, and _action to the JSON payload along with these fields.
- Post-Commit Execution: Outbound webhooks in Odoo are executed post-commit execution. In case of a failure in the transaction, the webhook call will not be made.
Receiving Webhooks (Inbound)
When receiving webhooks, the trigger of the automation rule is set to “on_webhook”. Odoo creates an endpoint at /web/hook/, and whenever an external system posts its data to it, Odoo checks the Python expression known as record_getter and finds the corresponding record after that. Then it runs a server action with our Python logic inside.
Implementation of the Python Method
sale.order is extended to use the data that was sent to the webhook endpoint (e.g., logging tracking info into the chatter)
# models/sale_order.py
from odoo import models, _
class SaleOrder(models.Model):
_inherit = 'sale.order'
def action_update_tracking_from_webhook(self, tracking_ref, carrier=None):
"""Update chatter with tracking details received via webhook."""
for order in self:
carrier_name = carrier or _("Carrier")
order.message_post(
body=_("Shipment update from <b>%(carrier)s</b>. Tracking Number: <code>%(tracking)s</code>",
carrier=carrier_name,
tracking=tracking_ref)
)
Defining the Inbound Rule in XML
Create data/inbound_webhook_data.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<!-- Server Action: Processes the Incoming Payload -->
<record id="server_action_process_tracking_webhook" model="ir.actions.server">
<field name="name">Process Inbound Tracking Webhook</field>
<field name="model_id" ref="sale.model_sale_order"/>
<field name="state">code</field>
<field name="usage">base_automation</field>
<field name="code"><![CDATA[
# 'records' is the sale.order returned by the record_getter
# 'payload' is the dictionary containing the incoming request data
tracking_ref = payload.get('tracking_number')
carrier = payload.get('carrier')
if tracking_ref and records:
records.action_update_tracking_from_webhook(tracking_ref, carrier)
]]></field>
</record>
<!-- Automation Rule: Inbound Webhook Listener -->
<record id="automation_rule_receive_tracking_webhook" model="base.automation">
<field name="name">Receive Tracking Webhook</field>
<field name="model_id" ref="sale.model_sale_order"/>
<field name="trigger">on_webhook</field>
<field name="log_webhook_calls" eval="True"/>
<field name="record_getter"><![CDATA[
model.search([('name', '=', payload.get('order_ref'))], limit=1)
]]></field>
<field name="action_server_ids" eval="[
Command.link(ref('server_action_process_tracking_webhook'))
]"/>
</record>
</data>
</odoo>
How it Works:
- trigger=“on_webhook”: It creates a public endpoint /web/hook/. It can accept either JSON bodies or URL query parameters as input data. There is no CSRF validation, and no user login is required to access that endpoint.
- record_getter: It is a Python expression that gets the corresponding record based on input data (payload). E.g.: model.search([('name', '=', payload.get('order_ref'))], limit=1) searches for a sales order which matches the order_ref field in the payload.
- payload: In the server action’s Python logic, payload is a dictionary that is sent by the external system. Also, records include the found record by record_getter.
- log_webhook_calls="True": Logs incoming payloads and execution errors to Settings > Technical > Database Structure > Logging (accessible in Developer Mode). This is especially helpful during setup to verify the structure of payloads received from external systems.
For outbound webhooks in Odoo 20, you need to use state="webhook" in a server action.
Outbound webhooks will not send record details to URLs until transactions are securely committed.
Inbound webhooks can execute when trigger="on_webhook" is triggered in an automation rule. Inbound webhooks form an endpoint to search for corresponding records using record_getter and then execute Python code with the help of the payload in the request.
Both inbound and outbound webhooks have been declared in XML and Python code without requiring custom web controllers.
To read more about A Complete Overview of Webhooks in Odoo 19, refer to our blog A Complete Overview of Webhooks in Odoo 19.