Odoo Generic API
Useful for integration with third-party applications.
Provide access to Odoo's models through REST APIs through a JWT authentication method
Fisrt...
You need to install: PyJWT.
pip install PyJWT==1.7.1
You need to install: PyJWT.
pip install PyJWT==1.7.1
Secure authentication trough Json Web Tokens...
{server-url}/auth
For example:
{server-url}/auth
For example:
curl -X GET \ http://localhost:8069/auth \ -H 'password: admin' \ -H 'user: admin'Return a secure token that will be used in the subsequent api calls...
{ "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1Nzc1NTMwOTIsInN1YiI6eyJuYW1lIjoiQWRtaW5pc3RyYXRvciIsImlkIjoxfSwiZXhwIjoxNTc3NTU2NjkyfQ.DPgVvx_wm0q252NQS8nq4VmwjcSY2v7G2fn3ZN3ee9M" }
(Search) - Using API to get resources
[HTTP method GET]: To obtain the resources list of a given model use:
{server-url}/api/{model_name}
Also, you can define the limit, offset, count and args as if you are working with the ORM.
For example:
{server-url}/api/{model_name}
Also, you can define the limit, offset, count and args as if you are working with the ORM.
For example:
$ sudo apt-get install httpie $ printf '{}'| http --follow --timeout 3600 GET localhost:8069/api/hr.employee 'limit'=='10' 'args'=='[("id", "like", "2")]' 'offset'=='0' 'count'=='0' 'order'=='"id DESC"' 'fields'=='["id", "name", "work_email", ("country_id", ["id", "name"]), ["child_ids", ["id", "name", ("title", ["id", "name", "shortcut"]), "work_email", "work_phone", "notes"]]]' \ Content-Type:'application/json' \ Authorization:'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MDA3MzMwODAsImlhdCI6MTYwMDcyOTQ4MCwic3ViIjp7Im5hbWUiOiJNaXRjaGVsbCBBZG1pbiIsImlkIjoyfX0.4nBNlinxZEW5_5etr7Se_0g2ZdigHan8xQ4854OG8MY'
You will obtain something like:
{ "id": null, "jsonrpc": "2.0", "result": { "data": [ { "child_ids": [], "country_id": {}, "id": 20, "name": "Abigail Peterson", "work_email": "abigail.peterson39@example.com" }, { "child_ids": [], "country_id": {}, "id": 12, "name": "Tina Williamson", "work_email": "tina.williamson98@example.com" }, { "child_ids": [ { "id": 3, "name": "Anita Oliver", "notes": false, "title": {}, "work_email": "anita.oliver32@example.com", "work_phone": "(538)-497-4804" }, { "id": 6, "name": "Jennie Fletcher", "notes": false, "title": {}, "work_email": "jennie.fletcher76@example.com", "work_phone": "(157)-363-8229" }, { "id": 8, "name": "Keith Byrd", "notes": false, "title": {}, "work_email": "keith.byrd52@example.com", "work_phone": "(449)-505-5146" }, { "id": 5, "name": "Randall Lewis", "notes": false, "title": {}, "work_email": "randall.lewis74@example.com", "work_phone": "(332)-775-6660" }, { "id": 4, "name": "Sharlene Rhodes", "notes": false, "title": {}, "work_email": "sharlene.rhodes49@example.comcom", "work_phone": "(450)-719-4182" } ], "country_id": {}, "id": 2, "name": "Ronnie Hart", "work_email": "ronnie.hart87@example.com" } ], "status_code": 200 } }
As you could notice, you can define the fields you wish to obtain, by means the parameter "fields" added on the request.
basics fields | ['id', 'name', 'street'] |
Many2one | ['id', 'name', 'street', ('country_id', ['id', 'name'])] |
Many2many & One2many | ['id', 'name', ('country_id', ['id', 'name']), ['child_ids', ['id', 'name', ('title', ['id', 'name', 'shortcut']), 'work_email', 'work_phone', 'notes']]] |
(Creation) - Using API to create resources
[HTTP method POST]: To create a resource of a given model use:
{server-url}/api/{model_name}
For example: http://localhost:8069/api/res.partner
{server-url}/api/{model_name}
For example: http://localhost:8069/api/res.partner
If the model don't exist or the module that it belong isn't installed. You will obtain:
Suppose you need to create a "hr.employee" record but you need to define by creation its country (Many2one => res.country) and its categories (One2many => category_ids).
The way you can do that is using the following data:
Example...
{ "jsonrpc": "2.0", "id": null, "result": { "status_code": 406, "error": "Wrong model! Or module not registered!" } }You will have the possibility to create simple objects, but you can also create objects with internal relationships (in other words with related fields Many2one, Many2many and One2many).
Suppose you need to create a "hr.employee" record but you need to define by creation its country (Many2one => res.country) and its categories (One2many => category_ids).
The way you can do that is using the following data:
{ "name": "Alejandro Cora Glez", "mobile_phone": "+5354137760", "notes": "Testing employee creation...", "identification_id": "12341115678", "passport_id": "JXXXXXXX", "work_location": "Central Park", "work_email": "alek.cora.glez@gmail.com", "work_phone": "+5858355581", "gender": "male", "marital": "married", "birthday": "07/09/1985", "category_ids": [ 1, {"name": "Some New Category"} ], "country_id": { "name": "NewCountry" }, "job_id": { "name": "Data Architect" } }
Example...
curl -X POST \ http://localhost:8069/api/hr.employee/ \ -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1Nzc1NTMwOTIsInN1YiI6eyJuYW1lIjoiQWRtaW5pc3RyYXRvciIsImlkIjoxfSwiZXhwIjoxNTc3NTU2NjkyfQ.DPgVvx_wm0q252NQS8nq4VmwjcSY2v7G2fn3ZN3ee9M' \ -H 'Content-Type: application/json' \ -d '{ "name": "Alejandro Cora Glez", "mobile_phone": "+5354137760", "notes": "Testing employee creation...", "identification_id": "12341115678", "passport_id": "JXXXXXXX", "work_location": "Central Park", "work_email": "alek.cora.glez@gmail.com", "work_phone": "+5858355581", "gender": "male", "marital": "married", "birthday": "07/09/1985", "category_ids": [ 1, {"name": "Some New Category"} ], "country_id": { "name": "NewCountry" }, "job_id": { "name": "Data Architect" } }'
You will obtain, that indicate the id of the created resource:
{ "jsonrpc": "2.0", "id": null, "result": { "status_code": 201, "data": { "id": 36 } } }
Other details about relational fields. Relational fields could be created only once, the second time, an error will be raised!
Many2one | If you know the id of the resource use it. ("country_id": 50 instead "country_id": {"name": "Country Test"}) |
Many2many & One2many | You can use both: resource id or resource creation in place. (Remember this is a list of elements). "child_ids": [3, {"name": "Contact 1", "phone": "+000000111"}], |
(Update) - Using API to update resources
[HTTP method PUT]: To update resources of a given model and specific id use:
{server-url}/api/{model_name}/{resource_id}
For example: http://localhost:8069/api/hr.employee/35
{server-url}/api/{model_name}/{resource_id}
For example: http://localhost:8069/api/hr.employee/35
Example...
curl -X PUT \ http://localhost:8069/api/hr.employee/36 \ -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1Nzc1NDYwMjYsInN1YiI6eyJuYW1lIjoiQWRtaW5pc3RyYXRvciIsImlkIjoxfSwiZXhwIjoxNTc3NTQ5NjI2fQ.wBfOYNsdY0fSchS0hEMUohUbZlJqL5wfoKlGHBmp3QU' \ -H 'Content-Type: application/json' \ -d '{ "name": "Alek Cora Glez 1", "category_ids": [2], "job_id": { "name": "Data Architect", } }'
You will obtain, that indicate the id of the updated resource:
{ "jsonrpc": "2.0", "id": null, "result": { "status_code": 200, "data": { "id": 36 } } }
(Deletion) - Using API to delete resources
[HTTP method DELETE]: To delete a resource of a given model and specific id use:
{server-url}/api/{model_name}/{resource_id}
For example: http://localhost:8069/api/hr.employee/36
{server-url}/api/{model_name}/{resource_id}
For example: http://localhost:8069/api/hr.employee/36
[HTTP method GET]: To obtain the resources list of a given model use:
{server-url}/api/{model_name}
For example:
{server-url}/api/{model_name}
For example:
curl -X DELETE \ http://localhost:8069/api/hr.employee/36 \ -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1Nzc1NTczODUsInN1YiI6eyJuYW1lIjoiQWRtaW5pc3RyYXRvciIsImlkIjoxfSwiZXhwIjoxNTc3NTYwOTg1fQ.sBsK5zB1Me2iIM04w-LjBCCf3pPzDovzBGES7-eIKhY' \ -H 'Content-Type: application/json' \ -d '{}'
You will obtain something like:
{ "jsonrpc": "2.0", "id": null, "result": { "status_code": 200, "data": "OK" } }
Alejandro Cora Gonzalez
alek.cora.glez@gmail.com
Odoo Proprietary License v1.0 This software and associated files (the "Software") may only be used (executed, modified, executed after modifications) if you have purchased a valid license from the authors, typically via Odoo Apps, or if you have received a written agreement from the authors of the Software (see the COPYRIGHT file). You may develop Odoo modules that use the Software as a library (typically by depending on it, importing it and using its resources), but without copying any source code or material from the Software. You may distribute those modules under the license of your choice, provided that this license is compatible with the terms of the Odoo Proprietary License (For example: LGPL, MIT, or proprietary licenses similar to this one). It is forbidden to publish, distribute, sublicense, or sell copies of the Software or modified copies of the Software. The above copyright notice and this permission notice must be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Please log in to comment on this module
Report comment
Any abuse of this reporting system will be penalizedThere are no ratings yet!
Migrated to Odoo14
Interesting module
Do you plan to release a version for Odoo 14?
Re: Interesting module
Yes. I will do it!