how-to-integrate-an-external-api-with-odoo-using-xml-rpc

How to Integrate an External API with Odoo Using XML-RPC?

Integrating External API with Odoo Using XML-RPC

Integrating an External API with Odoo can open strong automation and simplify business operations. Whether you want to connect third-party apps, sync data in real time, or enhance your Odoo system’s capabilities, one of the most commonly used methods for integrating with Odoo externally is through XML-RPC, which offers a reliable way to do it. 

Odoo provides a robust API that allows external applications to interact with its database. In this guide, we’ll break down the process of integrating an external API with Odoo using XML-RPC step by step. You’ll learn to establish connections, send requests, manage responses smoothly and perform CRUD (Create, Read, Update, Delete) operations using XML-RPC, even if you’re new to API integration. Ready to simplify your workflows and boost productivity? Let’s get started!

Step-by-Step Guide to Integrating an External API with Odoo Using XML-RPC

Integrating an external API with Odoo using XML-RPC covers essential steps like authentication, data retrieval, and record management. Follow the clear instructions to automate your Odoo ERP workflows and enhance system functionality.

1. Setting Up XML-RPC in Python

To interact with Odoo via XML-RPC, we use Python’s xmlrpc.client module. First, ensure you have Python installed and then import the required module:

				
					import xmlrpc.client as xmlrpclib
				
			

2. Authentication with Odoo Server

Before we can interact with any Odoo model, we need to authenticate with the Odoo server using the common endpoint.

				
					url_common = 'http://localhost:8019/xmlrpc/2/common'
db = ‘Your_db_name’
username = 'admin'
pwd = 'admin'

common = xmlrpclib.ServerProxy(url_common)
uid = common.authenticate(db, username, pwd, {})
if uid:
    print("Authentication success")
else:
    print("Authentication failed")

				
			
  • url_common: The URL for the Odoo XML-RPC common endpoint.
  • db: The database name.
  • username & pwd: Credentials for authentication.
  • uid: The unique identifier for the authenticated user. If uid is returned, authentication is successful.

3. Accessing Models and Understanding execute_kw

After authentication, we need to interact with Odoo ERP models through the object endpoint.

				
					url_object = 'http://localhost:8019/xmlrpc/2/object'
model = xmlrpclib.ServerProxy(url_object)

				
			
  • url_object: The URL for the Odoo XML-RPC object endpoint, used to interact with the models.
  • model: The proxy object that lets us perform operations on Odoo models.

Understanding execute_kw

The execute_kw method is used to call model methods remotely.

				
					Variable_name=model.execute_kw(db, uid, pwd, model_name, method_name, args, kwargs)
				
			
  • db: Database name.
  • uid: User ID obtained after authentication.
  • pwd: User password.
  • model_name: Name of the model you want to interact with (e.g., ‘res.partner’).
  • method_name: Method you want to call (e.g., ‘search’, ‘read’, ‘create’, ‘write’, ‘unlink’).
  • args: Positional arguments (usually a list).
  • kwargs: Keyword arguments (optional dictionary).

4. Searching for Records in res.partner

Once authenticated, we can interact with Odoo models. To search for records in the res.partner model, we use the search method.

				
					res_partner = model.execute_kw(db, uid, pwd, 'res.partner', 'search', [[]], {'offset': 1, 'limit': 1})
print("Search Result:", res_partner)

				
			
  • The search method retrieves records based on conditions.
  • The empty list [] means no filter is applied.
  • offset: 1 skips the first record.
  • limit: 1 returns only one record.

5. Reading Data from res.partner

To fetch details of the retrieved record(s), we use the read method.

				
					res_partner_read = model.execute_kw(db, uid, pwd, 'res.partner', 'read', [res_partner], {'fields': ['name', 'company_type', 'street', 'street2', 'city', 'vat']})
print("Read Data:", res_partner_read)

				
			
  • The read method fetches the details of the records retrieved in the search query.
  • The fields parameter specifies the fields to retrieve.

6. Counting Records Using search_count

To count the number of records in a model, use search_count.

				
					res_partner_count = model.execute_kw(db, uid, pwd, 'res.partner', 'search_count', [[['is_company', '=', True]]], {})
print("Total Companies:", res_partner_count)

				
			
  • search_count returns the total count of records matching the given criteria.
  • The filter [[‘is_company’, ‘=’, True]] ensures we count only companies.

7. Creating a New Record Using create

You can create a new record in Odoo using the create method.

				
					partner_id = model.execute_kw(db, uid, pwd, 'res.partner', 'create', [{
    'name': 'New Partner',
    'company_type': 'company',
    'city': 'New York'
}])
print("Created Partner ID:", partner_id)

				
			

The create method inserts a new record into the model.

8. Updating an Existing Record Using write

To update an existing record, use the write method.

				
					update_partner = model.execute_kw(db, uid, pwd, 'res.partner', 'write', [[partner_id], {'city': 'Los Angeles'}])
print("Update Status:", update_partner)

				
			

The write method updates the fields of existing records.

9. Deleting a Record Using unlink

To delete a record, use the unlink method.

				
					delete_partner = model.execute_kw(db, uid, pwd, 'res.partner', 'unlink', [[partner_id]])
print("Delete Status:", delete_partner)

				
			

The unlink method removes the record(s) with the given IDs.

Your Odoo Integration Journey Starts Here

Integrating external applications with Odoo using XML-RPC is an effective way to automate operations and synchronize data. We have covered authentication, searching, reading, counting, creating, updating, and deleting records using Python’s XML-RPC library.

With these techniques, you can extend and customize Odoo’s functionality according to your business needs, enabling more intelligent workflows, efficient data management, and smooth system connectivity.

Picture of Madhu Satya Deva Raju

Madhu Satya Deva Raju

I'm Madhu Satya Deva Raju P, a Junior Odoo Developer passionate about building solutions through Odoo development, customization, and integration. I share insights and experiences here to help others grow in the ERP ecosystem.

Get a free quote