|
| 1 | +================= |
| 2 | +External JSON API |
| 3 | +================= |
| 4 | + |
| 5 | +Odoo is usually extended internally via modules, but many of its features and all of its data are also available from the outside for external analysis or integration with various tools. Part of the :ref:`reference/orm/model` API is easily available over HTTP via the ``/json/2`` endpoint. |
| 6 | + |
| 7 | + |
| 8 | +TL;DR |
| 9 | +===== |
| 10 | + |
| 11 | +Request |
| 12 | +------- |
| 13 | + |
| 14 | +POST a json object at the ``/json/2/<model>/<method>`` URL. |
| 15 | + |
| 16 | +.. code:: http |
| 17 | +
|
| 18 | + POST /json/2/res.partner/search_read HTTP/1.1 |
| 19 | + Host: mycompany.odoo.com |
| 20 | + X-Odoo-Database: mycompany |
| 21 | + Authorization: Bearer 6578616d706c65206a736f6e20617069206b6579 |
| 22 | + Content-Type: application/json; charset=utf-8 |
| 23 | + User-Agent: mysoftware python-requests/2.25.1 |
| 24 | +
|
| 25 | + { |
| 26 | + "domain": [ |
| 27 | + ["name", "ilike", "%deco%"], |
| 28 | + ["is_company", "=", true] |
| 29 | + ], |
| 30 | + "fields": ["name"], |
| 31 | + } |
| 32 | +
|
| 33 | +.. code:: http |
| 34 | +
|
| 35 | + POST /json/2/res.partner/write HTTP/1.1 |
| 36 | + Host: mycompany.odoo.com |
| 37 | + X-Odoo-Database: mycompany |
| 38 | + Authorization: Bearer 6578616d706c65206a736f6e20617069206b6579 |
| 39 | + Content-Type: application/json; charset=utf-8 |
| 40 | + User-Agent: mysoftware python-requests/2.25.1 |
| 41 | +
|
| 42 | + { |
| 43 | + "ids": [5], |
| 44 | + "context": { |
| 45 | + "lang": "en_US" |
| 46 | + }, |
| 47 | + "vals_list": [ |
| 48 | + { |
| 49 | + "name": "Deco Classic" |
| 50 | + } |
| 51 | + ] |
| 52 | + } |
| 53 | +
|
| 54 | +The JSON must be a json-object containing the arguments for the model's method. The two ``ids`` |
| 55 | +and ``context`` are special arguments and serve to craft a recordset on which the method is |
| 56 | +executed. |
| 57 | + |
| 58 | +The headers ``Host``, ``Authorization`` (bearer + api key) and ``Content-Type`` are required. The |
| 59 | +``X-Odoo-Database`` header is only necessary when multiple databases are hosted behind a same |
| 60 | +``Host``. A ``User-Agent`` with the name of the software where the request comes from is |
| 61 | +recommended. |
| 62 | + |
| 63 | +The available models and methods depend on the list of modules that are installed in the database. |
| 64 | +The exact list of what's available is accessible on the ``/doc`` page of every database. |
| 65 | + |
| 66 | + |
| 67 | +Success response |
| 68 | +---------------- |
| 69 | + |
| 70 | +The function's return value is serialized as json in the body. |
| 71 | + |
| 72 | +.. code:: http |
| 73 | +
|
| 74 | + HTTP/1.1 200 OK |
| 75 | + Content-Type: application/json; charset=utf-8 |
| 76 | +
|
| 77 | + [ |
| 78 | + {"id": 5, "name": "Deco Addict"} |
| 79 | + ] |
| 80 | +
|
| 81 | +
|
| 82 | +Error response |
| 83 | +-------------- |
| 84 | + |
| 85 | +The errors use a sensible http status code. The error message is serialized as a json string in the |
| 86 | +body. |
| 87 | + |
| 88 | +.. code:: http |
| 89 | +
|
| 90 | + HTTP/1.1 401 Unauthorized |
| 91 | + Date: Fri, 18 Jul 2025 08:33:35 GMT |
| 92 | + Content-Type: application/json; charset=utf-8 |
| 93 | +
|
| 94 | + "Invalid apikey" |
| 95 | +
|
| 96 | +The complete traceback is available in the server log, at the same date as the error response. |
| 97 | + |
| 98 | + |
| 99 | +.. _User-Agent: https://httpwg.org/specs/rfc9110.html#field.user-agent |
0 commit comments