ODOO JSON API

Fahmi Roihanul Firdaus
2 min readSep 8, 2020

Hello guys I want to share my experience using odoo API.
so in this post, I will try to explain odoo API and simple examples of how to use it based on my experience.

If you have any advice or comment please do it on the comment section :D

Basically Odoo API is used for other applications that want access data on odoo, it can use to trigger methods on odoo or only to get data from odoo.

Schema

like example:
1. we have odoo application with URL http://localhost:8071 and react application for the front end.
2. in odoo, we have model name example.event.odoo.
3. on react application we want to show a list of data from example.event.odoo which is the model/data is in odoo application.
4. then we need to provide API in odoo for transferring data from odoo to the react app with requests and response method.

Before we start to code our API, please note that you need to have only 1 database on your odoo, if you have multiple databases you need to config some of the variables on the odoo.conf file:

db_name = aflowz_odoo13
list_db = False
# if you have var dbfilter, make it empty
dbfilter =

first, we create an API endpoint for the front end or react app, we will use odoo HTTP controller to do that.

from odoo import httpclass ExampleOdooRequest(http.Controller):    @http.route('/api/list_event', auth='public', methods=['GET'])
def get_list_event(self):
return "Succeed"

Expected Result from Postman:

Result from Postman

Yay! now we already connected to odoo with API, so now we will get data from table/model example.event.odoo with odoo ORM and throw JSON response to the front end/react app.

import json
from odoo import http
from odoo.http import request, Response
class ExampleOdooRequest(http.Controller): @http.route('/api/list_event', auth='public', methods=['GET'])
def get_list_event(self):
data = []
event = request.env['example.event.odoo'].sudo().search([])
headers_json = {'Content-Type': 'application/json'}
for eve in event:
data.append({
'name': eve.name,
'description': eve.description
})
result = {
'data': data,
'errors': {},
'meta': {}
}
return Response(json.dumps(result), headers=headers_json)

Expected Result (from Postman):

Final Result

To be continued with POST method..

--

--