Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions x-api/posts/search/integrate/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ Search endpoints return results in pages. Use the `next_token` from the response
3. Include `next_token` in subsequent requests
4. Repeat until no `next_token` is returned

<Note>
A page can contain fewer Posts than `max_results`, or none at all, and still include a `next_token`. Keep paginating as long as `next_token` is present: only a missing `next_token` signals the end of the results, and the final page can itself be empty. For complete historical collection, use the default `sort_order=recency`. See [Empty and short pages](/x-api/posts/search/integrate/paginate#empty-and-short-pages).
</Note>

### Example

<CodeGroup dropdown>
Expand Down
43 changes: 41 additions & 2 deletions x-api/posts/search/integrate/paginate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Here are fundamental recent search pagination details:

* The recent search endpoints will respond to a query with at least one page, and provide a next_token in its JSON response if additional pages are available. To receive matching Posts, this process can be repeated until no token is included in the response.
* The next_token does not expire. Multiple requests using the same next_token value will receive the same results, regardless of when the request is made.
* A page can contain fewer Posts than max_results, or no Posts at all, and still include a next_token. Keep paginating as long as a next_token is present. See [Empty and short pages](#empty-and-short-pages).

* Posts are delivered in reverse-chronological order, in the UTC timezone. This is true within individual pages, as well as across multiple pages: 
* The first Post in the first response will be the most recent one matching your query.
Expand Down Expand Up @@ -63,7 +64,7 @@ To retrieve the next 10 Posts, this next_token is added to the original request.

https://api.x.com/2/tweets/search/recent?query=snow&next_token=b26v89c19zqg8o3fobd8v73egzbdt3qao235oql

The process of looking for a next_token and including it in a subsequent request can be repeated until all (or some number of) Posts are collected, or until a specified number of requests have been made. If data fidelity (collecting all matches of your query) is key to your use case, a simple "repeat until request.next_token is null" design will suffice. 
The process of looking for a next_token and including it in a subsequent request can be repeated until all (or some number of) Posts are collected, or until a specified number of requests have been made. If data fidelity (collecting all matches of your query) is key to your use case, a simple "repeat until request.next_token is null" design will suffice. Don't stop early because a page is empty or has fewer Posts than max_results; only a missing next_token marks the end of the results.


#### Polling and listening use cases
Expand Down Expand Up @@ -121,4 +122,42 @@ https://api.x.com/2/tweets/search/recent?query=snow&since\_id=12000&next\_token=

This second response provides the remaining eight Posts, and no next_token. Note that we do not update our newest_id value (12300), and instead base our next since_id request on the first response's newest_id value:

https://api.x.com/2/tweets/search/recent?query=snow&since_id=13800
https://api.x.com/2/tweets/search/recent?query=snow&since_id=13800


### Empty and short pages

These rules apply to both recent search (`GET /2/tweets/search/recent`) and full-archive search (`GET /2/tweets/search/all`).

* **Keep paginating while `next_token` is present.** A page can contain fewer Posts than `max_results`, or no Posts at all (no `data` array and `"result_count": 0`), and still include `meta.next_token`. This can happen when matching Posts are removed after retrieval, for example because they were deleted or protected, or when a stretch of the requested time range has few matches. Request the next page with that token as usual.
* **Only a missing `next_token` signals the end of the results.** The final page of a complete result set can itself contain zero Posts. For example, a response of `{"meta": {"result_count": 0}}` with no `next_token` means that there are no more results.
* **Use `sort_order=recency` for complete collection.** `recency` is the default and returns every matching Post in reverse-chronological order across pages. `relevancy` ordering is designed to surface the most relevant Posts and isn't intended to return every matching Post.
* **Retry server errors with the same token.** If a request fails with a `5xx` error such as `503 Service Unavailable`, retry it with the same `next_token` after a short backoff. The token stays valid, so you won't skip or repeat results.

A minimal loop looks like this:

```python
import requests

params = {
"query": "snow",
"start_time": "2026-01-01T00:00:00Z",
"end_time": "2026-01-02T00:00:00Z",
"max_results": 500,
"sort_order": "recency",
}
posts = []
while True:
response = requests.get(
"https://api.x.com/2/tweets/search/all",
headers={"Authorization": f"Bearer {BEARER_TOKEN}"},
params=params,
)
response.raise_for_status() # retry 5xx errors with the same params
body = response.json()
posts.extend(body.get("data", [])) # a page may have no "data"
next_token = body.get("meta", {}).get("next_token")
if not next_token:
break # only a missing next_token ends pagination
params["next_token"] = next_token
```
Loading