RESTFUL API V16

by
Odoo

49.78

v 16.0 Third Party
Availability
Odoo Online
Odoo.sh
On Premise
Lines of code 576
Technical Name odooapi_restful
LicenseLGPL-3
Versions 15.0 16.0
You bought this module and need support? Click here!
Availability
Odoo Online
Odoo.sh
On Premise
Lines of code 576
Technical Name odooapi_restful
LicenseLGPL-3
Versions 15.0 16.0

Restful API Documentation

The Restful API provides access to the authentication token, allowing users to interact with the Odoo system programmatically. This API is designed to authenticate users and generate access tokens for subsequent requests.

API Overview

Operation Endpoint Description
Login /rest/api/login/token User authentication to obtain an access token.
Logout /rest/api/logout Logout and invalidate the user's access token.
Search and Read /restful/api/v1/{model} Search and retrieve resources from the specified model. Use the HTTP GET method to perform a search query on the model, returning a list of matching resources. You can customize the search parameters in the request URL.
Create /restful/api/v1/{model} Create a new resource. Use the HTTP POST method to add a new item to the specified model.
Update /restful/api/v1/{model}/{id} Update information for a specific resource identified by {id}. Use the HTTP PUT or PATCH method to modify the item.
Delete /restful/api/v1/{model}/{id} Delete a specific resource identified by {id}. Use the HTTP DELETE method to remove the item.
Call Method /restful/api/v1/{model}/{id}/{method} Invoke a specific method for a resource. Use the HTTP POST method to call a custom method on the specified resource.

Enable API access for the model.

Authentication Endpoint

Token Retrieval

Request
  • URL: http://localhost:8069/rest/api/login/token
  • Method: GET
Parameters
  • login (string, required): The username of the user.
  • password (string, required): The password of the user.
  • db (string, required): The name of the database.
Example
import requests
import json

base_url = 'http://localhost:8069/rest/api/login/token'
params = {
    'login': 'admin',
    'password': 'admin@123',
    'db': 'saleorder_api'
}

response = requests.get(base_url, params=params)

content = json.loads(response.content.decode('utf-8'))
print(content)
Response
{
    "uid": 2,
    "access_token": "access_token_c12010c3c56abf81345358020678f8c277554f41"
}

Error Handling

In case of authentication failure or invalid parameters, the API may respond with an error message. Check the response status code and content for details on the encountered issue.

Security Considerations

  • Ensure that sensitive information, such as login credentials, is transmitted securely.
  • It is recommended to use HTTPS for secure data transmission.

Notes

  • The access token obtained from the authentication endpoint should be included in the headers of subsequent requests for authentication.

API Documentation: Delete Access Token

Endpoint

DELETE http://localhost:8069/rest/api/logout/token

Description

This endpoint is used to delete a specific access token associated with a user.

Request Parameters

Headers

  • access_token: (string) The access token to be deleted. (Required)

Example

import requests
              
              # API endpoint URL
              base_url = 'http://localhost:8069/rest/api/logout/token'
              
              # Access token to be deleted
              access_token = 'access_token_c12010c3c56abf81345358020678f8c277554f41'
              
              # Request parameters
              params = {
                  'access_token': access_token,
              }
              
              # Make a DELETE request to the endpoint
              response = requests.delete(base_url, params=params)
              
              # Check the response
              if response.status_code == 200:
                  print("Access token successfully deleted")
              else:
                  print(f"Failed to delete access token. Status code: {response.status_code}")
                  print(response.text)

Response

Status Code: 200 OK

Description: Access token successfully deleted.

Status Code: 4xx or 5xx

Description: Failed to delete access token. Check the error details in the response.

GET: Search and Read API

Request
This endpoint retrieves information about a specific resource.
  • URL: http://localhost:8069/restful/api/v1/{model}
  • Method: GET

Parameters

Parameter Description
offset Offset for paginating results
limit Limit for results
order Order by for results
More Domain Filters field : value | example: (create_date_start, create_date_end, country_id, etc...)

How to create API Filters

Sample Request


import requests
import json

base_url = 'http://your-api-base-url/restful/api/v1/sale.order'

# Optional: If you have additional parameters, include them in the payload
payload = {
    'fields': 'name,id,partner_id,order_line',
    'offset': 0,
    'limit': 2,
    'order': 'name desc',
    'create_date_start':"2023-12-31",
    'create_date_end':"2024-12-01",
    'date_order_start':"2023-12-31",
    'date_order_end':"2024-12-31",
}

