Locoia
Search…
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 instances HOST name, database name and a users email and password. The host name is the subdomain and domain of an instance, e.g. mycompanyname.odoo.com. 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
Odoo App in "debug mode" enabled will show the small bug in the upper left corner.
Once in debug mode, the database name is visible on the right next to the account name in brackets:
Database name as visible in debug mode.
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.

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:
1
# Based on: https://www.odoo.com/documentation/15.0/developer/howtos/backend.html#json-rpc-library
2
3
import json
4
import random
5
import requests
6
7
HOST = "mycompanyname.odoo.com"
8
PORT = 443 # Default port for the Odoo.sh cloud version.
9
url = f"https://{HOST}:{PORT}/jsonrpc"
10
DB = "mycompanydb-name"
12
PASSWORD = "some secret PW"
13
14
15
def json_rpc(url, method, params):
16
data = {
17
"jsonrpc": "2.0",
18
"method": method,
19
"params": params,
20
"id": random.randint(0, 1000000000),
21
}
22
headers={
23
"Content-Type":"application/json",
24
}
25
26
reply = requests.get(url=url, data=json.dumps(data).encode(), headers=headers)
27
json_response = reply.json()
28
error = None
29
error = json_response.get("error")
30
31
if error is not None:
32
raise Exception(json_response["error"])
33
return json_response["result"]
34
35
def make_request(url, service, method, *args):
36
return json_rpc(url, "call", {"service": service, "method": method, "args": args})
37
38
39
# Get the user is for further calls.
40
uid = make_request(url, "common", "login", DB, USER, PASS)
41
Copied!
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)
1
args = [("phone", "ilike", "+491232...")]
2
phpne user = make_request(url, "object", "execute", DB, uid, PASSWORD, "res.partner", "search", args)
Copied!
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:
1
Each tuple in the search domain needs to have 3 elements, in the form: **('field_name', 'operator', value)**, where:
2
3
* **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.
4
* **operator** must be a string with a valid comparison operator from this list: ``=, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right``
5
The semantics of most of these operators are obvious.
6
The ``child_of`` operator will look for records who are children or grand-children of a given record,
7
according to the semantics of this model (i.e following the relationship field named by
8
``self._parent_name``, by default ``parent_id``.
9
* **value** must be a valid value to compare with the values of **field_name**, depending on its type.
Copied!
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:
1
[['name','=','ABC'],'!',['language.code','=','en_US']]
Copied!

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 modified 2mo ago