Odoo REST API
Overview
Enhanced RESTful API access to Odoo resources with simplified variant of OAuth2 authentication and OpenAPI specification.
The module may have a predefined (statically and dynamically customizable) tree-like schema of response Odoo fields for ‘Read one’, ‘Read all’ and ‘Create one’ methods. This allow not to fetch unnecessary fields (like a heavy image/binary fields or technical garbage fields) in each request or not to compose the list of desired fields in each request. Also that schemas can be used as a quick and clear model reference guide for your developers (backend, client side, etc.). See “Example #3” below.
The schema of the response fields can have a tree-like structure with any level of nesting. So you can read an object at once with absolutely all its inner data (including its lines with all inner data) in just one http request. Therefore you don’t need to make a two (or much more) requests to get one object (if would so, the possible interruptions or lags between that requests can be fatal to the integrity of your data). See “Example #1” below.
The schema of the request fields can have a tree-like structure with one (and more in some cases) level of nesting. So you can easily update (or create) an object at once with all its lines (including all their data) in just one http request. See “Example #2” below.
This module has a high-load ready feature: it uses a mechanism which allow to cope with intensive and concurrent reading and writing the same Odoo records.
The previous features improves the integrity of your data, enhance the reliability of data processing and also reduce the size and complexity of code on your REST client side.
Also this module allow to fetch any PDF report, attachment or binary data from Odoo.
This module works with standard and custom Odoo models, also this API is CORS compatible.
All features and behaviour of this module are identical in all Odoo versions, so you will not have a problems when migrating.
We don’t cheat with itself purchasing and don’t use any SEO techniques.
By default, all Odoo models have a flat and non-predefined response schema.
There are two possible ways to set up predefined schemas for any Odoo model – in UI mode or by generating a special file. The special files also allow to create a custom API routes/endpoints, and in this case you can debug its code like an ordinary Python code using your favorite IDE.
- Each Odoo model has the following API methods:
-
- Read all (with optional filters, offset, limit, order, exclude_fields, include_fields)
- Read one (with optional exclude_fields, include_fields)
- Create one (with optional static default values)
- Update one/multi
- Delete one/multi
- Call any method of Odoo model, including workflow manipulations (till the Odoo v10)
- Also the ‘Call any method’ feature allow to execute any standard model’s methods, like:
-
- copy()
- check_access_rights()
- check_access_rule()
- fields_get()
- etc.
- Simple REST Client example (Python):
- import requests # Authentication r = requests.get(‘https://rest-api-demo.root.sx/api/auth/get_tokens?username=demo&password=demo’) access_token = r.json()[‘access_token’] # GET – Read record r = requests.get( ‘https://rest-api-demo.root.sx/api/sale.order/21’, headers = {‘Access-Token’: access_token}) print(r.json()) # PUT – Update record r = requests.put( ‘https://rest-api-demo.root.sx/api/res.partner/26?name=Tom Lenz&city=New York’, headers = {‘Access-Token’: access_token}) print(r)
- Custom API route/endpoint example:
- from .main import * class ControllerREST(http.Controller): @http.route(‘/api/your.custom.endpoint’, methods=[‘GET’], type=’http’, auth=’none’, cors=rest_cors_value) @check_permissions def a(self, **kw): # get Odoo env params cr, uid = request.cr, request.session.uid # get any model as ordinary Odoo object odoo_model_obj = request.env(cr, uid)[‘res.partner’] # make models manipulations, actions, computations etc. And if you need, fill the results dictionary your_custom_dict_data = {‘your_vals’: ‘…’} # send HTTP response return successful_response(status=200, dict_data=your_custom_dict_data)
Installation and Setup
First install one Python dependency:
pip install simplejson
Then install this module as ordinary Odoo module – in developer mode, go to menu Apps > Update Apps List > find this app ‘rest_api’ and install it.
This module requires ‘db_name’ Odoo config parameter (or command line option) with only one database (without aliases)!:
(config parameter) db_name = your_db_name (or command line option) –database=your_db_name
After the installation (or updating) of this module it need to restart Odoo server!
- This module adds the following ‘System Parameters’ in Odoo:
-
- rest_api.access_token_expires_in (600 seconds)
- rest_api.refresh_token_expires_in (7200 seconds)
- rest_api.cors_parameter_value_in_all_routes (‘null’)
- rest_api.u_escape_characters_for_unicode_in_responses (False)
- rest_api.use_redis_token_store (False)
- rest_api.redis_host (localhost)
- rest_api.redis_port (6379)
- rest_api.redis_db (0)
- rest_api.redis_password (None)
If you want to use the Redis token store (what is optional), you should set the Odoo system parameter rest_api.use_redis_token_store = True
, and also you need to install, (optional) setup and run Redis
server, something like this (in terminal):
(choose your package manager) sudo apt install redis-server python3-redis sudo apt-get install redis-server python3-redis sudo yum install redis python3-redis sudo dnf install redis python3-redis (run) redis-server
And then restart Odoo server.
Useful Redis links:
- https://pypi.python.org/pypi/redis
- https://redis.io/topics/quickstart
First possible way to set up predefined schemas for any Odoo model – in UI mode:
Activate the developer mode, go to the menu Settings > Technical > Database Structure > Models, open desired model and go to “REST API” tab: