How to Collect Donald Trump X Data with TwexAPI
For a public account dataset, the most reliable structure is not a one-off keyword search. Start with the account profile, collect timeline pages with cursors, and use search only when you need a filtered subset.
This guide uses realDonaldTrump as the example screen name. Verify the current handle before running any collection job, especially when you are building a repeatable archive.
Collection Plan
Use three TwexAPI endpoints:
| Task | Endpoint | When to use it |
|---|---|---|
| Get public account metadata | GET /twitter/{screen_name}/about | Save the account snapshot before collecting posts. |
| Page through account posts | POST /twitter/{screen_name}/timeline/page | Collect timeline posts with cursor-based pagination. |
| Search public X results | POST /twitter/advanced_search/page | Filter the account's posts by topic, phrase, or search operator. |
Save raw responses as well as normalized rows. That makes the dataset easier to audit when a field mapping or export rule changes later.
Get Account Metadata
curl --request GET \
--url https://api.twexapi.io/twitter/realDonaldTrump/about \
--header 'Authorization: Bearer <token>'Keep this account snapshot separate from your post table. It can include account identifiers, display fields, bio fields, and public counters returned by the API.
Page Through Timeline Posts
POST /twitter/{screen_name}/timeline/page accepts count and next_cursor. Omit next_cursor on the first request. If the response returns has_next_page: true, send the returned next_cursor with the next request.
curl --request POST \
--url https://api.twexapi.io/twitter/realDonaldTrump/timeline/page \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"count": 20
}'A page response includes data plus pagination fields.
{
"code": 200,
"msg": "success",
"data": [],
"has_next_page": true,
"next_cursor": "DAAHCgAB...",
"requested_count": 20,
"effective_count": 20,
"parsed_count": 20
}Python Collector
This collector fetches a bounded number of timeline pages and normalizes each post into a reviewable row.
1import os
2from typing import Any
3
4import requests
5
6TOKEN = os.environ["TWEXAPI_BEARER_TOKEN"]
7SCREEN_NAME = "realDonaldTrump"
8BASE_URL = "https://api.twexapi.io"
9
10def headers() -> dict[str, str]:
11 return {"Authorization": f"Bearer {TOKEN}", "Content-Type": "application/json"}
12
13def get_profile(screen_name: str) -> dict[str, Any] | None:
14 response = requests.get(
15 f"{BASE_URL}/twitter/{screen_name}/about",
16 headers=headers(),
17 timeout=30,
18 )
19 response.raise_for_status()
20 return response.json().get("data")
21
22def get_timeline_page(
23 screen_name: str,
24 *,
25 count: int = 20,
26 next_cursor: str | None = None,
27) -> dict[str, Any]:
28 payload: dict[str, Any] = {"count": count}
29 if next_cursor:
30 payload["next_cursor"] = next_cursor
31
32 response = requests.post(
33 f"{BASE_URL}/twitter/{screen_name}/timeline/page",
34 headers=headers(),
35 json=payload,
36 timeout=30,
37 )
38 response.raise_for_status()
39 return response.json()
40
41def normalize_post(post: dict[str, Any]) -> dict[str, Any]:
42 tweet_id = post.get("tweet_id") or post.get("id")
43 return {
44 "tweet_id": tweet_id,
45 "created_at": post.get("created_at_datetime") or post.get("created_at"),
46 "text": post.get("full_text") or post.get("text") or "",
47 "favorite_count": post.get("favorite_count") or 0,
48 "retweet_count": post.get("retweet_count") or 0,
49 "reply_count": post.get("reply_count") or 0,
50 "quote_count": post.get("quote_count") or 0,
51 "url": f"https://x.com/i/status/{tweet_id}",
52 }
53
54def collect_timeline(screen_name: str, max_pages: int = 3) -> list[dict[str, Any]]:
55 rows: list[dict[str, Any]] = []
56 cursor = None
57
58 for _ in range(max_pages):
59 page = get_timeline_page(screen_name, count=20, next_cursor=cursor)
60 rows.extend(normalize_post(post) for post in page.get("data", []))
61
62 if not page.get("has_next_page"):
63 break
64 cursor = page.get("next_cursor")
65 if not cursor:
66 break
67
68 return rows
69
70if __name__ == "__main__":
71 profile = get_profile(SCREEN_NAME)
72 posts = collect_timeline(SCREEN_NAME, max_pages=2)
73 print(profile.get("screen_name") if profile else SCREEN_NAME, len(posts))Optional Topic Search
Use timeline pagination for the account archive. Use advanced search when you need a narrower dataset.
curl --request POST \
--url https://api.twexapi.io/twitter/advanced_search/page \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"searchTerms": ["from:realDonaldTrump economy"],
"sortBy": "Latest"
}'Store the exact query with each result row. This matters when someone later asks why a post was included or excluded.
Data Hygiene
- Record
screen_name, run time, endpoint, query, and cursor for every job. - Use
tweet_idas the deduplication key. - Save raw JSON before flattening fields.
- Do not treat engagement counts as proof of accuracy, importance, or public opinion.
- Review quoted posts, media, and reply context before using posts in a report.
Wrap-up
For a public account such as realDonaldTrump, the practical workflow is profile metadata, timeline pagination, optional topic search, and careful export hygiene.
That gives you a repeatable collection process without adding unsupported pricing claims, invented response data, or analysis the API did not perform.