Limitation of results per request

TimBloembergenTimBloembergen Moderator, Lightspeed Staff Posts: 38 moderator
The API returns 50 results per request, you can extend this limit to a max of 250 results by adding the filter limit=250. By using the page filter you can receive 250 results page page page=2.

With the PHP client you pass these filters in as an array as the second argument.
$api->products->get(null, array('limit'=>250, 'page'=>2))
Tim Bloembergen
API Integrations Specialist
Lightspeed HQ
Tags:

13 comments

  • reportsreports Member Posts: 8
    This didn't work for me. Where do you post the code? I get a response ""findAllByFieldMatch got search for \"$what \". Did you mean: Note?"

    Any help appreciated. Using Postman to call the API
  • DennisGeusDennisGeus Member Posts: 11
    Hi, this is PHP code for use with the PHP apiclient.
    you can't do this in Postman or curl.
  • levittlevitt Member Posts: 13
    I'd like to share my solution to get all items in one array.
    $what = 'products';
    $pages = ceil($count/250);
    $result = array();
    $all = array();
    $page = 1; // first page
    do {
        $filter = array('limit' => 250, 'page' => $page);
        try {
            $result = $api->$what->get(null, $filter);
        }
        catch (Exception $e) {
            echo  $e->getMessage() ;
        }
    
        $all = array_merge($all, $result);
    
        $page++;
    }
    while ($page <= $pages);
    
    ofcourse you can use it for all endpoints.
    Thank you for your share :) 
    very nice and it works for me, however, I only got 250 products in total, is this the upper limit rule of this api or not?
    because I have more than 500 products in my shop, do you have any idea that how can I get all of them? 

    Thank you again :) 
  • Adrian SamuelAdrian Samuel Moderator, Lightspeed Staff Posts: 625 moderator
    Hey @levitt, I personally use an almost identical solution to the one presented above.

    As stated in our documentation here:
    https://developers.lightspeedhq.com/ecom/introduction/resources/

    limit: Amount of products returned by the API. (default: 50, maximum: 250)

    Thus for server integrity, we limit each page to return this listed maximum.

    Adrian Samuel

    Integrations & Solutions Developer - Strategic Solutions

    Lightspeed HQ

  • levittlevitt Member Posts: 13
    Hey @levitt, I personally use an almost identical solution to the one presented above.

    As stated in our documentation here:
    https://developers.lightspeedhq.com/ecom/introduction/resources/

    limit: Amount of products returned by the API. (default: 50, maximum: 250)

    Thus for server integrity, we limit each page to return this listed maximum.
    Hi Adrian, thank you for your reply, but I am still struggling here, too bad :/ 

    I used my code blew (very simple), only got 179 products, I changed 'page' into 3,4, or ant bigger than 2, then return nothing....
    I have 500 products in this shop, and other shops I have something around 1000 products, I need get all of them... 

    Do you have any suggestion for me? thank you very much ;) 

    my code looks like this 
    <?php
    include_once 'WebshopappApiClient.php';

    $api = new WebshopappApiClient('eu1', '***public key***', '***private key***', 'nl');

    $products = $api->products->get(null, array('limit'=>250, 'page'=>2));

    $count=0;

    foreach ($products as $x){
    $count=$count+1;
    }

    echo $count;
  • DennisGeusDennisGeus Member Posts: 11
    edited October 2018
    No, you are right, the above code is returning 250 items because I forgot the inital count of the total items.

    so add this line
    $count = $api->$what->count();

    $count = $api->$what->count();
    $pages = ceil($count/250);
    $result = array();
    $all = array();
    $page = 1; // first page
    do {
    $filter = array('limit' => 250, 'page' => $page);
    try {
    $result = $api->$what->get(null, $filter);
    }
    catch (Exception $e) {
    echo $e->getMessage() ;
    }

    $all = array_merge($all, $result);

    $page++;
    }
    while ($page <= $pages);
    echo count($all);

    I've added a count to display the number of items in the $all array to check and in my case it is 768 so, this works.


  • levittlevitt Member Posts: 13
    No, you are right, the above code is returning 250 items because I forgot the inital count of the total items.

    so add this line
    $count = $api->$what->count();

    $count = $api->$what->count();
    $pages = ceil($count/250);
    $result = array();
    $all = array();
    $page = 1; // first page
    do {
    $filter = array('limit' => 250, 'page' => $page);
    try {
    $result = $api->$what->get(null, $filter);
    }
    catch (Exception $e) {
    echo $e->getMessage() ;
    }

    $all = array_merge($all, $result);

    $page++;
    }
    while ($page <= $pages);
    echo count($all);

    I've added a count to display the number of items in the $all array to check and in my case it is 768 so, this works.


    You are amazing! thank you very very much, you solved my biggest problem of the day! thanks a lot and I appreciate your help!!! <3<3<3<3<3  
  • thisisbolothisisbolo Member Posts: 26
    @DennisGeus Hi. I'm trying to come up with a solution for pagination. I came across this thread. How would I use this in my code below? Would you be able to provide any suggestions? I'd like to return all results.

    <?php
    Super Cat Lightspeed
  • DennisGeusDennisGeus Member Posts: 11
    You just need to apply the same concept.

    determine the maximum number of results, devide it by 250 and round up to find out how many pages you have.
    then loop through the requests and add 1 page at each loop
    You can store the results of each page in an array.
  • JoostJoost Member Posts: 5

    Hello,

    Regarding the ProductsAttributes resource, you can only retrieve for one product at one. It results in selecting all products with a type attached and do a request for each one by one.

    Would it be possible to do a request like products, by setting the id to null retrieving all? It would be a huge reduce in requests

  • Adrian SamuelAdrian Samuel Moderator, Lightspeed Staff Posts: 625 moderator

    Hey @Joost, could you send a link to the endpoint you're speaking of and an example of your request?

    Adrian Samuel

    Integrations & Solutions Developer - Strategic Solutions

    Lightspeed HQ

  • JoostJoost Member Posts: 5
    edited May 7

    @Adrian Samuel

    I'am talking about the recource

    GET /products/{product_id}/attributes.json
    

    There are no webhooks for this, so i need to check regularly, but it can only done one by one at a time. By adding a "get->productAttributes" function that returns all (or by limit 250) i would have several thousands (+10.000) less api requests each day, and that is only for my own clients! It would only need to add the product Id to the returned value...

Sign In or Register to comment.