APIs and Webhooks are tools for sharing and syncing data between software systems, but they work differently. APIs act as an interface for two-way communication, relying on a request-and-response process. On the other hand, Webhooks are triggered by specific events in a system and provide real-time updates or actions without needing a request. They are especially useful in Odoo for automating tasks and receiving updates from other platforms instantly.
This blog post will walk through the procedures needed to create a Webhook-based real-time integration between Odoo and an external system.
To set up a webhook, we need a URL that external software systems can send data to when a specific event occurs. This URL, along with the triggering event, is shared with the external system. When the event happens, the system sends data to the URL. In Odoo, this URL should point to a controller that can process the received data.
Once an event happens in the external system, it sends a response to an Odoo controller via a specific URL. The data, usually in JSON format, is received and processed in the controller. From there, we can parse the data and perform various actions based on the information provided.
Let's consider an example. I'm using Shopify, an eCommerce platform, as an external system. It's important that these systems support webhooks. Since Shopify offers webhooks, we can integrate it with Odoo in real time.
In the image above, I added a new webhook in Shopify, setting the URL to point to an Odoo controller. The URL includes the Odoo instance link followed by the controller endpoint. Whenever the selected event, "Customer Creation," occurs in Shopify, it triggers a call to this Odoo controller, which then runs the specified code.
@http.route('/products', type='json', auth='none', methods=['POST'],
csrf=False)
def get_webhook_url(self):
""" Method for creating new products while creating the product in
shopify.
dict: returns dictionary of message as success or fail.
"""
shop_name = request.httprequest.headers.get(
'X-Shopify-Shop-Domain')
shopify_instance_id = request.env[
'shopify.configuration'].with_user(SUPERUSER_ID).search(
[('shop_name', 'like', shop_name)], limit=1)
try:
if not shopify_instance_id.sudo().is_exporting:
data = request.get_json_data()
Created a controller in Odoo with the same URL as the one configured on Shopify. This controller gets triggered when Shopify sends a webhook request. Inside the controller, we can process the received data and execute various actions or methods as needed.
Clicking the "Send test" button in Shopify triggers a test call, which activates the controller.
Webhooks are a great way to connect Odoo with external systems in real time. By setting up a webhook URL using controllers in Odoo, you can respond to specific events from external systems. When these events occur, the controllers execute the defined actions automatically, helping streamline and automate your business processes for better efficiency.
To read more about How to Use Odoo 16 Webhooks to Connect External Systems, refer to our blog How to Use Odoo 16 Webhooks to Connect External Systems.