Skip to content

Commit 46a9f49

Browse files
committed
wip
1 parent 45a3e66 commit 46a9f49

File tree

1 file changed

+63
-44
lines changed

1 file changed

+63
-44
lines changed

content/developer/reference/external_json2_api.rst

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ External JSON-2 API
55
.. versionadded:: 19.0
66

77
Odoo is usually extended internally via modules, but many of its features and all of its data are
8-
also available from the outside for external analysis or integration with various tools. Part of
9-
the :ref:`reference/orm/model` API is easily available over HTTP via the ``/json/2`` endpoint.
8+
also available from the outside for external analysis or integration with various other softwares.
9+
Part of the :ref:`reference/orm/model` API is easily available over HTTP via the ``/json/2``
10+
endpoint. The actual models, their fields and methods are specific to every database and can be
11+
consulted at the ``/doc`` page.
1012

1113

12-
Examples
13-
========
14-
1514
Request
16-
-------
15+
=======
1716

1817
POST a json object at the ``/json/2/<model>/<method>`` URL.
1918

@@ -27,37 +26,20 @@ POST a json object at the ``/json/2/<model>/<method>`` URL.
2726
User-Agent: mysoftware python-requests/2.25.1
2827
2928
{
29+
"ids": [],
30+
"context": {
31+
"lang": "en_US"
32+
}
3033
"domain": [
3134
["name", "ilike", "%deco%"],
3235
["is_company", "=", true]
3336
],
3437
"fields": ["name"],
3538
}
3639
37-
.. code:: http
38-
39-
POST /json/2/res.partner/write HTTP/1.1
40-
Host: mycompany.odoo.com
41-
X-Odoo-Database: mycompany
42-
Authorization: Bearer 6578616d706c65206a736f6e20617069206b6579
43-
Content-Type: application/json; charset=utf-8
44-
User-Agent: mysoftware python-requests/2.25.1
45-
46-
{
47-
"ids": [25],
48-
"context": {
49-
"lang": "en_US"
50-
},
51-
"vals_list": [
52-
{
53-
"name": "Deco Classic"
54-
}
55-
]
56-
}
57-
58-
The body must be a json-object containing the arguments for the model's method. The two ``ids``
59-
and ``context`` are special arguments and serve to craft a recordset on which the method is
60-
executed.
40+
The body must be a json-object containing the arguments for the model's method. Both ``ids`` and
41+
``context`` are special arguments: they are used to craft the environment and recordset on which the
42+
method is executed.
6143

6244
The headers ``Host``, ``Authorization`` (bearer + api key) and ``Content-Type`` are required. The
6345
``X-Odoo-Database`` header is only necessary when multiple databases are hosted behind a same
@@ -68,8 +50,11 @@ The available models and methods depend on the list of modules that are installe
6850
The exact list of what's available is accessible on the ``/doc`` page of every database.
6951

7052

71-
Success response
72-
----------------
53+
Response
54+
========
55+
56+
Success
57+
-------
7358

7459
A **200 OK** status with the method's return value serialized as json in the body.
7560

@@ -83,8 +68,8 @@ A **200 OK** status with the method's return value serialized as json in the bod
8368
]
8469
8570
86-
Error response
87-
--------------
71+
Error
72+
-----
8873

8974
A **4xx**/**5xx** status with the error message serialized as a json string in the body.
9075

@@ -99,17 +84,23 @@ A **4xx**/**5xx** status with the error message serialized as a json string in t
9984
The complete traceback is available in the server log, at the same date as the error response.
10085

10186

102-
.. _User-Agent: https://httpwg.org/specs/rfc9110.html#field.user-agent
10387

88+
Database
89+
========
90+
91+
Depending on the deploiement, the ``Host`` and/or ``X-Odoo-Database`` request headers might be
92+
required. The ``Host`` header is required on servers where Odoo is installed next to other web
93+
applications, so a web-server/reverse-proxy is able to route the request to the Odoo server. The
94+
``X-Odoo-Database`` header is required when a single Odoo server hosts multiple databases, and that
95+
:ref:`dbfilter` wasn't configured to use the ``Host`` header.
10496

105-
Authentication & Access Control
106-
===============================
97+
Most HTTP client libraries automatically set the ``Host`` header using the connection url.
10798

108-
The JSON-2 API uses the access rights of the current user for all operations, and the user is
109-
selected using an API key.
11099

111100
API Key
112-
-------
101+
=======
102+
103+
An API key must be set in the ``Authorization`` request header, as a bearer token.
113104

114105
Create a new API key for a user via :guilabel:`Preferences`, :guilabel:`Account Security`, and
115106
:guilabel:`New API Key`.
@@ -134,10 +125,38 @@ for interactive usage. It is not possible to create keys that last for more than
134125
that long lasting keys must be rotated at least once every 3 months.
135126

136127
The :guilabel:`Generate Key` creates a 20 bytes (160 bits) strong random key. Its value appears on
137-
screen, this is the only time and place the key is visible on screen, it must be copied and stored
138-
somewhere safe. If it ever gets compromized or lost, then it must be removed.
128+
screen, this is the only time and place the key is visible on screen, it must be copied, kept secret and stored somewhere secure. If it ever gets compromized or lost, then it must be removed.
139129

140-
The `Secrets Management Cheat Sheet`_ is a document published by the OWASP foundation on how to
141-
safely manage and store secrets such as API keys, with additionnal resources linked at the end.
130+
Please refer to OWASP's `Secrets Management Cheat Sheet`_ for further guidance on the management of
131+
API keys.
142132

143133
.. _Secrets Management Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html#secrets-management-cheat-sheet
134+
135+
136+
Access Rights
137+
=============
138+
139+
The JSON-2 API uses the standard :ref:`security <reference/security>` model of Odoo. All operations
140+
are validated against the access rights, record rules and field accesses granted to the current
141+
user. The current user is used as well for the :ref:`reference/fields/automatic/log_access`.
142+
143+
For **interfactive usage**, such as discovering the API or running one-time scripts, it is fine to
144+
use a **personal account**.
145+
146+
For **extended automated usage**, such as an integration with another software, it is recommended to
147+
create and use **dedicated bot users**.
148+
149+
Using dedicated bot users has several benefits:
150+
151+
* The minimum required permissions can be granted to the bot, limiting the impact may the API key
152+
gets compromised;
153+
* The password can be set empty to disable login/password authentication, limiting the likelihood
154+
the account get compromized;
155+
* The :ref:`reference/fields/automatic/log_access` use the bot account. No internal user gets
156+
impersonalized.
157+
158+
159+
Transaction
160+
===========
161+
162+
.. TODO

0 commit comments

Comments
 (0)