Disclosure: This post contains my referral link for TwitterAPI.io. If you sign up through it, I may earn credits through their community rewards program. Everything below is what I actually ran — including the parts that broke.
I run a small daily hot-topics board (in Chinese) that collects trending posts from YouTube, X, Xiaohongshu, V2EX, GitHub and a few other sources, scores them, and publishes a batch twice a day. X was the hardest source to keep alive:
x_search) next. In headless mode on Grok Build CLI 1.0.77, the tool simply doesn't exist: --tools x_search warns that it names no tool, and grok mcp list comes back empty. It may only exist in the interactive TUI, or it may be plan-gated. Either way, it didn't work for an automated pipeline.
I needed a real search API that I could call from a script, with per-call pricing instead of a monthly plan. That's what led me to TwitterAPI.io.
TwitterAPI.io is an independent third-party X data API — it states on its own site that it is not affiliated with, endorsed by, or sponsored by X Corp. Keep that in mind: it's unofficial, so I kept my free fallback readers in the pipeline.
The pricing is what sold me. Search is billed per returned tweet:
My board uses about 40 returned tweets per refresh, twice a day — roughly $0.006/day. The $1 signup credit covers that for 300+ days. If your volume is bigger, the math is still simple: 1,000 tweets cost 15 cents.
The endpoint is:
GET https://api.twitterapi.io/twitter/tweet/advanced_search?query=<url-encoded query>&queryType=Top
Authentication is a single header:
X-API-Key: <your key>
The response envelope looks like this:
{
"tweets": [
{
"id": "2119069739423240656",
"url": "https://x.com/i/status/2119069739423240656",
"text": "...full post text...",
"createdAt": "Sat Nov 07 14:09:00 +0000 2026",
"likeCount": 72,
"retweetCount": 12,
"replyCount": 9,
"quoteCount": 1,
"viewCount": 6688,
"author": {"userName": "zengge1984", "name": "..."}
}
],
"has_next_page": true,
"next_cursor": "..."
}
Two things worth knowing about queryType:
For a trends board, neither is right on its own. I use Top and then filter to the last 30 days, sort by likes (then views), and cap at 10 posts per refresh. Pagination works through next_cursor if you need more than the ~20 posts a call returns.
One honest caveat: the provider's own API reference pages returned 404 when I fetched them directly, so the endpoint shape above is what I established by making live calls, not by reading docs. The calls themselves have been stable.
import json, os, time, urllib.request, urllib.parse
API_KEY = os.environ["TWITTERAPI_KEY"]
BASE = "https://api.twitterapi.io/twitter/tweet/advanced_search"
def search(query, query_type="Top", cursor=None, attempts=3):
params = {"query": query, "queryType": query_type}
if cursor:
params["cursor"] = cursor
url = BASE + "?" + urllib.parse.urlencode(params)
req = urllib.request.Request(url, headers={"X-API-Key": API_KEY})
for wait in (0, 5, 10): # backoff on 429 / 5xx
time.sleep(wait)
try:
with urllib.request.urlopen(req, timeout=30) as r:
return json.load(r)
except urllib.error.HTTPError as e:
if e.code in (429, 500, 502, 503) and wait < 10:
continue
raise
raise RuntimeError("search failed after retries")
if __name__ == "__main__":
data = search("AI获客", "Top")
tweets = sorted(data["tweets"], key=lambda t: t["likeCount"], reverse=True)
for t in tweets[:10]:
print(f"{t['likeCount']:>5} likes {t['viewCount']:>6} views @{t['author']['userName']}")
print(" ", t["text"][:120].replace("\n", " "))
The one thing that bit me: two searches fired back-to-back return HTTP 429. The first call succeeds, the second one gets rate-limited. My fix is in the snippet above — a 5-second gap between keyword searches plus retry with backoff (0, 5, 10 seconds, 3 attempts). Since I added that, I haven't seen another 429. If you're wiring this into a cron job, sleep between queries instead of hammering the endpoint; the provider clearly throttles rapid successive calls.
The board collector now tries X sources in this order:
site:x.com links, then reading each post through FxTwitter / vxTwitter / oEmbed
A full manual run after wiring it in returned 10 X posts, all from the last two weeks, 43–680 likes, each with full text and real engagement numbers. The board's latest batch (15 items across X, Xiaohongshu, V2EX and web sources) scored up to 84/100 on the rubric I use — the X posts from this API were three of the top scorers.
If you need X search from a script and you don't want a monthly plan, this is the cheapest honest option I've verified by actually paying (well, by spending free credit): $0.15 per 1,000 tweets, $1 to start, and a 429 quirk that a 5-second sleep fixes. Keep a free fallback in your pipeline, because it's an unofficial API and the docs are thin — but for a low-volume collector, it just works.
If you want to try it, here's my referral link: https://twitterapi.io/?ref=z3c2w3ah
And if you sign up through it and later pay for usage, I may earn a commission under their affiliate program too — same link, same disclosure.