#How to retrieve PIM structure

Retrieve the PIM structure through a channel resource. This is usually the required step before starting to read the PIM catalog data.

Use case:
App Workflow
PIM Features:
Channel
REST API endpoint(s):
channel
If you're starting building your App, make sure you previously followed:

#Context

The channel resource holds the basic PIM structure data.

relationship schema

Get the big picture here.

#Fetch the PIM structure

#0. Initialization


    function buildApiClient(): GuzzleHttp\Client
    {
        $pimUrl = '<PIM_URL>';
        $appToken = '<APP_TOKEN>'; // Token provided during oauth steps
    
        // If you haven't done it yet,
        // please follow the Guzzle official documentation to install the client
        // https://docs.guzzlephp.org/en/stable/overview.html#installation
    
        return new GuzzleHttp\Client([
            'base_uri' => $pimUrl,
            'headers' => ['Authorization' => 'Bearer ' . $appToken],
        ]);
    }
    

    // Install the node-fetch library by following the official documentation:
    // https://www.npmjs.com/package/node-fetch
    
    import fetch from 'node-fetch';
    
    // Set your client for querying Akeneo API as follows
    async function get(url, accessToken) {
        return await fetch(url, {
            headers: {
                'Authorization': `Bearer ${accessToken}`
            }
        });
    }
    

#1. Get the PIM structure by fetching a channel from API

Workflow

synchronisation steps

Collect channel from PIM API:


    $client = buildApiClient();
    
    const API_URL = '/api/rest/v1/channels/%s';
    
    // Your own channel
    $channelCode = 'ecommerce';
    
    // Make an authenticated call to the API
    $response = $client->get(
        sprintf(API_URL, $channelCode)
    );
    
    // Convert json response to array
    $channel = json_decode($response->getBody()->getContents(), true);
    

    const pimUrl = 'https://url-of-your-pim.com';
    const accessToken = 'your_app_token'; // Token provided during oAuth steps
    
    const channelCode = 'ecommerce';
    const apiUrl = `${pimUrl}/api/rest/v1/channels/${channelCode}`;
    
    const response = await get(apiUrl, accessToken);
    
    const channel = await response.json();
    

The retrieved channel resource looks like this:


    var_export($channel);
    
    // Output
    [
        'code' => 'ecommerce',
        'currencies' => [
            'USD',
            'EUR',
        ],
        'locales' => [
            'de_DE',
            'en_US',
            'fr_FR',
        ],
        'category_tree' => 'master',
        'conversion_units' => [],
        'labels' => [
            'en_US' => 'Ecommerce',
            'de_DE' => 'Ecommerce',
            'fr_FR' => 'Ecommerce',
        ],
    ]
    

    console.log(channel);
    
    // Output
    {
        code: 'ecommerce',
        currencies: [
            'USD',
            'EUR'
        ],
        locales: [
            'de_DE',
            'en_US',
            'fr_FR'
        ],
        category_tree: 'master',
        conversion_units: {},
        labels: {
            en_US: 'Ecommerce',
            de_DE: 'Ecommerce',
            fr_FR: 'Ecommerce'
        }
    }
    

#2. Store the PIM structure

We advise storing in your App locales, currencies, and root category.

  • Locales will allow filtering many localizable resource labels, and parsing product attribute values,
  • Currencies will be used to parse product values price attribute type,
  • Root category property will allow retrieving the whole category tree linked to the channel.

    storeCurrencies($channel['currencies']);
    storeLocales($channel['locales']);
    storeCategoryTree($channel['category_tree']);
    

    storeCurrencies(channel.currencies);
    storeLocales(channel.locales);
    storeCategoryTree(channel.category_tree);
    
Next Step
Well done! Keep digging into the “App workflow” and follow the next tutorial!