# Pagination

Most "retrieve all" responses from EasyPost are paginated.
Records are returned in descending order (most recent first), and the `has_more` attribute indicates whether or not additional pages can be requested.

The request accepts a variety of parameters which can be used to modify the scope.
The recommended way of paginating is to use either the `before_id`/`after_id` or `start_datetime`/`end_datetime` parameters to specify where the next page begins.

You can also use the optional `page_size` parameter to specify how many records you would like per page. We limit the number of records per page to 100, and most endpoints default to 20.

Support for these parameters vary by endpoint.

If you need to retrieve a successive page when the `has_more` key is present, make another "retrieve all" request and set the `before_id` to the ID of the last item returned.
For example, let's say you have 140 `Shipment` objects and are trying to interact with all of them.
If you make a call to retrieve all `Shipment`s with the `page_size` set to 20, you would need to loop through 7 pages, each with 20 results.

To get to the next page of results, you would need to set `before_id` to the `response['shipments'][19]['id']`.
You would continue to request each additional page while specifying the `id` of the last `Shipment` of the previous request until the `has_more` attribute is `false`, indicating that all results have been retrieved.

### Example: Paginate shipment objects

#### cURL

```shell
# Get first page of results
curl -X GET "https://api.easypost.com/v2/shipments?page_size=5" \
  -u "EASYPOST_API_KEY":

# Provide the ID of the last element of the previous page in the before_id param
curl -X GET "https://api.easypost.com/v2/shipments?page_size=5&before_id=shp_..." \
  -u "EASYPOST_API_KEY":
```

#### Go

```go
package example

import (
	"fmt"

	"github.com/EasyPost/easypost-go/v5"
)

func getNextPage() {
	client := easypost.New("EASYPOST_API_KEY")

	// Get first page of results
	shipments, _ := client.ListShipments(
		&easypost.ListShipmentsOptions{
			PageSize: 5,
		},
	)

	// Provide the previous results page to move onto the next page
	secondPage, _ := client.GetNextShipmentPage(shipments)

	// You can also ask for the next page to be of a specific size
	lastPage, _ := client.GetNextShipmentPageWithPageSize(secondPage, 10)

	fmt.Println(lastPage)
}
```

#### Java

```java
package shipments;

import com.easypost.exception.EasyPostException;
import com.easypost.model.ShipmentCollection;
import com.easypost.service.EasyPostClient;

import java.util.HashMap;

public class All {
    public static void main(String[] args) throws EasyPostException {
        EasyPostClient client = new EasyPostClient("EASYPOST_API_KEY");

        // Get the first page of results
        HashMap<String, Object> params = new HashMap<>();
        params.put("page_size", 5);

        ShipmentCollection shipments = client.shipment.all(params);

        // Provide the previous results page to move onto the next page
        ShipmentCollection nextPage = client.shipment.getNextPage(shipments);

        System.out.println(nextPage);
    }
}
```

#### C#

```csharp
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyPost;
using Newtonsoft.Json;

namespace EasyPostExamples
{
    public class Examples
    {
        public static async Task Main()
        {
            var client = new EasyPost.Client(new EasyPost.ClientConfiguration("EASYPOST_API_KEY"));

            // Get first page of results
            EasyPost.Parameters.Shipment.All parameters = new()
            {
                PageSize = 5
            };

            EasyPost.Models.API.ShipmentCollection shipmentCollection = await client.Shipment.All(parameters);

            // Provide the previous results page to move onto the next page
            EasyPost.Models.API.ShipmentCollection nextPage = await client.Shipment.GetNextPage(shipmentCollection);

            Console.WriteLine(JsonConvert.SerializeObject(nextPage, Formatting.Indented));
        }
    }
}
```

#### Node.js

```javascript
const EasyPostClient = require('@easypost/api');

const client = new EasyPostClient('EASYPOST_API_KEY');

(async () => {
  // Get first page of results
  const shipments = await client.Shipment.all({
    page_size: 5,
  });

  // Provide the previous results page to move onto the next page
  const nextPage = await client.Shipment.getNextPage(shipments);

  console.log(nextPage);
})();
```

#### PHP

```php
<?php

$client = new \EasyPost\EasyPostClient('EASYPOST_API_KEY');

// Get first page of results
$shipments = $client->shipment->all([
    'page_size' => 5,
]);

// Provide the previous results page to move onto the next page
$nextPage = $client->shipments->getNextPage($shipments);

echo $nextPage;
```

#### Python

```python
import easypost

client = easypost.EasyPostClient("EASYPOST_API_KEY")

# Get first page of results
shipments = client.shipment.all(
    page_size=5,
)

# Provide the previous results page to move onto the next page
next_page = client.shipments.get_next_page(shipments)

print(next_page)
```

#### Ruby

```ruby
require 'easypost'

client = EasyPost::Client.new(api_key: 'EASYPOST_API_KEY')

# Get first page of results
shipments = client.shipment.all(page_size: 5)

# Provide the previous results page to move onto the next page
next_page = client.shipment.get_next_page(shipments)

puts next_page
```