Catalog

Get list of products

To get the products you'll need to use the slyk.product.list method of the SDK, like this:

const products = await slyk.product.list({
  page: { number: 1, size: 10 },
  filter: { available: 'true', visible: 'true' },
  sort: [{ name: '-updatedAt' }],
  include: 'gallery',
});

This methods returns a promise that resolves with an object that contains two properties: results and total, being the list of products and the total number of products for the given filters.

There are, however a few things to take into consideration with this method:

  1. Pagination

  2. Filters

  3. Includes

Pagination

The slyk.product.list method is paginated, meaning it will give you a subset of the product list, dependening on the pagination options you pass.

By passing a page option you can specify the page you want to receive. This is an object with a number and a size properties, that represent the page number and the page size, respectively.

const products = await slyk.product.list({
  page: {
    number: 2,
    size: 10,
  },
  // Other options...
});

When passing a page, the total on the resolved object will tell you how many products exist that match the filter. If the total is higher than the number of products in results, then it means there are more pages of products to retrieve.

If instead you want to receive all products you can pass an all option with the value of true.

const products = await slyk.product.list({
  all: true,
  // Other options...
});

Filters

With the filters you can specify what kind of products you want to list. For example, you might want to list the featured products. To do this you'd pass featured: 'true' to the filters option.

const products = await slyk.product.list({
  filter: { featured: 'true', available: 'true', visible: 'true' },
  // Other options...
});

As a rule of thumb, you should always specify the available: 'true' and visible: 'true' filters, to make sure your users don't see products you don't want them to see.

Check the API documentation for a full list of available filters.

Includes

The include option allows you to include sub-entities on the product list directly, so that you don't have to make more requests to the Slyk API.

A common thing to include on a product list is the product's gallery. This can be done like thie:

const products = await slyk.product.list({
  include: 'gallery',
  // Other options...
});

Another thing you might want to include is the product's questions (the options you'll want to present users when buying the product). To pass multiple includes use a comma-separated list of words, like shown in the next example:

const products = await slyk.product.list({
  include: 'gallery,questions',
  // Other options...
});

Last updated