How to Get X Trending Topics by Country
Trending topics are useful when you need a fast read on what X is surfacing in a region. They are not a complete public-opinion dataset, and they should not be treated as long-term sentiment by themselves. Their value is immediacy: they help dashboards, newsrooms, content teams, and alerting systems notice which topics are moving right now.
TwexAPI exposes this workflow through GET /twitter/{country}/trending. Use worldwide for default global-style trends, or a supported country slug such as united-states, japan, or united-kingdom.
Endpoint
GET https://api.twexapi.io/twitter/{country}/trending
Authorization: Bearer <your_token>Path parameter:
| Parameter | Description |
|---|---|
country | worldwide or a supported country slug such as united-states, japan, france, india, or brazil |
Example:
curl --request GET \
--url https://api.twexapi.io/twitter/united-states/trending \
--header 'Authorization: Bearer <your_token>'Use the same endpoint for other regions:
/twitter/worldwide/trending
/twitter/japan/trending
/twitter/united-kingdom/trending
/twitter/brazil/trendingWhat To Store
The response uses the standard TwexAPI wrapper with code, msg, and data. The items in data represent the current trend list for the selected country.
For repeatable analysis, store a snapshot instead of only reading the latest response:
| Field | Why it matters |
|---|---|
country | Makes regional comparisons possible |
fetched_at | Turns a volatile trend list into a time series |
name | Human-readable trend text |
query / url | Useful for linking into search or follow-up collection |
tweet_volume | Helpful when available, but can be missing or approximate |
| raw item | Preserves fields that may be useful later |
Trend lists change quickly. Without fetched_at, a trend row loses most of its meaning.
Python Snapshot Script
This script fetches one country and appends the current trend list to a JSONL file. Each line contains the country, fetch time, and raw trend object.
1import json
2import os
3from datetime import datetime, timezone
4
5import requests
6
7API_BASE = "https://api.twexapi.io"
8TOKEN = os.environ["TWEXAPI_TOKEN"]
9COUNTRY = "united-states"
10OUTPUT_PATH = "trending-topics.jsonl"
11
12def fetch_trends(country: str) -> list:
13 response = requests.get(
14 f"{API_BASE}/twitter/{country}/trending",
15 headers={"Authorization": f"Bearer {TOKEN}"},
16 timeout=30,
17 )
18 response.raise_for_status()
19
20 payload = response.json()
21 if payload.get("code") not in (None, 200):
22 raise RuntimeError(payload.get("msg", "TwexAPI request failed"))
23
24 return payload.get("data") or []
25
26fetched_at = datetime.now(timezone.utc).isoformat()
27trends = fetch_trends(COUNTRY)
28
29with open(OUTPUT_PATH, "a", encoding="utf-8") as output:
30 for trend in trends:
31 output.write(
32 json.dumps(
33 {
34 "country": COUNTRY,
35 "fetched_at": fetched_at,
36 "trend": trend,
37 },
38 ensure_ascii=False,
39 )
40 + "\n"
41 )
42
43print(f"Saved {len(trends)} trends for {COUNTRY} at {fetched_at}")Run it with:
TWEXAPI_TOKEN="your_twexapi_bearer_token" python save-trending-topics.pyMonitoring Multiple Countries
If you need a small regional dashboard, keep the country list explicit:
countries = ["worldwide", "united-states", "japan", "united-kingdom"]
for country in countries:
trends = fetch_trends(country)
print(country, len(trends))For scheduled jobs, store each country separately and compare snapshots by fetched_at. This makes it easier to answer questions like:
- Which topics appeared in multiple countries?
- Which trends disappeared after one snapshot?
- Which topics moved from local to global?
- Which topics deserve a follow-up search export?
Trend Topics Versus Trending Tweets
This endpoint returns the topic list. It is best for dashboards, alerts, and editorial planning.
If you need tweets within a trend category, use the global trending tweet workflow instead. That workflow starts with /twitter/global-trending/countries, /twitter/global-trending/topics, and /twitter/global-trending/tweets.
Keep the two jobs separate: trend topics tell you what to inspect; trending tweets give you posts to analyze.
Production Notes
- Cache country choices in configuration instead of hard-coding them across scripts.
- Store raw responses for debugging and future field changes.
- Treat missing
tweet_volumeas normal; do not make it required in your schema. - Keep timestamps in UTC.
- Use trend data as a discovery signal, then run search or tweet export workflows for evidence.