API Response only retrieves 100 elements at a time!

ld_Mtlld_Mtl Member Posts: 7

Hello,

As I just started working with a new LightSpeed API I need to obtain the historical Sales and Items for my company from 01-07-2018. When I make this request I receive a large amount of data (about 100000 sales) but each response is limited to only 100 sales.


I know that the limit is set by adding &limit=(number of records you need) in your URL. In my case I need to retrieve so many that if this is limited to 100 per request it will take me a really long time to build my historical database.

I have tried to increase the limit to 10000 and to 1000 but with no success. Apparently it is set to be 100 at the most (I have been able to decrease the limit to 1, 10 and other figure < 100). Below the code I'm using to get my item information:

I know this technically is okay, but I'm just not getting the results I need.

I have already looked into the API documentation but I wasn't able to find any information on the subject.

How can I get all my information in just one call?

And if it's not possible to do so: How can I build my historical database in batches?


Thanks,

ld_Mtl

2 comments

  • Adrian SamuelAdrian Samuel Moderator, Lightspeed Staff Posts: 573 moderator
    edited October 11

    Hey @ld_Mtl,

    You are correct, the only option you have is to to increment your offset in your request url entry by 100 each iteration of your API request.

    To do this, you should simply write a loop to get all your data and on each iteration of the loop, store it into some persistence layer whether that be a database or in a list in memory to be bulk written to your database later.


    I've written a basic example for you to work with


    using System;
    class Program {
        static void Main()
        {
        int offset = 0;
        bool nextPage = true;
       
            while (nextPage)
            {
                try {
                
                  string itemURL = $"https://api.lightspeedapp.com/API/Account/{accountID}/Item.json?offset={offset}";
    
                      var requestItemData = (HttpWebRequest)WebRequest.Create(itemURL);
                        if(requestItemData){ // write logic in conditional to check if it has the object property "Item" in the response
                                // store data retrieved from API in DB/cache/list
                            offset+=100;
                        } else { // Once the Object property, this indicates there is no more data and thus the looping should end
                            nextPage = false;
                            // This effectively ends the loop
                        }
                    offset+=100;
              
                }catch{
                    throw Console.Write("An Error occurred");
                }
            }
    
            Console.WriteLine("All data retrieved and processed!");
        }
    }
    
    
    

    I haven't ever written any C# code before so apologies if the code doesn't work if copy and pasted but I hope you get the gist of where to go from here :)

    Adrian Samuel

    API Integrations Consultant - Strategic Solutions

    Lightspeed HQ

Sign In or Register to comment.