To declare endpoints on classes that are not controllers (eg: services)
whose routes are generated, see endpoint_spec.
base_rest (OCA/rest-framework) has been the foundation of many APIs,
including the Shopfloor apps. Over time it showed some limits:
- it is a monolith: dispatcher, service layer, validation, documentation
and routing are bound together and you cannot use one piece without
the others
- services must be components, a second registry with its own
inheritance and boot lifecycle, which is often used only to look up a
class by name
- controllers are generated at boot from the services, making routing
hard to follow and to customize
The fastapi stack is a way to go for new standalone APIs, but moving
existing APIs to it means rewriting every service and schema and
endpoints are not extendable models. Plus, it pulls in more external
dependencies that for many cases are not needed.
This module is the base of a lighter stack, made of small modules that
can be used separately and extended via standard Odoo inheritance:
- http_api_dispatcher: the http_api route type (parsing,
validation hooks, JSON errors) usable by plain Odoo controllers
- endpoint_spec: declare endpoints on services, for routes
generators
- http_api_cerberus: Cerberus validation
- openapi_doc_base: API docs from the actual routes
- endpoint_route_handler generators: routes computed from the code
without storing a row per route
Goals:
- simplify the base_rest stack without moving to fastapi
- keep modules small and reusable
- drop the dependency on component, staying highly customizable
(routing keys per route, ir.http hooks per database)
- stay compatible with base_rest clients (error format, validation
behavior) to allow a gradual migration
To include the traceback and the description of any error in the
responses, set dev_mode in the http_api section of the
configuration file:
[http_api]
dev_mode = True
The dev_mode key of the base_rest section is honored as well.
Never enable it in production.
Use the http_api type on standard routes. Extra routing keys drive
the behavior of the dispatcher:
from odoo import http
from odoo.addons.http_api_cerberus.params import CerberusParam
class PartnerAPI(http.Controller):
@http.route(
"/api/partner/<int:id>",
type="http_api",
auth="user",
methods=["GET"],
output=CerberusParam({"id": {"type": "integer"}, "name": {"type": "string"}}),
)
def get(self, id):
partner = http.request.env["res.partner"].browse(id)
return {"id": partner.id, "name": partner.name}
Any value returned by the endpoint is serialized as JSON, unless it’s
already a response (eg: request.make_response).
Routing keys:
| Key |
Meaning |
| input |
ApiParam validating and converting request params |
| input_kwarg |
keyword used to pass non-dict input values (default payload) |
| output |
ApiParam validating and converting the result |
| prepare |
callable or list of callables receiving the ApiCall, run before
input validation (eg: check headers, update the context) |
| body_parsers |
{mimetype: callable} overriding body parsers |
| query_parser |
callable overriding the query string parser |
| error_map |
[(exception classes, HTTP exception class, include description)]
evaluated before the default error mapping |
| json_encoder |
json.JSONEncoder subclass for the response |
| csrf |
as for any Odoo route (default True). The token is read from the
X-CSRF-Token header, else from the csrf_token param of the
query or the body. Set it to False for token based APIs |
Params from the URL (eg: <int:id>) always win over query and body
params. The query string and the body are parsed on first access. An
input param reading only the query or the body keeps the other one
unparsed.
Subclass odoo.addons.http_api_dispatcher.params.ApiParam and
implement from_params, to_response and optionally
to_json_schema (used to generate API docs). Raise
odoo.addons.http_api_dispatcher.errors.ApiValidationError for
invalid input: the details of the errors are returned in the errors
key of the response.
| Exception |
Status |
Description in body |
| SessionExpiredException |
401 |
no |
| MissingError |
404 |
no |
| AccessError, AccessDenied |
403 |
no |
| UserError, ValidationError |
400 |
yes |
| werkzeug HTTPException |
its own code |
yes for 4xx |
| anything else |
500 |
no |
The body looks like
{"code": 400, "name": "Bad Request", "description": "..."} plus the
content of the rest_json_info attribute of the exception, if any.
Each step of the pipeline is a classmethod of ir.http that can be
extended:
- _http_api_prepare(call)
- _http_api_validate_input(call)
- _http_api_call(call): wrap it to log or profile calls
- _http_api_validate_output(call)
- _http_api_make_response(call)
All of them receive an ApiCall holding the request, the endpoint,
the result and an extra dict for your own data. The params are
exposed lazily: call.query, call.body and call.params (the
merged dict, built on first access).
Bugs are tracked on GitHub Issues.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
feedback.
Do not contact contributors directly about support or help with technical issues.
Error handling and request parsing are derived from base_rest
(OCA/rest-framework) by ACSONE SA/NV.
This module is maintained by the OCA.
OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.
Current maintainer:

This module is part of the OCA/rest-framework project on GitHub.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.