Pagination

This tutorial will teach you how to navigate through the multiple records in the response. The maximum number of results that can be fetched with a single API call is 20. To view more records, another API call has to be made. To make things easier, each API call which has a total count greater than the limit requested will return a parameter called next and previous. Once the first set of records have been fetched, you need to browse the next URL to view the next set of 20 records.

You can also set the number of records fetched per page by using the limit parameter.

For example, lets fetch the details of all existing applications in your account, two at a time. Here the limit parameter is set to 2 and the offset is set to 0, so only the first 2 applications in your account will be returned.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import plivo,plivoxml

auth_id = "Your AUTH_ID"
auth_token = "Your AUTH_TOKEN"

p = plivo.RestAPI(auth_id, auth_token)

# Get details all existing applications
params = {
    'limit' : '2', # The number of results per page
    'offset' : '0' # The number of value items by which the results should be offset
}

response = p.get_applications(params)
print response[1]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
require 'rubygems'
require 'plivo'
include Plivo

AUTH_ID = "Your AUTH_ID"
AUTH_TOKEN = "Your AUTH_TOKEN"


p = RestAPI.new(AUTH_ID, AUTH_TOKEN)

# Get details all existing applications
params = {
        'limit' => '2', # The number of results per page
        'offset' => '0' # The number of value items by which the results should be offset
}

response = p.get_applications(params)
print response[1]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
var plivo = require('plivo');
var p = plivo.RestAPI({
  authId: 'Your AUTH_ID',
  authToken: 'Your AUTH_TOKEN'
});

// Get details of all applications
var params = {
    'limit' : '2', // The number of results per page
    'offset' : '0' // The number of value items by which the results should be offset
};
p.get_applications(params, function (status, response) {
    console.log('Status: ', status);
    console.log('API Response:\n', response);
    console.log('Next: ', response['meta']['next'])
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?php
    require 'vendor/autoload.php';
    use Plivo\RestAPI;
    
    $auth_id = "Your AUTH_ID";
    $auth_token = "Your AUTH_TOKEN";
    
    $p = new RestAPI($auth_id, $auth_token);
    
    // Get Details of all existing Applications
    $params = array(
        'limit' => '2', # The number of results per page
        'offset' =>'0' # The number of value items by which the results should be offset
        );
    $response = $p->get_applications($params);
    print_r ($response['response']);    
?>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package plivoexample;

import java.util.LinkedHashMap;

import com.plivo.helper.api.client.*;
import com.plivo.helper.api.response.application.ApplicationFactory;
import com.plivo.helper.exception.PlivoException;

public class App {

    public static void main(String[] args) throws IllegalAccessException {

        String auth_id = "Your AUTH_ID";
        String auth_token = "Your AUTH_TOKEN";
        
        RestAPI api = new RestAPI(auth_id, auth_token, "v1");

        // Get details all existing endpoints

        LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>();
        parameters.put("limit","2"); // The number of results per page
        parameters.put("offset","0"); // The number of value items by which the results should be offset

        try {
            EndpointFactory resp = api.getEndpoints(parameters);
            System.out.println(resp);
        }catch (PlivoException e){
            System.out.println(e.getLocalizedMessage());
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using RestSharp;
using Plivo.API;

namespace apps
{
    class Program
    {
        static void Main(string[] args)
        {
            RestAPI plivo = new RestAPI("Your AUTH_ID", "Your AUTH_TOKEN");

            // Get details of all existing applications
            IRestResponse<ApplicationList> res = plivo.get_applications(new Dictionary<string, string>(){ 
                {"limit", "2"}, // The number of results per page
                {"offset", "0"}, // The number of value items by which the results should be offset
            });

            // Prints the response
            Console.Write(res.Content);
        }
    }
}
1
2
curl -i --user AUTH_ID:AUTH_TOKEN \
    "https://api.plivo.com/v1/Account/{auth_id}/Application/?limit=2&offset=0"

Sample Response

(200,{
        u'meta': {
            u'previous': None, 
            u'total_count': 12, 
            u'offset': 0, 
            u'limit': 2, 
            u'next': u'/v1/Account/xxxxxxxxxxxxxxxxx/Application/?limit=2&offset=2'
        },  
        u'objects': [
            {
                u'fallback_method': u'POST', 
                u'default_app': False, 
                u'app_name': u'Testing_App10', 
                u'sub_account': None, 
                u'production_app': False, 
                u'enabled': True, 
                u'app_id': u'14344967555248312', 
                u'public_uri': False, 
                u'hangup_url': u'http://example.com', 
                u'sip_uri': u'sip:14344967555248312@app.plivo.com', 
                u'default_endpoint_app': False, 
                u'answer_url': u'http://example.com', 
                u'message_url': u'', 
                u'resource_uri': u'/v1/Account/XXXXXXXXXXXX/Application/14344967555248312/', 
                u'hangup_method': u'POST', 
                u'message_method': u'POST', 
                u'fallback_answer_url': u'', 
                u'answer_method': u'POST'
            }, 
            {
                u'fallback_method': u'POST', 
                u'default_app': False, 
                u'app_name': u'Testing_App7', 
                u'sub_account': None, 
                u'production_app': False, 
                u'enabled': True, 
                u'app_id': u'12933174623656681', 
                u'public_uri': False, 
                u'hangup_url': u'http://example.com', 
                u'sip_uri': u'sip:12933174623656681@app.plivo.com', 
                u'default_endpoint_app': False, 
                u'answer_url': u'http://example.com', 
                u'message_url': u'', 
                u'resource_uri': u'/v1/Account/XXXXXXXXXXXX/Application/12933174623656681/', 
                u'hangup_method': u'POST', 
                u'message_method': u'POST', 
                u'fallback_answer_url': u'', 
                u'answer_method': u'POST'
            }
        ], 
    u'api_id': u'92150f80-8f58-11e4-96e3-22000abcb9af'
    }
)

The next parameter in the response is /v1/Account/XXXXXXXXXXXX/Application/?limit=2&offset=2 Now, browse https://api.plivo.com/v1/Account/XXXXXXXXXXXX/Application/?limit=2&offset=2 to view the next page of results. To traverse pages, browse the next and previous uris.

  1. Account API
  2. Application API
  3. Endpoint API
  4. Number API
  5. Phone Number API
  6. Pricing API
  7. API Pagination