By default, the webservice feature is disabled on PrestaShop and needs to be switched on before the first use.
Go in the PrestaShop back office, open the “Web service” page under the “Advanced Parameters” menu, and then choose “Yes” for the “Enable PrestaShop Webservice” option.
The Webservice switch is stored in the configuration table of PrestaShop.
In order to enable the webservice API:
<?php
Configuration::updateValue('PS_WEBSERVICE', 1);
With the equivalent for disabling it:
<?php
Configuration::updateValue('PS_WEBSERVICE', 0);
In order to have access to the API you need to create an access key, this will allow you to finely tune the permissions you give to the different data of your shop. You will use this key later in each call to the API. Open the “Webservice” page under the “Advanced Parameters” menu, and then click the “Add new webservice key” button to access the account configuration section.
Each access key is defined by this information:
Creating API keys can be done with the class WebserviceKey.
<?php
$apiAccess = new WebserviceKey();
$apiAccess->key = 'GENERATE_A_COMPLEX_VALUE_WITH_32_CHARACTERS';
$apiAccess->save();
This first code allows you to pass the authentication layer. You also need access to the resources you expect to use.
We need the Api account ID in order to grant it access, and an array having the resource name as key and the array of methods allowed as value.
The available resources can be found in WebserviceRequest::getResources()
(link to definition).
For instance is we want to give all permissions for customers and orders resources for the account we previously created:
<?php
$permissions = [
'customers' => ['GET' => 1, 'POST' => 1, 'PUT' => 1, 'PATCH' => 1, 'DELETE' => 1, 'HEAD' => 1],
'orders' => ['GET' => 1, 'POST' => 1, 'PUT' => 1, 'PATCH' => 1, 'DELETE' => 1, 'HEAD' => 1],
];
WebserviceKey::setPermissionForAccount($apiAccess->id, $permissions);
Note only the key in the nested arrays of permissions are important. We set 1
as a value in this example but it can be anything.