Search 1.9 billion lines of Odoo code on GitHub

odoo-rest-api

Author: Yezileli Ilomo
License: no license
Branch: master
Repository: ChinaShrimp/odoo-rest-api
Dependencies: base
Languages: Markdown (615, 47.5%), Python (671, 51.9%), and XML (8, 0.6%)
Other repositories: HammerSport-Marketing/odoo-rest-api, HarshalBhoir/odoo-rest-api, NCS-75/odoo-rest-api, Netsoro/odoo-rest-api, RizkyEmkka/odoo-rest-api, Zilur/odoo-rest-api, altanmur/odoo-rest-api, anhchuyen/odoo-rest-api, bluemix/odoo-rest-api, chris-lsn/odoo-rest-api, cyberclone8/odoo-rest-api, elliotxin/odoo-rest-api, etiamdotbiz/odoo-rest-api, gandad/odoo-rest-api, harri88/odoo-rest-api, havid0707/odoo-rest-api, hoangpq/odoo-rest-api, jakkritz/odoo-rest-api, jeffery9/odoo-rest-api, loftwah/odoo-rest-api, luss613/odoo-rest-api, mashanz/odoo-rest-api, maxindo/odoo-rest-api, muhammedashraf9244/odoo-rest-api, mwasyou/odoo-rest-api, naibor/odoo-rest-api, popsolutions/odoo-rest-api, rsanhasan/odoo-rest-api, solutionfounder/odoo-rest-api, sygel-technology/odoo-rest-api, ucupers/odoo-rest-api, vic600/odoo-rest-api, wafasa/odoo-rest-api, wbsouza/odoo-rest-api, windedge/odoo-rest-api, yasmanycastillo/odoo-rest-api, and yezyilomo/odoo-rest-api

