Skip to content

Scrape Twitter followers with Python

You don’t need a headless browser or a logged-in X account to get a follower list. The RelayX API returns followers as JSON, one page at a time. This guide exports a whole list to CSV.

You want Endpoint Returns
Followers with full profiles GET /twitter/user/followers followers[] (user objects)
Who an account follows GET /twitter/user/followings followings[]
Just the IDs, fast and cheap GET /twitter/user/followers_ids ids[]
import csv, requests
API = "https://api.relayxapi.com"
HEADERS = {"X-API-Key": "YOUR_KEY"}
def followers(user, max_pages=50):
cursor = None
for _ in range(max_pages):
params = {"userName": user, **({"cursor": cursor} if cursor else {})}
r = requests.get(f"{API}/twitter/user/followers", params=params, headers=HEADERS)
r.raise_for_status()
body = r.json()
yield from body.get("followers", [])
if not body.get("has_next_page"):
break
cursor = body["next_cursor"]
with open("followers.csv", "w", newline="", encoding="utf-8") as f:
w = csv.writer(f)
w.writerow(["id", "userName", "name", "followers", "following", "verified", "createdAt"])
for u in followers("nasa", max_pages=5):
w.writerow([u["id"], u["userName"], u["name"], u["followers"], u["following"],
u["isBlueVerified"], u["createdAt"]])

max_pages is your spending cap: every page costs credits, so stop as soon as you have what you need. Swap followers for followings (response key followings) to get who an account follows.

followers_ids returns up to 5,000 IDs per call and costs much less per item. It’s the right choice for diffing lists (who unfollowed, mutuals) or counting overlap between accounts:

def follower_ids(user):
cursor, ids = -1, []
while True:
body = requests.get(f"{API}/twitter/user/followers_ids", headers=HEADERS,
params={"userName": user, "cursor": cursor}).json()
ids += body.get("ids", [])
if not body.get("has_next_page"):
return ids
cursor = body["next_cursor"]
a, b = set(follower_ids("nasa")), set(follower_ids("Space_Station"))
print(len(a & b), "accounts follow both")

Hydrate just the IDs you care about with GET /twitter/user/batch_info_by_ids.

Followers are billed per user returned, and the rate drops as pages fill: 3 → 2 → 1 credits each. Follower IDs cost 2 → 1 → 0.4 credits each. With 100,000 credits = $1, 1,000 followers cost $0.01–$0.03 and 1,000 IDs cost well under a cent. See credits in practice and pricing.

Self-hosted X scrapers need logged-in accounts, proxies and constant fixes whenever X changes its web app. Accounts used for scraping get rate-limited or suspended. The API gives you the same public data over plain HTTPS, and keeping it working is our job. More on this: Twitter scraper vs API.