DEV Community

Cover image for Get Twitter/X Data in 3 Lines of Python (No Official API Needed)
ApiTwitter
ApiTwitter

Posted on • Originally published at apitwitter.com

Get Twitter/X Data in 3 Lines of Python (No Official API Needed)

Twitter's official API requires an approval process that can take weeks, costs $100+/mo, and limits what you can do on lower tiers.

Here's how to get Twitter data in 3 lines of Python — no developer portal, no approval, no waiting.

Install the SDK

pip install apitwitter
Enter fullscreen mode Exit fullscreen mode

3 Lines. That's it.

from apitwitter import ApiTwitter

client = ApiTwitter("your-api-key")
user = client.get_user("elonmusk")
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "id": "44196397",
  "userName": "elonmusk",
  "name": "Elon Musk",
  "followers": 236446942,
  "following": 1294,
  "statuses_count": 99031,
  "is_blue_verified": true,
  "profile_image_url": "https://pbs.twimg.com/profile_images/..."
}
Enter fullscreen mode Exit fullscreen mode

No OAuth dance. No token refresh. No 40-page documentation to read first.

Where to get the API key

  1. Sign up at apitwitter.com
  2. You get 10,000 free credits instantly
  3. Go to Dashboard → API Keys → Create Key

That's your "your-api-key" string. It starts with tda_.

More examples

Search tweets

results = client.search("python programming", count=5)

for tweet in results["tweets"]:
    print(f"@{tweet['author']['userName']}: {tweet['text'][:100]}")
Enter fullscreen mode Exit fullscreen mode

Get user's tweets

tweets = client.get_user_tweets("elonmusk")

for tweet in tweets["tweets"]:
    print(tweet["text"][:120])
    print(f"  ❤️ {tweet.get('likes', 0)}  🔁 {tweet.get('retweets', 0)}")
    print()
Enter fullscreen mode Exit fullscreen mode

Get followers

result = client.get_followers("elonmusk", count=10)

for follower in result["followers"]:
    print(f"@{follower['userName']}{follower['name']}")
Enter fullscreen mode Exit fullscreen mode

Pagination

all_followers = []
cursor = None

while True:
    result = client.get_followers("username", count=200, cursor=cursor)
    all_followers.extend(result["followers"])

    cursor = result.get("next_cursor")
    if not cursor:
        break

print(f"Total: {len(all_followers)} followers")
Enter fullscreen mode Exit fullscreen mode

Write operations

Read endpoints (above) use a server-side pool — you only need your API key.

For write operations (posting tweets, liking, following, DMs), you need your own Twitter cookies and a proxy:

COOKIE = "ct0=...;auth_token=..."
PROXY = "http://user:pass@host:port"

# Post a tweet
client.create_tweet("Hello from Python!", COOKIE, PROXY)

# Like a tweet
client.like("1234567890", COOKIE, PROXY)

# Follow someone
client.follow("44196397", COOKIE, PROXY)

# Send a DM
client.send_dm("44196397", "Hey!", COOKIE, PROXY)
Enter fullscreen mode Exit fullscreen mode

Error handling

from apitwitter.exceptions import (
    AuthenticationError,
    InsufficientCreditsError,
    RateLimitError,
)

try:
    user = client.get_user("someone")
except AuthenticationError:
    print("Check your API key")
except InsufficientCreditsError:
    print("Time to top up credits")
except RateLimitError as e:
    print(f"Slow down — retry in {e.retry_after}s")
Enter fullscreen mode Exit fullscreen mode

What's available

The SDK covers all 56 endpoints:

Category Endpoints
Users get by username, get by ID, batch, followers, following, likes, media, replies, blocked, muted
Tweets user tweets, lookup by ID, create, delete, retweet, pin/unpin
Search full search with filters (Top, Latest, People, Photos, Videos)
Engagement like, unlike, follow, unfollow
DM send message, block, unblock
Bookmarks add, remove, list
Timeline For You, Latest
Lists full CRUD, members, subscribers
Communities explore, join, leave, tweets, media
Topics info, follow, unfollow

Pricing

Pay-per-use. No subscriptions.

  • Read operations: $0.14 per 1,000 requests
  • User profiles: $0.16 per 1,000 requests
  • Write operations: $2.25 per 1,000 requests
  • 10,000 free credits on signup

If your server errors out (5xx), credits are auto-refunded.

Also available for JavaScript/TypeScript

npm install @apitwitter/sdk
Enter fullscreen mode Exit fullscreen mode
import { ApiTwitter } from "@apitwitter/sdk";

const client = new ApiTwitter("your-api-key");
const user = await client.getUser("elonmusk");
console.log(user.name, user.followers);
Enter fullscreen mode Exit fullscreen mode

Links


Questions? Drop a comment — happy to help.

Top comments (0)