APIs and Webhooks provide features to sync and share different information between different software systems. The idea of both of them is the same as sharing and syncing information between the software systems, but actually, they are different. APIs are like an interface that allows two-way communication via a request response cycle between the software systems. Webhooks are a powerful tool that allows us to receive real-time updates from external systems and perform or trigger some actions within Odoo. Rather than requests, webhooks are driven by events that occur on the software systems.
In this blog, we will go through the steps required to build a real-time integration between Odoo and an external system using Webhooks.
To drive a webhook, we need some URL that needs to send data based on some event occurrence in external software systems. This URL will be provided to the external software systems along with the event which triggers the webhook. Once this event happens, a response is sent to the system pointing to the specified URL. In Odoo, the URL must point to a controller which can manipulate the received data. To create a controller, refer to How to Configure Web Controllers in Odoo 16.
As described above, once the event occurs in the external system, the response will be sent to the URL which is pointed to the Odoo controller. From here, we can manage the data received from the external software system. Mostly the incoming data will be in JSON format. This must be parsed and can be used in the controller. In the controller, we can add different actions to be performed along with this received data.
Let us look at a real example. Here, I’m using Shopify, an eCommerce platform, as an external software system. One more thing to remember is that these external systems must support webhooks. In Shopify, it supports webhooks and thus, we can use it to integrate Odoo in real-time using webhooks.
import json
from odoo import http
from odoo.http import request
class ShopifyWebhook(http.Controller):
@http.route('/customer', type='json', auth='none', methods=['POST'],
csrf=False)
def get_webhook_url(self, *args, **kwargs):
print('received webhook data')
data = json.loads(request.httprequest.data)
print('data', data)