headers = {
    'access-token': 'access_token_ecaabbb5d4bc93381c2eeba1fbc89478d0d4b83f',
}

# Make a GET request to the endpoint
response = requests.get(base_url, headers=headers, params=payload)

# Check the response
if response.status_code == 200:
    # Access the response content (JSON format)
    data = response.json()
    print("Response:", data)
else:
    print(f"Failed to fetch data. Status code: {response.status_code}")
    print("Error Details:", response.text)

        

Sample Response


    [{
        'id': 40,
        'name': 'Your Sale Order Name',
        'date_order': '2024-01-26T13:37:58',
        'create_date': '2024-01-26T13:37:58.701565',
        'amount_tax': 0.0,
        'amount_total': 200.0,
        'state': 'draft',
        'partner_id': {
            'id': 11,
            'name': 'Gemini Furniture',
            'city': 'Fairfield'
        },
        'user_id': {
            'id': 2,
            'name': 'Mitchell Admin'
        },
        'order_line': [{
            'id': 56,
            'product_id': {
                'id': 23,
                'name': 'Conference Chair',
                'type': 'product',
                'barcode': False,
                'categ_id': {
                    'id': 8,
                    'name': 'Office Furniture'
                },
                'attribute_line_ids': [{
                    'id': 3,
                    'display_name': 'Legs'
                }]
            },
            'name': 'Product 1',
            'product_uom_qty': 2.0,
            'price_unit': 100.0,
            'price_subtotal': 200.0
        }]
    }, {
        'id': 35,
        'name': 'Your Sale Order Name',
        'date_order': '2024-01-26T12:15:49',
        'create_date': '2024-01-26T12:15:49.176117',
        'amount_tax': 0.0,
        'amount_total': 200.0,
        'state': 'draft',
        'partner_id': {
            'id': 11,
            'name': 'Gemini Furniture',
            'city': 'Fairfield'
        },
        'user_id': {
            'id': 2,
            'name': 'Mitchell Admin'
        },
        'order_line': [{
            'id': 51,
            'product_id': {
                'id': 23,
                'name': 'Conference Chair',
                'type': 'product',
                'barcode': False,
                'categ_id': {
                    'id': 8,
                    'name': 'Office Furniture'
                },
                'attribute_line_ids': [{
                    'id': 3,
                    'display_name': 'Legs'
                }]
            },
            'name': 'Product 1',
            'product_uom_qty': 2.0,
            'price_unit': 100.0,
            'price_subtotal': 200.0
        }]
    }]

        

Read schema.

Sample Response Schema


[
    'id',
    'name',
    'date_order',
    'create_date',
    'amount_tax',
    'amount_total',
    'state',
    ('partner_id', (
        'id',
        'name',
        'city',
    )),
    ('user_id', (
        'id',
        'name',
    )),
    ('payment_term_id', (
        'id',
        'name',
    )),
    ('order_line', [(
        'id',
        ('product_id', (  # many2one
            'id',
            'name',
            'type',
            'barcode',
            ('categ_id', (  # many2one
                'id',
                'name',
            )),
            ('attribute_line_ids', [(  # one2many
                'id',
                'display_name',
            )]),
        )),
        'name',
        'product_uom_qty',
        'price_unit',
        ('tax_id', [(  # many2many
            'id',
            'name',
        )]),
        'price_subtotal',
    )]),
]

POST: Creating a Record

Endpoint URL

  • URL: http://localhost:8069/restful/api/v1/{model}
  • Method: POST

Sample Request:


import requests

api_endpoint = 'http://localhost:8069/restful/api/v1/sale.order'
headers = {
    'content-type': 'application/json',
    'charset': 'utf-8',
    'access-token': 'access_token_ecaabbb5d4bc93381c2eeba1fbc89478d0d4b83f',
}
data = {
    'name': 'Your Sale Order Name',
    'partner_id': 11,
    'order_line': [(0,0,
    {
        'name': 'Product 1',
        'product_id': 23,  # Replace with the actual product ID
        'product_uom_qty': 2,
        'price_unit': 100,
    }),
    (0,0,
    {
        'name': 'Product 2',
        'product_id': 27,  # Replace with the actual product ID
        'product_uom_qty': 2,
        'price_unit': 100,
    })
    # Add more order lines if needed
],
}
response = requests.post(api_endpoint, headers=headers, json=data)

