Odoo

Odoo jsonrpc API connections.

API versions of Odoo

The Odoo API is an XML-RPC or JSON-RPC API. There are multiple REST API plugins that can be purchased and installed in on top of the regular Odoo APIs mentioned before. Locoia covers only the JSON-RPC API as it is free of charge and requests can easily be built by viewing the requests made in the Odoo web app's Developer Tools > Network tab and using those payloads for JSON-RPC as described below.

Initial API setup

To start using the Odoo API, one needs to get the these three parameters from the Odoo account database name and a user's id and password.

1. Database

The database name can easily be found, if Odoo is set into debug mode. By adding debug=1 to the web app url, the web app is set it to debug mode, e.g. https://mycompanyname.odoo.com/web?debug=1

Once in debug mode, the database name is visible on the right next to the account name in brackets:

Note: Odoo can be installed locally (e.g. on an AWS cloud instance) or setup via Odoo.sh in the Odoo-cloud. The later avoids and manual installation.

2. User ID

Follow the steps to get it:

  1. Open the developer console of your browser on the Network tab.

  2. In Odoo click on the upper right corner, then click on "Preferences".

  3. Find the request like *your subdomain*.odoo.com/web/dataset/call_kw/res.users/read

  4. Go to the payload tab and open the payload as per the JSON-path: params.kwargs.context.uid

  5. You found the uid e.g. something 2

3. Password:

Password is your user's password of the user you logged in with. Any user can be used to get access.

JSON-RPC Connection

JSON-RPC is an easy way to connect to OdooB. Below one can find a slightly adjusted script based on the one provided from Odoo to make initial testing calls:

# Based on: https://www.odoo.com/documentation/15.0/developer/howtos/backend.html#json-rpc-library

import json
import random
import requests

HOST = "mycompanyname.odoo.com"
PORT = 443 # Default port for the Odoo.sh cloud version.
url = f"https://{HOST}:{PORT}/jsonrpc"
DB = "mycompanydb-name"
USER = "support+resmio@locoia.com"
PASSWORD = "some secret PW"


def json_rpc(url, method, params):
    data = {
        "jsonrpc": "2.0",
        "method": method,
        "params": params,
        "id": random.randint(0, 1000000000),
    }
    headers={
        "Content-Type":"application/json",
    }

    reply = requests.get(url=url, data=json.dumps(data).encode(), headers=headers)
    json_response = reply.json()
    error = None
    error = json_response.get("error")
    
    if error is not None:
        raise Exception(json_response["error"])
    return json_response["result"]

def make_request(url, service, method, *args):
    return json_rpc(url, "call", {"service": service, "method": method, "args": args})


# Get the user is for further calls.
uid = make_request(url, "common", "login", DB, USER, PASS)

To make individual calls, one needs to make further requests, based on the above snippet with the below called.

  • url = the request url as defined above

  • object = TBD

  • execute = stay the same

  • uid = the user id as defined above

  • PASSWORD = as defined initially (alternatively the api-key)

  • "res.partner" = The object to be manipulated

  • "search" = the request method

  • args = arguments specifying the request (similar to query string parameters)

args = [("phone", "ilike", "+491232...")]
phpne user = make_request(url, "object", "execute", DB, uid, PASSWORD, "res.partner", "search", args)

Note that Locoia can only send request payloads (body) on POST, PUT or PATCH requests. So for any JSON or XML-RPC requests, POST is to be used.

Searching and Search Operators in Odoo

If you want to search records using custom search operators, a (not too helpful) list of logical and search operators can be found in the source code. The most important take-aways:

Each tuple in the search domain needs to have 3 elements, in the form: **('field_name', 'operator', value)**, where:

            * **field_name** must be a valid name of field of the object model, possibly following many-to-one relationships using dot-notation, e.g 'street' or 'partner_id.country' are valid values.
            * **operator** must be a string with a valid comparison operator from this list: ``=, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right``
              The semantics of most of these operators are obvious.
              The ``child_of`` operator will look for records who are children or grand-children of a given record,
              according to the semantics of this model (i.e following the relationship field named by
              ``self._parent_name``, by default ``parent_id``.
            * **value** must be a valid value to compare with the values of **field_name**, depending on its type.

When using the Odoo Connector, you might come across simple, specific search actions (such as "Search Partner by Phone), which will make the process as easy as possible for you. However, if you want to execute more elaborate search queries, you can use the custom search endpoints, e.g. "Search Partner by Custom". These Actions allow you to pass your own Tuples (as elaborated above) in as search parameter. Be aware that, as we are using JSON to communicate with your Odoo instance, instead of using simple parenthesis ( , you will have to use square brackets ( [ ) in your tuples, like so:

[['name','=','ABC'],'!',['language.code','=','en_US']]

The difference between =, like and ilike

The = operator will look for a perfect match, the like operator will add wildcards to the beginning and end of your search term, but is case sensitive, and the ilike operator is case insensitive.

By adding a = in front of like and ilike (=like, =ilike), you can prevent the search from adding wildcards, but can control the case-sensitivity (e.g. ["name", "ilike", "dog"] will match dog, bulldog, DOGS, ["name", "=ilike", "dog"] will only match dog, Dog, DOG, etc.).

Last updated