How to Use REST API ?

The Akeneo PIM comes with a REST API which allows to fetch product data in JSON format.

We’re still working on to add new methods to fetch and update other entities.

Configure a user

We first need to generate an API key for a PIM user as shown on following screenshot :

../../_images/configure-rest.png

Retrieve the product data

Then we can write a simple script to retrieve some product data :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
$username = "admin";
$apiKey   = "79fadafa61723d34582f591586d63cee33199216";
$salt     = "2lmnle11aeucsgc0cw0go0o0gcow8sw";

$nonce   = uniqid();
$created = date('c');

$digest  = base64_encode(sha1(base64_decode($nonce) . $created . $apiKey.'{'.$salt.'}', true));

$headers = array();
$headers[] = 'CONTENT_TYPE: application/json';
$headers[] = 'Authorization: WSSE profile="UsernameToken"';
$headers[] =
    sprintf(
        'X-WSSE: UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
        $username,
        $digest,
        $nonce,
        $created
    );

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://pim-dev.local/api/rest/products/sku-000.json');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);

echo "HTTP Return code:".$httpStatus."\n";

if (false === $result) {
    echo "ERROR:".curl_error($ch)."\n";
} else {
    echo "RESULT:$result\n";
}

The script can be executed with php-cli :

php rest-api-product.php

The response has the following format :

{
    "family":"mug",
    "groups":[],
    "categories":["desktops"],
    "enabled":true,
    "associations":{
        "X_SELL":{"products":["sku-001","sku-002","sku-003","sku-004"]}
    },
    "values":{
        "sku":[{"locale":null,"scope":null,"value":"sku-000"}],
        "color":[{"locale":"en_US","scope":null,"value":["Red","Blue","Purple"]},{"locale":"fr_FR","scope":null,"value":[]}],
        "manufacturer":[{"locale":"en_US","scope":null,"value":"MyMug"},{"locale":"fr_FR","scope":null,"value":null}],
        "name":[{"locale":"en_US","scope":null,"value":"Iusto ea sint."},{"locale":"fr_FR","scope":null,"value":"Neque eveniet quasi accusantium."}],
        "price":[{"locale":"en_US","scope":null,"value":[{"data":"43.21","currency":"EUR"},{"data":"19.70","currency":"USD"}]},{"locale":"fr_FR","scope":null,"value":[{"data":null,"currency":"EUR"},{"data":null,"currency":"USD"}]}],
        "release_date":[{"locale":null,"scope":null,"value":"2012-12-27"}],
        "weight":[{"locale":"en_US","scope":null,"value":{"data":"360.0000","unit":"GRAM"}},{"locale":"fr_FR","scope":null,"value":null}],
        "height":[{"locale":null,"scope":null,"value":"21.0000"}],
        "short_description":[{"locale":"en_US","scope":"ecommerce","value":null},{"locale":"fr_FR","scope":"ecommerce","value":null},{"locale":"en_US","scope":"mobile","value":null},{"locale":"fr_FR","scope":"mobile","value":null}]
    },
    "resource":"http:\/\/pim-dev.local\/api\/rest\/products\/sku-000"
}