Creating a Record in Odoo from External Applications Using the Odoo External API

Abid Patel
05-Nov-2024 Updated : 05-Nov-2024

Learn how to create records in Odoo from external applications using the Odoo External API with XML-RPC and JSON-RPC protocols. Perfect for seamless data integration.

Creating a Record in Odoo from External Applications Using the Odoo External API

Connect external applications with Odoo. You can do this smoothly through Odoo External API. This API enables you to effortlessly generate records outside of applications. Odoo is compatible with XML-RPC and JSON-RPC protocols. This compatibility allows for the programming of record creation. You can create these records from any external environment. The process of integration between platforms becomes seamless.

What is Odoo's External API?

The API brings a broad spectrum of tools to the table. These tools help in accessing and interacting with Odoo models remotely. Being able to do this is especially useful in several instances. For example you might need to transfer data from an external application into Odoo. You might want to create a new customer record. Or maybe you want to initiate a sales order. The Odoo External API can help with this.

Creating Connection with Odoo's External API is more than essential. It's critical. This connection is key in creating records in Odoo from an external system. The connection can be established by using XML-RPC or JSON-RPC. Below you will find an example. The example shows the use of XML-RPC. This allows you to authenticate and send data. The data is sent into Odoo models.

Step-by-Step Example: Creating a Record with XML-RPC

Let's imagine a scenario together. Picture a partner record in Odoo. It originates from an external application. The partner's details are collected in another environment. Let's get started.

1. Set Up XML-RPC Library

Initially XML-RPC library is required. Python uses this for Odoo API communication. Installation happens with a certain command:

command

pip install xmlrpc.client

2. Authenticate with Odoo

API operation needs first step. That step is authentication. Utilize the following code snippet for Odoo connection and user ID retrieval.

python

import xmlrpc.client

# Define Odoo server URL
url = "https://your-odoo-instance.com"
db = "your_database_name"
username = "your_username"
password = "your_password"

# Authenticate
common = xmlrpc.client.ServerProxy(f"{url}/xmlrpc/2/common")
uid = common.authenticate(db, username, password, {})

3. Connect to the Model. There is a need to Create Record

Authentication is done. Now, you can access the models endpoint. This allows you to perform operations on Odoo models. The following steps show how you can create a new record.

python

models = xmlrpc.client.ServerProxy(f"{url}/xmlrpc/2/object")

# Define record data to create a new partner
partner_data = {
    'name': "New Partner",
    'email': "new.partner@example.com",
    'phone': "1234567890",
}

# Create the partner record
partner_id = models.execute_kw(db, uid, password, 'res.partner', 'create', [partner_data])
print(f"Created partner ID: {partner_id}")

JSON-RPC Example

JSON-PRC can also serve as an effective choice. This is when XML-RPC might not be the best option. Short JSON-RPC example is provided for creating a partner.

python

import requests
import json

url = "https://your-odoo-instance.com/jsonrpc"
headers = {'Content-Type': 'application/json'}

payload = {
    "jsonrpc": "2.0",
    "method": "call",
    "params": {
        "service": "object",
        "method": "execute_kw",
        "args": [db, uid, password, 'res.partner', 'create', [{'name': 'New Partner'}]],
    },
    "id": 1,
}

response = requests.post(url, headers=headers, data=json.dumps(payload))
print(f"Created partner ID: {response.json()['result']}")

Conclusion

Using Odoo's External API to make records from external applications can streamline your processes. It can also bring data to Odoo without any manual input. Whether it's XML-RPC or JSON-RPC, this API-based method improves data integration. It renders Odoo a more flexible component for your system architecture.

Make a Comment

Your email address will not be published. Required fields are marked *