print(response.status_code)
print(response.json())

Sample Response:


    {
        'id': 41,
        'name': 'Your Sale Order Name',
        'date_order': '2024-01-26T13:57:52',
        'create_date': '2024-01-26T13:57:52.581871',
        'amount_tax': 0.0,
        'amount_total': 400.0,
        'state': 'draft',
        'partner_id': {
            'id': 11,
            'name': 'Gemini Furniture',
            'city': 'Fairfield'
        },
        'user_id': {
            'id': 2,
            'name': 'Mitchell Admin'
        },
        'order_line': [{
            'id': 57,
            'product_id': {
                'id': 23,
                'name': 'Conference Chair',
                'type': 'product',
                'barcode': False,
                'categ_id': {
                    'id': 8,
                    'name': 'Office Furniture'
                },
                'attribute_line_ids': [{
                    'id': 3,
                    'display_name': 'Legs'
                }]
            },
            'name': 'Product 1',
            'product_uom_qty': 2.0,
            'price_unit': 100.0,
            'price_subtotal': 200.0
        }, {
            'id': 58,
            'product_id': {
                'id': 27,
                'name': 'Drawer Black',
                'type': 'product',
                'barcode': False,
                'categ_id': {
                    'id': 8,
                    'name': 'Office Furniture'
                }
            },
            'name': 'Product 2',
            'product_uom_qty': 2.0,
            'price_unit': 100.0,
            'price_subtotal': 200.0
        }]
    }

Create / Update API Respomse schema.

Sample Response Schema


[
    'id',
    'name',
    'date_order',
    'create_date',
    'amount_tax',
    'amount_total',
    'state',
    ('partner_id', (
        'id',
        'name',
        'city',
    )),
    ('user_id', (
        'id',
        'name',
    )),
    ('payment_term_id', (
        'id',
        'name',
    )),
    ('order_line', [(
        'id',
        ('product_id', (  # many2one
            'id',
            'name',
            'type',
            'barcode',
            ('categ_id', (  # many2one
                'id',
                'name',
            )),
            ('attribute_line_ids', [(  # one2many
                'id',
                'display_name',
            )]),
        )),
        'name',
        'product_uom_qty',
        'price_unit',
        ('tax_id', [(  # many2many
            'id',
            'name',
        )]),
        'price_subtotal',
    )]),
]

PUT : Update a Record

This documentation provides details about a Python script that makes a PUT request to update a record through a RESTful API endpoint.

Endpoint URL

  • URL: http://localhost:8069/restful/api/v1/{model}/{id}
  • Method: PUT

Request:


import requests

api_endpoint = 'http://localhost:8069/restful/api/v1/res.partner/12'

headers = {
    'access-token': 'access_token_ecaabbb5d4bc93381c2eeba1fbc89478d0d4b83f',
}

data = {
    'name': "Nikil M",
}

# Sending a PUT request to update a record
response = requests.put(api_endpoint, headers=headers, json=data)

# Printing the response
print(response.status_code)
print(response.json())

    

Response:


{
    'name': 'Nikil M',
    'id': 12
}

    

Response Schema


[
    'name': 'string',
    'id': 'integer'
    // Add more fields and their data types as needed
]

                    

Method DELETE: Delete a Record

This documentation provides details about a Python script that makes a DELETE request to remove a record through a RESTful API endpoint.

Endpoint URL

http://localhost:8069/restful/api/v1/{model}/{id}

Python Script


import requests

api_endpoint = 'http://localhost:8069/restful/api/v1/sale.order/43'

headers = {
    'access-token': 'access_token_ecaabbb5d4bc93381c2eeba1fbc89478d0d4b83f',
}

response = requests.delete(api_endpoint, headers=headers)

print(response.status_code)
print(response.json())

                    

Expected Response


record 43 has been successfully deleted

                    

PATCH: Call method

This documentation provides details about a Python script that makes a PATCH request to call a method through a RESTful API endpoint.

Endpoint URL

http://localhost:8069/restful/api/v1/{model}/{id}/{method_name}

