Module Web Services API REST (developer): Difference between revisions

m
Tag: 2017 source edit
Tag: 2017 source edit
 
(11 intermediate revisions by 3 users not shown)
Line 3: Line 3:
     Links below will be automatically replicated on translated pages by PolyglotBot -->
     Links below will be automatically replicated on translated pages by PolyglotBot -->
[[fr:Module_Web_Services_API_REST_(développeur)]]
[[fr:Module_Web_Services_API_REST_(développeur)]]
[[ja:Module_Web_Services_API_REST_(developer)_JA]]
[[zh:提供REST_API的Web服务模块(开发者)]]
<!-- END interlang links -->
<!-- END interlang links -->


Line 13: Line 15:
devdoc=This page|
devdoc=This page|
userdoc=Not applicable|}}
userdoc=Not applicable|}}


=Function=
=Function=
Line 105: Line 100:
For example, you can try the explorer on the demo instance at:
For example, you can try the explorer on the demo instance at:


'''https://demo.dolibarr.org/api/index.php/explorer'''
'''https://preview.dolibarr.org/api/index.php/explorer'''
 
The swagger file to describe all available API services can be found at:
 
'''https://preview.dolibarr.org/api/index.php/explorer/swagger.json?DOLAPIKEY=demokey'''




Line 122: Line 121:
To use the REST API,  you have to call an url such as this one : <nowiki>http://<my_server>/api/index.php/<action></nowiki><br />
To use the REST API,  you have to call an url such as this one : <nowiki>http://<my_server>/api/index.php/<action></nowiki><br />
with one of the 4 following methods : GET, POST, PUT, DELETE, replacing <action> by the action you want to use. Ex : <nowiki>http://<my_server>/api/index.php/invoices</nowiki>
with one of the 4 following methods : GET, POST, PUT, DELETE, replacing <action> by the action you want to use. Ex : <nowiki>http://<my_server>/api/index.php/invoices</nowiki>
Before using any API, you must first get a custom API-key. You have to create a new user and generate or set its "Key for API".
[[File:Dolibarr user for api.png|400px]]
You will have to use this API key in any of your client program that need to call a Dolibarr API. The API key must be provided as a HTTP header "DOLAPIKEY: apikey".
If you are using the multicompany module, you can also force the API to be executed as if you were logged into a given entity by adding the HTTP header "DOLAPIENTITY: idOfEntity"


==Client code example==
==Client code example==
Line 398: Line 407:
"email" => "customer company email",
"email" => "customer company email",
"client" => "1",
"client" => "1",
"code_client" => "-1"
"code_client" => "auto"
];
];
$newClientResult = CallAPI("POST", $apiKey, $apiUrl."thirdparties", json_encode($newClient));
$newClientResult = CallAPI("POST", $apiKey, $apiUrl."thirdparties", json_encode($newClient));
Line 404: Line 413:
$clientDoliId = $newClientResult;
$clientDoliId = $newClientResult;
</syntaxhighlight>
</syntaxhighlight>
Comments :


*client => 1 He is a customer (not a supplier)
Comments:
*code_client => -1 so the customer code will be generated automatically.
*client => 1, he is a customer.
*code_client => 'auto' so the customer code will be generated automatically.
*we get the customer ID in $clientDoliId
*we get the customer ID in $clientDoliId


=Examples with Python=
=Examples with Python=
See https://pypi.org/project/dolipy/ and https://pypi.org/project/dolibarr/
See https://pypi.org/project/dolipy/ (2021) or https://pypi.org/project/dolibarr/ (2019) or https://pypi.org/project/dolibarrpy/ (2024)


First you can install the python-module with "pip install dolibarr".
First you can install the python-module with "pip install dolibarr" or pypi install dolibarrpy


<br />
<syntaxhighlight lang="python">
  from dolibarr import Dolibarr
  from dolibarr import Dolibarr


Line 439: Line 448:
  product_dict = dolibarr_inst.call_list_api('products')
  product_dict = dolibarr_inst.call_list_api('products')
  print(product_dict)
  print(product_dict)
</syntaxhighlight>


=Develop your own service / API=
=Develop your own service / API=
Adding a new REST service is as easy than adding a file called '''api_mymoduleobject.class.php''' into the directory '''htdocs/mymodule/class'''.
Adding a new REST service is as easy than adding a file called '''api_mymoduleobject.class.php''' into the directory '''htdocs/mymodule/class'''.
You take the example into '''htdocs/commande/class/api_orders.class.php'''
You can take an example into '''htdocs/commande/class/api_orders.class.php'''


Note that if you use the '''modulebuilder''' tool, the file may be generated for you.
Note that if you use the '''modulebuilder''' tool, this file may be generated for you.


The framework detects automatically the API and it should be visible into the explorer.
The framework detects automatically the API files and it should be visible into the explorer.


Method and parameters are detected according to introspection done into PHP class of the object ('''htdocs/mymodule/class/object.class.php''') using the '''annotations''' found into the class.
Method and parameters are detected according to introspection done into PHP class of the object ('''htdocs/mymodule/class/object.class.php''') using the '''annotations''' found into the class.
Line 453: Line 463:
These are just the basics.
These are just the basics.


Some more informations in the Dolibarr code. You can look here : htdocs/<directory>/class/api_xxx_class.php<br />
To find all existing API files in Dolibarr, you can search all files htdocs/<directory>/class/api_xxx_class.php
Ex : htdocs/societe/class/api_thirdparties.class.php for thirdparties.<br />
Ex : htdocs/societe/class/api_contacts.class.php for contacts.<br />
Ex : htdocs/compta/facture/class/api_invoice_class.php for invoices.<br />
Ex : htdocs/mrp/class/api_mos_class.php for manufacturing orders.<br />
Ex : htdocs/product/class/api_products.class.php for contacts.<br />