Search code examples
twiliotwilio-apitwilio-node

Pagination is not working for calls resource in Twilio


Problem: I want API to serve all the calls that were received by any given twilio number. It works just fine initially when the call logs are in 50s but as the number increases the call logs API is becoming very slow as there are too many call logs to fetch and process on our end.

Expected Result: I want to paginate the call logs to fetch only 20 call logs at a time.

I tried using List all calls api

// Download the helper library from https://www.twilio.com/docs/node/install
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

client.calls.list({limit: 3})
            .then(calls => calls.forEach(c => console.log(c.sid)));

The expected result contain the following:

"calls": [1.., 2.., 3..],
"end": 1,
  "first_page_uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls.json?Status=completed&To=%2B123456789&From=%2B987654321&StartTime=2008-01-02&ParentCallSid=CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&EndTime=2009-01-02&PageSize=2&Page=0",
  "next_page_uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls.json?Status=completed&To=%2B123456789&From=%2B987654321&StartTime=2008-01-02&ParentCallSid=CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&EndTime=2009-01-02&PageSize=2&Page=1&PageToken=PACAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0",
  "page": 0,
  "page_size": 2,
  "previous_page_uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls.json?Status=completed&To=%2B123456789&From=%2B987654321&StartTime=2008-01-02&ParentCallSid=CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&EndTime=2009-01-02&PageSize=2&Page=0",
  "start": 0,
  "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls.json?Status=completed&To=%2B123456789&From=%2B987654321&StartTime=2008-01-02&ParentCallSid=CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&EndTime=2009-01-02&PageSize=2&Page=0"

But in my case its only returning, even though there are more call logs present

"calls": []

Now since I am not able to get next_page_uri, I am not able to paginate.

How can I get next_page_uri?


Solution

  • If you are using the latest version of the Twilio Node module then you can get all your calls in a couple of ways:

    The library automatically handles paging for you. Collections, such as calls, have list and each methods that page under the hood. With both list and each, you can specify the number of records you want to receive (limit) and the maximum size you want each page fetch to be (pageSize). The library will then handle the task for you.

    list eagerly fetches all records and returns them as a list, whereas each streams records and lazily retrieves pages of records as you iterate over the collection. You can also page manually using the page method.

    For more information about these methods, view the [auto-generated library https://www.twilio.com/docs/libraries/reference/twilio-node/).

    If you want to use paging anyway, it'd probably recommend the page option:

    client.calls.page({ pageSize: 10 }, function pageReceived(page) {
      page.instances.forEach(function(call) {
        console.log(call);
      });
      if (page.nextPage) {
        page.nextPage().then(pageReceived);
      }
    })