Python Script


    import requests

    url = "http://localhost:8069/restful/api/v1/res.partner/42/read"
    
    
    payload = {
        "arg1": "val",
        "arg2": "25"
    }
    headers = {
        'Access-Token': 'access_token_ecaabbb5d4bc93381c2eeba1fbc89478d0d4b83f',
    }
    
    response = requests.patch(url, headers=headers, json=payload)
    
    print(response.text)

                    

Expected Response


    [{
        "id": 11,
        "message_is_follower": false,
        "message_follower_ids": [],
        "message_partner_ids": [],
        "message_ids": [],
        "has_message": false,
        "message_needaction": false,
        "message_needaction_counter": 0,
        "message_has_error": false,
        "message_has_error_counter": 0,
        "message_attachment_count": 0,
        "message_main_attachment_id": false,
        "website_message_ids": [],
        "message_has_sms_error": false,
        "email_normalized": "gemini.furniture39@example.com",
        "avatar_128": "b'iVBORw0KGgoAAAANSUhEUgAAAIAAAAC'",
        "name": "Gemini Furniture",
        "display_name": "Gemini Furniture",
        "date": false,
        "title": false,
        "parent_id": false,
        "parent_name": false,
        "child_ids": [20, 22, 31, 23],
        "ref": false,
        "lang": "en_US",
        "active_lang_count": 1,
        "tz": false,
        "tz_offset": "+0000",
        "user_id": false,
        "vat": false,
        "same_vat_partner_id": false,
        "same_company_registry_partner_id": false,
        "company_registry": false,
        "bank_ids": [],
        "website": "http://www.gemini-furniture.com/",
        "comment": false,
        "category_id": [],
        "active": true,
        "employee": false,
        "function": false,
        "type": "contact",
        "street": "317 Fairchild Dr",
        "street2": false,
        "zip": "94535",
        "city": "Fairfield",
        "state_id": [13, "California (US)"],
        "country_id": [233, "United States"],
        "country_code": "US",
        "partner_latitude": 0.0,
        "partner_longitude": 0.0,
        "email": "gemini.furniture39@example.com",
        "phone": "(941)-284-4875",
        "mobile": false,
        "is_company": true,
        "is_public": false,
        "industry_id": false,
        "company_type": "company",
        "company_id": false,
        "color": 0,
        "user_ids": [],
        "partner_share": true,
        "contact_address": "Gemini Furniture\n317 Fairchild Dr\n\nFairfield CA 94535\nUnited States",
        "commercial_partner_id": [11, "Gemini Furniture"],
        "commercial_company_name": "Gemini Furniture",
        "company_name": false,
        "barcode": false,
        "self": [11, "Gemini Furniture"],
        "__last_update": "2024-01-26T05:22:53.842766",
        "create_uid": [1, "OdooBot"],
        "create_date": "2024-01-25T14:47:02.297862",
        "write_uid": [2, "Mitchell Admin"],
        "write_date": "2024-01-26T05:22:53.842766",
        "im_status": "im_partner",
        "channel_ids": [],
        "signup_token": "sLNwQePakrLBSgrQssVE",
        "signup_type": "signup",
        "signup_expiration": false,
        "signup_valid": true,
        "signup_url": "http://localhost:8069/web/signup?db=saleorder_api&token=sLNwQePakrLBSgrQssVE",
        "employee_ids": [],
        "employees_count": 0,
        "property_product_pricelist": [1, "Public Pricelist (USD)"],
        "team_id": false,
        "supplier_rank": 0,
        "customer_rank": 0,
        "duplicated_bank_account_partners_count": 0,
        "task_ids": [],
        "task_count": 0,
        "property_stock_customer": [5, "Partners/Customers"],
        "property_stock_supplier": [4, "Partners/Vendors"],
        "picking_warn": "no-message",
        "picking_warn_msg": false,
        "sale_order_count": 24,
        "sale_order_ids": [41, 40, 39, 38, 37, 36, 35, 24, 23, 22, 21, 16, 14, 13, 12, 10, 8, 7, 4, 9, 15, 11, 17, 18],
        "sale_warn": "no-message",
        "sale_warn_msg": false,
        "property_delivery_carrier_id": [3, "Local Delivery"]
    }]

                    
For Support

webdeveloper.inf@gmail.com

Please log in to comment on this module

  • The author can leave a single reply to each comment.
  • This section is meant to ask simple questions or leave a rating. Every report of a problem experienced while using the module should be addressed to the author directly (refer to the following point).
  • If you want to start a discussion with the author or have a question related to your purchase, please use the support page.