Limitation of results per request

TimBloembergenTimBloembergen Posts: 38Moderator, Lightspeed Staff 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:

10 comments

  • reportsreports Posts: 8Member
    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 Posts: 9Member
    Hi, this is PHP code for use with the PHP apiclient.
    you can't do this in Postman or curl.
  • levittlevitt Posts: 13Member
    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 Posts: 398Moderator, Lightspeed Staff 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.
  • levittlevitt Posts: 13Member
    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 Posts: 9Member
    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 Posts: 13Member
    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 Posts: 25Member
    @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
    $curl = curl_init();
    curl_setopt_array($curl, array(  CURLOPT_URL => "https://api.lightspeedapp.com/API/Account/{{Account_ID}}/Item.json?load_relations=%5B%22ItemShops%22%2C%22ItemVendorNums%22%5D&ItemShops.qoh=>,0",  CURLOPT_RETURNTRANSFER => true,  CURLOPT_ENCODING => "",  CURLOPT_MAXREDIRS => 10,  CURLOPT_TIMEOUT => 30,  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,  CURLOPT_CUSTOMREQUEST => "GET",  CURLOPT_HTTPHEADER => array(    "authorization: Bearer {{Access_Token}}"  ),));
    $response = curl_exec($curl);$err = curl_error($curl);
    curl_close($curl);
    if ($err) {    echo "cURL Error #:" . $err;} else {    echo $response;}
    ?>
    Super Cat Lightspeed
  • DennisGeusDennisGeus Posts: 9Member
    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.
Sign In or Register to comment.