<h1>Odoo REST API</h1> <p>This is a module which expose Odoo as a REST API </p> <h2>Installing</h2> <ul> <li>Download this module and put it to your Odoo addons directory</li> <li>Install requirements with <code>pip install -r requirements.txt</code></li> </ul> <h2>Getting Started</h2> <h3>Authenticating users</h3> <p>Before making any request make sure to login and obtain session<em>id(This will act as your Authentication token), Send all your requests with session</em>id as a parameter for authentication. There are two ways to obtain <code>session_id</code>, the first one is using <code>/web/session/authenticate/</code> route and the second one is using <code>/auth/</code> route.</p> <ul> <li>Using <code>/web/session/authenticate/</code> route</li> </ul> <p>Send a POST request with JSON body as shown below.</p> <p><code>POST /web/session/authenticate/</code></p> <p>Request Body</p> <p><code>js { &quot;jsonrpc&quot;: &quot;2.0&quot;, &quot;params&quot;: { &quot;login&quot;: &quot;your@email.com&quot;, &quot;password&quot;: &quot;your_password&quot;, &quot;db&quot;: &quot;database_name&quot; } } </code></p> <p>Obtain <code>session_id</code> from a cookie created(Not the one from a response). It&#39;ll be a long string something like &quot;62dd55784cb0b1f69c584f7dc1eea6f587e32570&quot;, Then you can use this as a parameter to all requests.</p> <ul> <li>Using <code>/auth/</code> route</li> </ul> <p>If you have set the default database then you can simply use <code>/auth</code> route to do authentication as </p> <p><code>POST /auth/</code></p> <p>Request Body</p> <p><code>js { &quot;params&quot;: { &quot;login&quot;: &quot;your@email.com&quot;, &quot;password&quot;: &quot;your_password&quot;, &quot;db&quot;: &quot;your_db_name&quot; } } </code></p> <p>Use <code>session_id</code> from the response as a parameter to all requests.</p> <p><strong>Note:</strong> For security reasons, in production don&#39;t send <code>session_id</code> as a parameter, use a cookie instead.</p> <h3>Examples showing how to obtain <code>session_id</code> and use it</h3> <details> <summary> Using <code>/web/session/authenticate/</code> route for authentication </summary> ```py import json import requests import sys AUTH_URL = 'http://localhost:8069/web/session/authenticate/' headers = {'Content-type': 'application/json'} # Remember to configure default db on odoo configuration file(dbfilter = ^db_name$) # Authentication credentials data = { 'params': { 'login': 'your@email.com', 'password': 'yor_password', 'db': 'your_db_name' } } # Authenticate user res = requests.post( AUTH_URL, data=json.dumps(data), headers=headers ) # Get session_id from the response cookies # We are going to use this as our API key session_id = res.cookies.get('session_id', '') # Example 1 # Get users USERS_URL = 'http://localhost:8069/api/res.users/' # Pass session_id for auth # This will take time since it retrives all res.users fields # You can use query param to fetch specific fields params = {'session_id': session_id} res = requests.get( USERS_URL, params=params ) # This will be a very long response since it has many data print(res.text) # Example 2 # Get products(assuming you have products in you db) # Here am using query param to fetch only product id and name(This will be faster) USERS_URL = 'http://localhost:8069/api/product.product/' # Pass session_id for auth params = {'session_id': session_id, 'query': '{id, name}'} res = requests.get( USERS_URL, params=params ) # This will be small since we've retrieved only id and name print(res.text) ``` </details> <details> <summary> Using <code>/auth/</code> route for authentication </summary> ```py import json import requests AUTH_URL = 'http://localhost:8069/auth/' headers = {'Content-type': 'application/json'} # Remember to configure default db on odoo configuration file(dbfilter = ^db_name$) # Authentication credentials data = { 'params': { 'login': 'your@email.com', 'password': 'yor_password', 'db': 'your_db_name' } } # Authenticate user res = requests.post( AUTH_URL, data=json.dumps(data), headers=headers ) # Get session_id from the response # We are going to use this as our API key session_id = json.loads(res.text)['result']['session_id'] # Example 1 # Get users USERS_URL = 'http://localhost:8069/api/res.users/' # Pass session_id for auth # This will take time since it retrives all res.users fields # You can use query param to fetch specific fields params = {'session_id': session_id} res = requests.get( USERS_URL, params=params ) # This will be a very long response since it has many data print(res.text) # Example 2 # Get products(assuming you have products in you db) # Here am using query param to fetch only product id and name(This will be faster) USERS_URL = 'http://localhost:8069/api/product.product/' # Pass session_id for auth params = {'session_id': session_id, 'query': '{id, name}'} res = requests.get( USERS_URL, params=params ) # This will be small since we've retrieved only id and name print(res.text) ``` </details> <details> <summary> Avoiding to send <code>session_id</code> as a parameter for security reasons </summary> When authenticating users, you can use a cookie instead of sending `session_id` as a parameter, this method is recommended in production for security reasons, below is the example showing how to use a cookie. ```py import json import requests import sys AUTH_URL = 'http://localhost:8069/web/session/authenticate/' headers = {'Content-type': 'application/json'} # Remember to configure default db on odoo configuration file(dbfilter = ^db_name$) # Authentication credentials data = { 'params': { 'login': 'your@email.com', 'password': 'yor_password', 'db': 'your_db_name' } } # Authenticate user res = requests.post( AUTH_URL, data=json.dumps(data), headers=headers ) # Get response cookies # We will use this to authenticate user cookies = res.cookies # Example 1 # Get users USERS_URL = 'http://localhost:8069/api/res.users/' # This will take time since it retrives all res.users fields # You can use query param to fetch specific fields res = requests.get( USERS_URL, cookies=cookies # Here we are sending cookies instead of `session_id` ) # This will be a very long response since it has many data print(res.text) # Example 2 # Get products(assuming you have products in you db) # Here am using query param to fetch only product id and name(This will be faster) USERS_URL = 'http://localhost:8069/api/product.product/' # Use query param to fetch only id and name params = {'query': '{id, name}'} res = requests.get( USERS_URL, params=params, cookies=cookies # Here we are sending cookies instead of `session_id` ) # This will be small since we've retrieved only id and name print(res.text) ``` </details> <h2>Allowed HTTP methods</h2> <h2>1. GET</h2> <h3>Model records:</h3> <p><code>GET /api/{model}/</code></p> <h4>Parameters</h4> <ul> <li><strong>query (optional):</strong></li> </ul> <p>This parameter is used to dynamically select fields to include on a response. For example if we want to select <code>id</code> and <code>name</code> fields from <code>res.users</code> model here is how we would do it.</p> <p><code>GET /api/res.users/?query={id, name}</code></p> <p><code>js { &quot;count&quot;: 2, &quot;prev&quot;: null, &quot;current&quot;: 1, &quot;next&quot;: null, &quot;total_pages&quot;: 1, &quot;result&quot;: [ { &quot;id&quot;: 2, &quot;name&quot;: &quot;Administrator&quot; }, { &quot;id&quot;: 6, &quot;name&quot;: &quot;Reconwajenzi&quot; } ] } </code></p> <p>For nested records, for example if we want to select <code>id</code>, <code>name</code> and <code>company_id</code> fields from <code>res.users</code> model, but under <code>company_id</code> we want to select <code>name</code> field only. here is how we would do it.</p> <p><code>GET /api/res.users/?query={id, name, company_id{name}}</code></p> <p><code>js { &quot;count&quot;: 2, &quot;prev&quot;: null, &quot;current&quot;: 1, &quot;next&quot;: null, &quot;total_pages&quot;: 1, &quot;result&quot;: [ { &quot;id&quot;: 2, &quot;name&quot;: &quot;Administrator&quot;, &quot;company_id&quot;: { &quot;name&quot;: &quot;Singo Africa&quot; } }, { &quot;id&quot;: 6, &quot;name&quot;: &quot;Reconwajenzi&quot;, &quot;company_id&quot;: { &quot;name&quot;: &quot;Singo Africa&quot; } } ] } </code></p> <p>For nested iterable records, for example if we want to select <code>id</code>, <code>name</code> and <code>related_products</code> fields from <code>product.template</code> model, but under <code>related_products</code> we want to select <code>name</code> field only. here is how we would do it.</p> <p><code>GET /api/product.template/?query={id, name, related_products{name}</code></p> <p><code>js { &quot;count&quot;: 2, &quot;prev&quot;: null, &quot;current&quot;: 1, &quot;next&quot;: null, &quot;total_pages&quot;: 1, &quot;result&quot;: [ { &quot;id&quot;: 16, &quot;name&quot;: &quot;Alaf Resincot Steel Roof-16&quot;, &quot;related_products&quot;: [ {&quot;name&quot;: &quot;Alloy Steel AISI 4140 Bright Bars - All 5.8 meter longs&quot;}, {&quot;name&quot;: &quot;Test product&quot;} ] }, { &quot;id&quot;: 18, &quot;name&quot;: &quot;Alaf Resincot Steel Roof-43&quot;, &quot;related_products&quot;: [ {&quot;name&quot;: &quot;Alloy Steel AISI 4140 Bright Bars - All 5.8 meter longs&quot;}, {&quot;name&quot;: &quot;Aluminium Sheets &amp; Plates&quot;}, {&quot;name&quot;: &quot;Test product&quot;} ] } ] } </code></p> <p>If you want to fetch all fields except few you can use exclude(-) operator. For example in the case above if we want to fetch all fields except <code>name</code> field, here is how we could do it<br> <code>GET /api/product.template/?query={-name}</code></p> <p><code>js { &quot;count&quot;: 3, &quot;prev&quot;: null, &quot;current&quot;: 1, &quot;next&quot;: null, &quot;total_pages&quot;: 1, &quot;result&quot;: [ { &quot;id&quot;: 1, ... // All fields except name }, { &quot;id&quot;: 2 ... // All fields except name } ... ] } </code></p> <p>There is also a wildcard(*) operator which can be used to fetch all fields, Below is an example which shows how you can fetch all product&#39;s fields but under <code>related_products</code> field get all fields except <code>id</code>.</p> <p><code>GET /api/product.template/?query={*, related_products{-id}}</code></p> <p><code>js { &quot;count&quot;: 3, &quot;prev&quot;: null, &quot;current&quot;: 1, &quot;next&quot;: null, &quot;total_pages&quot;: 1, &quot;result&quot;: [ { &quot;id&quot;: 1, &quot;name&quot;: &quot;Pen&quot;, &quot;related_products&quot;{ &quot;name&quot;: &quot;Pencil&quot;, ... // All fields except id } ... // All fields }, ... ] } </code></p> <p><strong>If you don&#39;t specify query parameter all fields will be returned.</strong></p> <ul> <li><p><strong>filter (optional):</strong></p> <p>This is used to filter out data returned. For example if we want to get all products with id ranging from 60 to 70, here&#39;s how we would do it.</p> <p><code>GET /api/product.template/?query={id, name}&amp;filter=[[&quot;id&quot;, &quot;&gt;&quot;, 60], [&quot;id&quot;, &quot;&lt;&quot;, 70]]</code></p> <p><code>js { &quot;count&quot;: 3, &quot;prev&quot;: null, &quot;current&quot;: 1, &quot;next&quot;: null, &quot;total_pages&quot;: 1, &quot;result&quot;: [ { &quot;id&quot;: 67, &quot;name&quot;: &quot;Crown Paints Economy Superplus Emulsion&quot; }, { &quot;id&quot;: 69, &quot;name&quot;: &quot;Crown Paints Permacote&quot; } ] } </code></p></li> <li><p><strong>page_size (optional) &amp; page (optional):</strong></p> <p>These two allows us to do pagination. Hre page<em>size is used to specify number of records on a single page and page is used to specify the current page. For example if we want our page</em>size to be 5 records and we want to fetch data on page 3 here is how we would do it.</p> <p><code>GET /api/product.template/?query={id, name}&amp;page_size=5&amp;page=3</code></p> <p><code>js { &quot;count&quot;: 5, &quot;prev&quot;: 2, &quot;current&quot;: 3, &quot;next&quot;: 4, &quot;total_pages&quot;: 15, &quot;result&quot;: [ {&quot;id&quot;: 141, &quot;name&quot;: &quot;Borewell Slotting Pipes&quot;}, {&quot;id&quot;: 114, &quot;name&quot;: &quot;Bright Bars&quot;}, {&quot;id&quot;: 128, &quot;name&quot;: &quot;Chain Link Fence&quot;}, {&quot;id&quot;: 111, &quot;name&quot;: &quot;Cold Rolled Sheets - CRCA &amp; GI Sheets&quot;}, {&quot;id&quot;: 62, &quot;name&quot;: &quot;Crown Paints Acrylic Primer/Sealer Undercoat&quot;} ] } </code></p> <p>Note: <code>prev</code>, <code>current</code>, <code>next</code> and <code>total_pages</code> shows the previous page, current page, next page and the total number of pages respectively.</p></li> <li><p><strong>limit (optional):</strong></p> <p>This is used to limit the number of results returned on a request regardless of pagination. For example</p> <p><code>GET /api/product.template/?query={id, name}&amp;limit=3</code></p> <p><code>js { &quot;count&quot;: 3, &quot;prev&quot;: null, &quot;current&quot;: 1, &quot;next&quot;: null, &quot;total_pages&quot;: 1, &quot;result&quot;: [ {&quot;id&quot;: 16, &quot;name&quot;: &quot;Alaf Resincot Steel Roof-16&quot;}, {&quot;id&quot;: 18, &quot;name&quot;: &quot;Alaf Resincot Steel Roof-43&quot;}, {&quot;id&quot;: 95, &quot;name&quot;: &quot;Alaf versatile steel roof&quot;} ] } </code></p></li> </ul> <h3>Model record:</h3> <p><code>GET /api/{model}/{id}</code></p> <h4>Parameters</h4> <ul> <li><p><strong>query (optional):</strong></p> <p>Here query parameter works exactly the same as explained before except it selects fields on a single record. For example</p> <p><code>GET /api/product.template/95/?query={id, name}</code></p> <p><code>js { &quot;id&quot;: 95, &quot;name&quot;: &quot;Alaf versatile steel roof&quot; } </code></p></li> </ul> <h2>2. POST</h2> <p><code>POST /api/{model}/</code></p> <h4>Headers</h4> <ul> <li>Content-Type: application/json #### Parameters </li> <li><p><strong>data (mandatory):</strong></p> <p>This is used to pass data to be posted. For example</p> <p><code>POST /api/product.public.category/</code></p> <p>Request Body</p> <p><code>js { &quot;params&quot;: { &quot;data&quot;: { &quot;name&quot;: &quot;Test category_2&quot; } } } </code></p> <p>Response</p> <p><code>js { &quot;jsonrpc&quot;: &quot;2.0&quot;, &quot;id&quot;: null, &quot;result&quot;: 398 } </code></p> <p>The number on <code>result</code> is the <code>id</code> of the newly created record.</p></li> <li><p><strong>context (optional):</strong></p> <p>This is used to pass any context if it&#39;s needed when creating new record. The format of passing it is</p> <p>Request Body</p> <p><code>js { &quot;params&quot;: { &quot;context&quot;: { &quot;context_1&quot;: &quot;context_1_value&quot;, &quot;context_2&quot;: &quot;context_2_value&quot;, .... }, &quot;data&quot;: { &quot;field_1&quot;: &quot;field_1_value&quot;, &quot;field_2&quot;: &quot;field_2_value&quot;, .... } } } </code></p></li> </ul> <h2>3. PUT</h2> <h3>Model records:</h3> <p><code>PUT /api/{model}/</code></p> <h4>Headers</h4> <ul> <li>Content-Type: application/json #### Parameters</li> <li><p><strong>data (mandatory):</strong></p> <p>This is used to pass data to update, it works with filter parameter, See example below</p></li> <li><p><strong>filter (mandatory):</strong></p> <p>This is used to filter data to update. For example</p> <p><code>PUT /api/product.template/</code></p> <p>Request Body</p> <p><code>js { &quot;params&quot;: { &quot;filter&quot;: [[&quot;id&quot;, &quot;=&quot;, 95]], &quot;data&quot;: { &quot;name&quot;: &quot;Test product&quot; } } } </code></p> <p>Response</p> <p><code>js { &quot;jsonrpc&quot;: &quot;2.0&quot;, &quot;id&quot;: null, &quot;result&quot;: true } </code></p> <p>Note: If the result is true it means success and if false or otherwise it means there was an error during update.</p></li> <li><p><strong>context (optional):</strong> Just like in GET context is used to pass any context associated with record update. The format of passing it is</p> <p>Request Body</p> <p><code>js { &quot;params&quot;: { &quot;context&quot;: { &quot;context_1&quot;: &quot;context_1_value&quot;, &quot;context_2&quot;: &quot;context_2_value&quot;, .... }, &quot;filter&quot;: [[&quot;id&quot;, &quot;=&quot;, 95]], &quot;data&quot;: { &quot;field_1&quot;: &quot;field_1_value&quot;, &quot;field_2&quot;: &quot;field_2_value&quot;, .... } } } </code></p></li> <li><p><strong>operation (optional)</strong>:</p> <p>This is only applied to <code>one2many</code> and <code>many2many</code> fields. The concept is sometimes you might not want to replace all records on either <code>one2many</code> or <code>many2many</code> fields, instead you might want to add other records or remove some records, this is where put operations comes in place. Thre are basically three PUT operations which are push, pop and delete. </p> <ul> <li>push is used to add/append other records to existing linked records</li> <li>pop is used to remove/unlink some records from the record being updated but it doesn&#39;t delete them on the system</li> <li>delete is used to remove/unlink and delete records permanently on the system</li> </ul> <p>For example here is how you would update <code>related_product_ids</code> which is <code>many2many</code> field with PUT operations</p> <p><code>PUT /api/product.template/</code></p> <p>Request Body</p> <p><code>js { &quot;params&quot;: { &quot;filter&quot;: [[&quot;id&quot;, &quot;=&quot;, 95]], &quot;data&quot;: { &quot;related_product_ids&quot;: { &quot;push&quot;: [102, 30], &quot;pop&quot;: [45], &quot;delete&quot;: [55] } } } } </code></p> <p>This will append product with ids 102 and 30 as related products to product with id 95 and from there unlink product with id 45 and again unlink product with id 55 and delete it from the system. So if befor this request product with id 95 had [20, 37, 45, 55] as related product ids, after this request it will be [20, 37, 102, 30].</p> <p>Note: You can use one operation or two or all three at a time depending on what you want to update on your field. If you dont use these operations on <code>one2many</code> and <code>many2many</code> fields, existing values will be replaced by new values passed, so you need to be very carefull on this part.</p> <p>Response:</p> <p><code>js { &quot;jsonrpc&quot;: &quot;2.0&quot;, &quot;id&quot;: null, &quot;result&quot;: true } </code></p></li> </ul> <h3>Model record:</h3> <p><code>PUT /api/{model}/{id}</code></p> <h4>Headers</h4> <ul> <li>Content-Type: application/json #### Parameters</li> <li>data (mandatory)</li> <li>context (optional)</li> <li>PUT operation(push, pop, delete) (optional)</li> </ul> <p>All parameters works the same as explained on previous section, what changes is that here they apply to a single record being updated and we don&#39;t have filter parameter because <code>id</code> of record to be updated is passed on URL as <code>{id}</code>. Example to give us an idea of how this works.</p> <p><code>PUT /api/product.template/95/</code></p> <p>Request Body</p> <p><code>js { &quot;params&quot;: { &quot;data&quot;: { &quot;related_product_ids&quot;: { &quot;push&quot;: [102, 30], &quot;pop&quot;: [45], &quot;delete&quot;: [55] } } } } </code></p> <h2>4. DELETE</h2> <h3>Model records:</h3> <p><code>DELETE /api/{model}/</code></p> <h4>Parameters</h4> <ul> <li><p><strong>filter (mandatory):</strong></p> <p>This is used to filter data to delete. For example</p> <p><code>DELETE /api/product.template/?filter=[[&quot;id&quot;, &quot;=&quot;, 95]]</code></p> <p>Response</p> <p><code>js { &quot;result&quot;: true } </code></p> <p>Note: If the result is true it means success and if false or otherwise it means there was an error during deletion.</p></li> </ul> <h3>Model records:</h3> <p><code>DELETE /api/{model}/{id}</code></p> <h4>Parameters</h4> <p>This takes no parameter and we don&#39;t have filter parameter because <code>id</code> of record to be deleted is passed on URL as <code>{id}</code>. Example to give us an idea of how this works.</p> <p><code>DELETE /api/product.template/95/</code></p> <p>Response</p> <p><code>js { &quot;result&quot;: true } </code></p> <h2>Calling Model&#39;s Function</h2> <p>Sometimes you might need to call model&#39;s function or a function bound to a record, inorder to do so, send a <code>POST</code> request with a body containing arguments(args) and keyword arguments(kwargs) required by the function you want to call.</p> <p>Below is how you can call model&#39;s function</p> <p><code>POST /object/{model}/{function name}</code></p> <p>Request Body</p> <p><code>js { &quot;params&quot;: { &quot;args&quot;: [arg1, arg2, ..], &quot;kwargs &quot;: { &quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;, ... } } } </code></p> <p>And below is how you can call a function bound to a record</p> <p><code>POST /object/{model}/{record_id}/{function name}</code></p> <p>Request Body</p> <p><code>js { &quot;params&quot;: { &quot;args&quot;: [arg1, arg2, ..], &quot;kwargs &quot;: { &quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;, ... } } } </code></p> <p>In both cases the response will be the result returned by the function called</p>