Table of contents
Web service one-page documentation
This chapter aims at grouping all the information from the web service tutorial into a handy one-page doc that you can print and keep at hand.
Setting everything up
In order for you to view, edit and delete the data on your PrestaShop store through its web service, you first need to enable the web service feature, and then create an access key.
Configure your PHP installation so that it has the cURL extension installed and enabled:
With Windows, place this line in your
With Linux/Mac, install the cURL extension:
Enabling the webservice feature
Go in the PrestaShop back office, open the "Webservice" page under the "Advanced Parameters" menu, and then choose "Yes" for the "Enable PrestaShop's webservice". Save your change: you're done!
Creating an access key
Open the "Webservice" page under the "Advanced Parameters" menu, and then click the "Add New" button to access the account configuration section. A long form appears:
- Key. The API key serves as the main identifier for the webservice account you are creating. Click the "Generate" button to get an unique authentication key. You can also create your own (which must be 32 characters long), but using a generated key prevents wrong-doers from guessing your key too easily.
Using this key, you and other selected users will be able to access the webservice.
- Key description. Helps you remember who you created that key for, what are the access rights assigned to it, etc. The description is not public, but make sure to put all the keywords pertaining to the user, so that you can find their key more quickly.
- Status. You can disable any key at any time.
- Permissions. This section is very important, as it enables you to assign rights for each resource you want to make available to this key. Indeed, you might want a user to have read and write access on some resources, but only read access on others – and no access to the more important ones.
In the list of permissions, the checkbox most on the left enables you to define all the rights for a given resource. Likewise, the checkbox at the top of each column enables you to give the select right (View, Modify, etc.) to all the resources.
Make sure to only select the rights needed for the usage of that key. Do not give all the rights for all resources to any key, keep that to yours and yours only.
- Shop association. This only appears in multistore mode. It enables you to choose which of your stores the key owner should have access to.
If you choose to use a custom passkey instead of a generated one, make sure it is very secure and that its rights are limited – and that it is 32characters long!
Accessing the webservice from the browser
The endpoint to your store's webservice is located in the
/api/ folder at the root of your installation of Prestashop:
- If PrestaShop is installed at the root of your server, you can access the API here: http://example.com/api/
- If PrestaShop is installed in a subfolder of your server, you can access the API here: http://example.com/prestashop/api/
To access it, you need to provide your API key when request. There is no password, providing your API key is enough – and therefore the key should be kept secret by the user!
You can either type the API endpoint address directly then enter your API key, or indicate your API key in the address. Here is an example, with "UCCLLQ9N2ARSHWCXLT74KUKSSK34BFKX" being the API key.
- At the root of the server: http://[email protected]/api/
- In a subfolder of the server: http://[email protected]/prestasshop/api/
You can test this with any browser that supports XML.
If no permission has been set for the key, then the browser will keep asking you to enter the key indefinitely.
If you cannot access some resources, the API might answer with this XML response:
Using XLink, you will then be able to access your various resources. XLink associates an XML file to another XML file via a link.
Accessing the webservice use our PHP library
You will need the latest version of the
PSWebServiceLibrary.php file, which can be found on our code repository: https://github.com/PrestaShop/PrestaShop-webservice-lib/blob/master/PSWebServiceLibrary.php
To download the file:
- Click here to view the raw file: https://raw.github.com/PrestaShop/PrestaShop-webservice-lib/master/PSWebServiceLibrary.php
- Copy/paste the file into an empty text local file, using for instance Notepad.
- Save the file as
You can also directly download a zip archive of all the files in this repository, including the example files, by clicking here: https://github.com/PrestaShop/PrestaShop-webservice-lib/archive/master.zip
PSWebServiceLibrary.php at the root of your web server, and add this line at the beginning of any PHP file that needs to use your store's webservice.
Then, you must create an instance of the
PrestaShopWebservice object, which takes 3 parameters in its constructor:
- The store's root path (ex: http://example.com/ ).
- The authentication key (ex: UCCLLQ9N2ARSHWCXLT74KUKSSK34BFKX).
- A boolean value, indicating whether the webservice must use its debug mode.
Here's how you create a webservice call:
It is essential that you understand how to handle errors with the webservice library. By implementing error-catch method early, you will more easily detect issues, and be able to correct them on the go.
Error handling with the webservice library is done using PHP exceptions. If you do not know about them, you should read http://php.net/manual/en/language.exceptions.php, as exceptions are an essential part of good coding practice.
How it works
The error handling is done within a
try..catch block, with the webservice processing being done in the try section, the catch one containing the error handling code.
That means each creation or use of the library must be located within a "try" block. The "catch" block can then handle the error if it occurs during the execution of the try block.
Now we'll see how to list all customers via the webservice, and then we will see the four CRUD methods.
In the following code sample, we want to get the list of all customers:
- In the try block, we instantiate the
PrestaShopWebserviceobject with the necessary parameters and retrieve the customer resource XML in a variable.
- In the catch block, we put code to display the PHP error message, if anything wrong happens in the try block.
All the example files can be found on our code repository: https://github.com/PrestaShop/PrestaShop-webservice-lib/tree/master/examples
Working with your data
As shown above, you have to instantiate the
PrestaShopWebservice object in order to use its methods:
You can also directly use call the REST API and get XML results, as this correspondence table shows:
Viewing your data
Using the PrestaShopWebservice methods
Let's see how to view a full list of client IDs. As you can see from the table above, we need the
get() method in order to retrieve a XML file containing all the customers. The parameter has to be a key-value array, where we define the resource we want:
The value defines the resource that the webservice will use in a future call. The value could be carrier types, countries or any other type of resource that can be found in the "Available resources" section of this guide.
In order to retrieve a list of customers in XML format, you only need to instantiate
PrestaShopWebservice (a seen earlier) and use this code:
Launching the code above will return a XML object containing all the current customer IDs on the store:
With this information, you can access the customer that interests you by using the value in the
As you can see in the result in the previous section, the data returned by calling
$webService->get() puts you at the root of the XML document, in the context you requested.
To access the fields of clients who are children of the
<customers> tag, we only need to retrieve all fields in an associative array in XML, like this:
From there, you can easily access client IDs. Here's an example with a path from identifiers:
Thanks to these elements, you can create a HTML table containing all the client IDs – or anything that displays this information in your interface.
Using HTTP requests
If you would rather not use the
PrestaShopWebservice object, note that PrestaShop's webservice is RESTful, in that you can work with its data using the known HTTP verbs just as easily as you would use the methods of the
Viewing your data follows the same rule, but with a HTTP GET request on the same URL:
To view your data you simply have to launch an HTTP GET request on the following URL:
The resulting XML document is the same as the one obtained using
Creating and adding
Creating and adding data takes a bit more work. Indeed, in order to create a new entry or edit an existing one, you need to send a fully formed and complete XML document, whether you use
PrestaShopWebservice or the HTTP methods.
When you need to create a new entry, the API can help you by providing a blank schema for any resource, or even a synopsis with indications of the meaning for each tag:
When a client is created from within PrestaShop's administration interface, a confirmation e-mail is sent to the client. This cannot be done directly with the webservice: there is no way to trigger the sending of that confirmation e-mail.
However, you can create an override file for the
Customer class and override the
addWs() method. This method is similar to
ObjectModel::add() but is only called from the webservice. You can find examples of its use in the
The RESTful-ness of the API goes all the way: in order to delete the product with an ID of 12, you simply have to launch an HTTP DELETE request on the following URL:
/api/ URL gives you the root of all the resources, in the form of an XML file.
Here it is, extremely simplified. This list is current as of version 22.214.171.124 of PrestaShop. Note that we only show the API resources available for one of the available stores.
As with any XLink-bearing XML file, each resource element leads to more resources, linked from the
In addition, the element contains a description of the resource, and two schemas: a blank one, which you can use to create a new item, and a synopsis one, which is just like the blank one but with a detailed description of what type of data is expected in each element.
Here is an extract of the Customer blank schema:
Here is an extract of the Customer synopsis schema:
The value types can be found in the webservice reference: http://doc.prestashop.com/display/PS15/Web+service+reference.