> ## Documentation Index
> Fetch the complete documentation index at: https://docs.x.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Pagination

> Paginate X API v2 responses in Java with Paginator<T> or manual tokens.

Paginated endpoints expose a `*Pages(...)` method returning `Paginator<T>` (`Iterable<T>`), which fetches pages lazily using `meta.next_token`.

## Automatic pagination (`*Pages`)

```java theme={null}
import com.x.xdk.Paginator;
import com.x.xdk.Schemas;
import com.x.xdk.XClient;
import com.x.xdk.clients.PostsClient;
import java.util.List;

XClient client = XClient.builder()
    .bearerToken(System.getenv("X_BEARER_TOKEN"))
    .build();

Paginator<Schemas.SearchPostsRecentResponse> pages =
    client.posts().searchRecentPages(
        "from:XDevelopers -is:retweet",
        PostsClient.SearchRecentOptions.builder()
            .maxResults(50)
            .postFields(List.of("created_at", "public_metrics"))
    );

int pageCount = 0;
for (Schemas.SearchPostsRecentResponse page : pages) {
    if (page.getData() != null) {
        System.out.println("Fetched " + page.getData().size() + " posts");
    }
    if (++pageCount >= 5) {
        break; // Stop early without extra requests
    }
}
```

## Manual pagination

Call the single-page method directly with `.paginationToken(...)`:

```java theme={null}
Schemas.SearchPostsRecentResponse firstPage = client.posts().searchRecent(
    "from:XDevelopers",
    PostsClient.SearchRecentOptions.builder().maxResults(25)
);

String nextToken = firstPage.getMeta() != null ? firstPage.getMeta().getNextToken() : null;
if (nextToken != null) {
    Schemas.SearchPostsRecentResponse secondPage = client.posts().searchRecent(
        "from:XDevelopers",
        PostsClient.SearchRecentOptions.builder()
            .maxResults(25)
            .paginationToken(nextToken)
    );
}
```
