> ## 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 in Ruby XDK

> Paginate through X API v2 results lazily using Ruby Enumerators.

Endpoints that accept `pagination_token` or `next_token` return a lazy Ruby `Enumerator` that fetches each page on demand until `meta.next_token` is absent.

## Iterating with an `Enumerator`

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(bearer_token: ENV.fetch("X_BEARER_TOKEN"))

# Fetch at most 3 pages lazily
client.posts.search_recent(query: "ruby lang:en", max_results: 25).take(3).each do |page|
  (page["data"] || []).each do |post|
    puts "#{post['id']}: #{post['text']}"
  end
end
```

## Fetching just the first page

```ruby theme={null}
first_page = client.posts.search_recent(query: "ruby lang:en", max_results: 10).first
```

## Passing a block directly

```ruby theme={null}
client.posts.search_recent(query: "ruby lang:en", max_results: 25) do |page|
  puts "Got #{(page['data'] || []).length} posts"
  break if page.dig("meta", "result_count").to_i < 25
end
```
