A common need in Odoo projects is to expose certain data or actions to external systems via APIs. This allows other tools to integrate easily with Odoo. Until now, the most common options were:
- Using the standard jsonrpc controllers.
- Manually implementing endpoints with http.Controller.
- Setting up an external middleware service with Flask or FastAPI.
However, thanks to the OCA’s fastapi module, we can now build a modern RESTful API directly inside Odoo, with automatic validation, OpenAPI documentation, and decoupled from the core.
In this post, we’ll explain how we used this module to expose Odoo resources in a robust and maintainable way.
What is the OCA FastAPI Module?
The fastapi module (available in the OCA/rest-frameworkrepository) lets you define a RESTful interface using the FastAPI library, fully integrated into the Odoo environment. Some of its benefits include:
- Automatically validates input and output data using Pydantic.
- Automatically generates Swagger documentation.
- Allows you to define functions in a specific and structured way.
- Supports multiple authentication methods.
Real Example: Exposing Products
To demonstrate its functionality, let’s build a small example that exposes products from our Odoo instance.
First, we define the router that will handle incoming requests. It’s recommended to place this in a file named router.py within our module.
from fastapi import APIRouter
router = APIRouter()
Next, we create a new type of endpoint application. For this, it’s common to use a file called models/fastapi_endpoint.py.
from odoo import fields, models
from ..router import router
class FastapiEndpoint(models.Model):
_inherit = "fastapi.endpoint"
app: str = fields.Selection(
selection_add=[("my_router", "My new router")],
ondelete={"my_router": "cascade"}
)
def _get_fastapi_routers(self):
if self.app == "my_router":
return [router]
return super()._get_fastapi_routers()
Then we define the routes themselves. This can be placed in a file called services/products.py.
from odoo.addons.fastapi.dependencies import odoo_env
from odoo.api import Environment
from typing import Annotated
from fastapi import Depends
from ..router import router
from pydantic import BaseModel
class ProductDTO(BaseModel):
id: int
name: str
list_price: float
@router.get("/products")
async def products(
env: Annotated[Environment, Depends(odoo_env)]
) -> list[ProductDTO]:
products = env["product.product"].sudo().search([])
return [
ProductDTO(
id=p.id,
name=p.name,
list_price=p.list_price
)
for p in products
]
Finally, in the web interface we can create and configure our endpoint. This can be accessed from the FastAPI menu in Odoo.
You can also view the auto-generated documentation and even test the implemented routes directly.
Authentication and Permissions
The module supports several authentication methods, but for simplicity we didn’t configure any in the example. It’s important to review this aspect before deploying to production.
When Should You Use (or Not Use) This Module?
The FastAPI module is very useful, but before implementing these kinds of integrations, we should ask ourselves whether the integration is really necessary. From experience, many times it’s not. The simpler our system is, the easier it is to maintain in the long term.
Conclusion
The OCA’s FastAPI module lets us expose modern APIs within Odoo without unnecessary complexity, avoiding duplication of business logic, and following best practices.
It’s especially useful when we want to offer standard, well-documented, and robust integrations without relying on spaghetti code inside http.Controller.
A very recommended solution when working with integrations between Odoo and mobile apps, React/Vue frontends, or other third-party systems.