Blog

News & Events, Quick Tips
Rest API in Odoo 15
February 24, 2022
REST APIs (also called RESTful APIs) are programming interfaces (API or web API) that adhere to REST's constraints. It is designed to interact with RESTful web services.
What is an API?
APIs are a set of definitions and protocols for developing and integrating applications. Sometimes, it's called a contract between an information provider and an information consumer - establishing the content required by the consumer (the request) and the content required by the producer (the response). APIs allow you to communicate what you want to a computer or system so it can understand and fulfill your request if you want to retrieve information or perform a task. Organizations can also share resources and information using this method while maintaining security, control, and authentication — deciding who has access to what.Features of Odoo REST API
You can create RESTful APIs for Odoo using the REST API Module. It allows access to and modification of data through HTTP requests.
- Accessing Endpoints is made easy by creating API keys.
- Endpoint users are provided with their own API key.
- Access rights can be assigned to multiple API keys in different ways.
- A record is created and retrieved from the database.
- In a Database, records can be updated or even deleted.
- Database records can be searched easily.
- Find out how a table's schema is structured.
- You can trigger your Odoo actions.
- In the Endpoint response, you will find a message about access rights.
Here is an example of a simple get API to get all the purchase orders from odoo:
from odoo import HTTP from odoo.http import request
class Purchase(http.Controller):
@http.route('/api/get-po',type='json', auth="public", methods=['GET'], csrf=False)
def po_details(self , **kwargs):
response = []
purchase_details = env[‘purchase.order’].sudo().search([])
for rec in purchase_details:
response.append({
‘po_name’:rec.name,
‘po_date’:rec.date
})
return json.dumps({‘po_name’:rec.name,’po_date’:rec.date})
This is a get api that returns the details of the PO from the system. Just like this, we can create an API of any RESTFUL method.