> ## 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 C# with IAsyncEnumerable<T> or manual tokens.

Paginated endpoints expose a `*Pages(...)` method returning `Paginator<T>` (`IAsyncEnumerable<T>`), which fetches pages lazily using `meta.next_token` as you iterate with `await foreach`.

## Automatic pagination (`*Pages`)

```csharp theme={null}
using System;
using System.Collections.Generic;
using System.Threading;
using Xdk;
using Xdk.Clients;

using var cts = new CancellationTokenSource();
using var client = new XClient(new XClientOptions
{
    BearerToken = Environment.GetEnvironmentVariable("X_BEARER_TOKEN"),
});

int pageCount = 0;
await foreach (var page in client.Posts.SearchRecentPages(
    "from:XDevelopers -is:retweet",
    new PostsClient.SearchRecentOptions
    {
        MaxResults = 50,
        PostFields = new List<string> { "created_at", "public_metrics" },
    }).WithCancellation(cts.Token))
{
    Console.WriteLine($"Fetched {page.Data?.Count ?? 0} posts");
    if (++pageCount >= 5)
    {
        break; // Stop early without extra requests
    }
}
```

## Manual pagination

Call the single-page `*Async` method directly with `PaginationToken`:

```csharp theme={null}
var firstPage = await client.Posts.SearchRecentAsync(
    "from:XDevelopers",
    new PostsClient.SearchRecentOptions { MaxResults = 25 });

string? nextToken = firstPage.Meta?.NextToken;
if (!string.IsNullOrEmpty(nextToken))
{
    var secondPage = await client.Posts.SearchRecentAsync(
        "from:XDevelopers",
        new PostsClient.SearchRecentOptions
        {
            MaxResults = 25,
            PaginationToken = nextToken,
        });
}
```
