Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

377
Views
Google API - using nextPageToken to return all pages via node command line

We are developing some command line Google API scripts via node.js, and I'm having a hard time understanding the following:

Given this code to return the max number of our Google Groups in a single call (200):

// auth object, creds, scopes, async, etc. are above and working

const getAllGroups = await googleAdminDir.groups.list({
    auth,
    domain: "ourDomain.com",
    nextPageToken: "nextPage",
});

Say I have a variable number of groups. How would I (sanely) utilize nextPageToken to keep repeating the request until there are no more returned results?

I haven't found yet an API endpoint so far that I could call to find data that says You have *This* many groups in your domain. So my best attempt so far is something like:

const getAllGroups = await googleAdminDir.groups.list({
    auth,
    domain: "myDomain.com",
    nextPageToken: "nextPage",
}).then(resp =>{

    let returnData = resp.data.groups;
    let pageToken = data.data.nextPageToken;

    return googleAdminDir.groups.list({
        auth,
        domain: "myDomain.com",
        nextPageToken: "pageToken",
    });

}).then(resp =>{

    let returnData = resp.data.groups;
    let pageToken = data.data.nextPageToken;

    // Create .then() clauses until it errors, 
    // then create some way to stop it before the error is thrown?
    // TODO: Hide that I did it this way 

});

The above idea is....bad, and what I need help remedying. I probably missed something simple or have a "Why would you ever do it that way.....?" in here. Please enlighten me if so, I'll be humble :)

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!