Skip to content

Python Twitter scraper: scrape tweets with an API

Scrape Twitter with Python by calling an API instead of driving a browser. The script below uses requests to pull a user's recent tweets or run a search and print them. There is no browser to automate, no login and no HTML to parse. It reads public X tweets only.

Try a user's tweets now, no signupThen copy the Python script on this page.
Python Twitter scraper: a small machine gathers notes from a pegboard.

The job

A scraper that opens X in a headless browser breaks when the page changes and gets blocked when it logs in. Calling an API avoids both problems. You send a GET request and read JSON, so the script stays short and keeps working.

This page covers getting tweets out of X for analysis, a dataset or a monitor. The X API in Python guide goes deeper on call patterns and credit use. The script here is the quick start: copy it, set one environment variable and run it.

You can use it to scrape a user's tweets, search a keyword or hashtag, or build a small dataset in a loop. It returns public tweets only, one page per call, and a cursor takes you to the next page.

How it works

  1. Install requests

    Run pip install requests. You don't need anything else.

  2. Set your key

    Export CHATTERAPI_KEY in your shell. A test key returns recorded sample data, so you can run the script before you pay for anything.

  3. Run the script

    Save the code as scrape.py and run python scrape.py levelsio for a user's tweets, or edit it to call search.

  4. Page and save

    Follow page.nextCursor for more rows, then write the list to a JSON or CSV file for your analysis.

Endpoints that do it

EndpointWhat it's forCredits
List user tweetsGET /v1/x/users/{handle}/tweetsA user's recent tweets, newest first.2 credits
Search tweetsGET /v1/x/tweets/searchTweets by keyword or hashtag, with X search operators.2 credits
Get a tweetGET /v1/x/tweets/{id}One tweet in full, by id or link.2 credits

Free tools for the same job

Try the job in a browser first. These tools call the same endpoints, and you don't need a key or any code.

What the script prints

The script above calls the timeline endpoint. This is a recorded response from the same call.

A Python Twitter scraper with requests. Run it as: python scrape.py <handle>
import os
import sys

import requests

BASE = "https://api.chatterapi.com/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['CHATTERAPI_KEY']}"}


def user_tweets(handle, max_pages=3):
    """Collect recent tweets for a handle, following the cursor."""
    rows, cursor = [], None
    for _ in range(max_pages):
        params = {"cursor": cursor} if cursor else {}
        r = requests.get(f"{BASE}/x/users/{handle}/tweets", headers=HEADERS, params=params, timeout=60)
        r.raise_for_status()
        body = r.json()
        rows += body["data"]
        cursor = body["page"]["nextCursor"]
        if not body["page"]["hasMore"] or not cursor:
            break
    return rows


if __name__ == "__main__":
    for t in user_tweets(sys.argv[1] if len(sys.argv) > 1 else "levelsio"):
        print(t["createdAt"], t["likes"], t["text"][:80].replace("\n", " "))
curl "https://api.chatterapi.com/v1/x/users/levelsio/tweets" \
  -H "Authorization: Bearer $CHATTERAPI_KEY"
response, first 2 rows
{
  "data": [
    {
      "id": "2110066474513793361",
      "text": "✨ 200,000+ users on my computer nostalgia project @pieter\n\nSometimes it takes a few years for things to take off, in this case about 2 years!\n\nKeep creating 💖 https://t.co/Na0LXnwAjW",
      "url": "https://x.com/levelsio/status/2110066474513793361",
      "createdAt": "2026-10-13T17:53:45.000Z",
      "lang": "en",
      "likes": 129,
      "retweets": 4,
      "replies": 37,
      "quotes": 0,
      "views": 15475,
      "bookmarks": 9,
      "isReply": false,
      "inReplyToId": null,
      "conversationId": "2110066474513793361",
      "isRetweet": false,
      "quotedTweetId": "2108957488410161442",
      "type": "quote",
      "isQuote": true,
      "isPinned": false,
      "source": null,
      "hasVideo": false,
      "urls": [],
      "mentions": [
        "pieter"
      ],
      "author": {
        "id": "1577241403",
        "handle": "levelsio",
        "name": "@levelsio",
        "bio": "💸http://levels.vc\n📸http://PhotoAI.com $86K/m\n🎮http://VibeJam.com $44K/m\n🏡http://InteriorAI.com $23K/m\n👙@X $13K/m\n🌍http://Nomads.com $7K/m\n🏩http://Hotelist.com $5K/m",
        "followers": 974707,
        "following": 3276,
        "tweets": 181450,
        "verified": false,
        "blueVerified": true,
        "canDm": false,
        "protected": false,
        "location": "📕My book $5K/m 👉",
        "url": "https://readMAKE.com",
        "avatarUrl": "https://pbs.twimg.com/profile_images/2077111020305162240/PwddgOau_400x400.jpg",
        "createdAt": "2013-07-08T09:31:59.000Z",
        "profileUrl": "https://x.com/levelsio",
        "bannerUrl": "https://pbs.twimg.com/profile_banners/1577241403/1665964220",
        "likesGiven": 185852,
        "mediaCount": 19829,
        "pinnedTweetIds": [
          "2109949585737052316"
        ]
      },
      "media": [
        {
          "type": "photo",
          "url": "https://pbs.twimg.com/media/HUh2NAYWQAAP7ZK.jpg",
          "videoUrl": null
        }
      ],
      "quotedTweet": {
        "id": "2108957488410161442",
        "text": "💾 Two years ago I started working on these crazy nostalgia projects, kind of randomly, because I was bored and wanted to do something different than just build another SaaS or AI app/website\n\nI wanted to try to run Wind…",
        "url": "https://x.com/levelsio/status/2108957488410161442",
        "createdAt": null,
        "lang": null,
        "likes": null,
        "retweets": null,
        "replies": null,
        "quotes": null,
        "views": null,
        "bookmarks": null,
        "isReply": false,
        "inReplyToId": null,
        "conversationId": null,
        "isRetweet": false,
        "quotedTweetId": null,
        "type": "tweet",
        "isQuote": false,
        "isPinned": false,
        "source": null,
        "hasVideo": false,
        "urls": [],
        "mentions": [],
        "author": {
          "id": "",
          "handle": "levelsio",
          "name": "@levelsio",
          "bio": null,
          "followers": null,
          "following": null,
          "tweets": null,
          "verified": null,
          "blueVerified": null,
          "canDm": null,
          "protected": null,
          "location": null,
          "url": null,
          "avatarUrl": null,
          "createdAt": null,
          "profileUrl": "https://x.com/levelsio",
          "bannerUrl": null,
          "likesGiven": null,
          "mediaCount": null,
          "pinnedTweetIds": null
        },
        "media": []
      },
      "retweetedTweet": null
    },
    {
      "id": "2110030627336540370",
      "text": "This is also interesting to think about\n\nRight now it still takes, let's say, 30 minutes to a few hours to build something usable with AI and then you still polish for days/weeks/months\n\nIf we assume this will keep getti…",
      "url": "https://x.com/levelsio/status/2110030627336540370",
      "createdAt": "2026-10-13T15:31:18.000Z",
      "lang": "en",
      "likes": 324,
      "retweets": 11,
      "replies": 112,
      "quotes": 10,
      "views": 38339,
      "bookmarks": 118,
      "isReply": false,
      "inReplyToId": null,
      "conversationId": "2110030627336540370",
      "isRetweet": false,
      "quotedTweetId": null,
      "type": "quote",
      "isQuote": true,
      "isPinned": false,
      "source": null,
      "hasVideo": false,
      "urls": [],
      "mentions": [],
      "author": {
        "id": "1577241403",
        "handle": "levelsio",
        "name": "@levelsio",
        "bio": "💸http://levels.vc\n📸http://PhotoAI.com $86K/m\n🎮http://VibeJam.com $44K/m\n🏡http://InteriorAI.com $23K/m\n👙@X $13K/m\n🌍http://Nomads.com $7K/m\n🏩http://Hotelist.com $5K/m",
        "followers": 974707,
        "following": 3276,
        "tweets": 181450,
        "verified": false,
        "blueVerified": true,
        "canDm": false,
        "protected": false,
        "location": "📕My book $5K/m 👉",
        "url": "https://readMAKE.com",
        "avatarUrl": "https://pbs.twimg.com/profile_images/2077111020305162240/PwddgOau_400x400.jpg",
        "createdAt": "2013-07-08T09:31:59.000Z",
        "profileUrl": "https://x.com/levelsio",
        "bannerUrl": "https://pbs.twimg.com/profile_banners/1577241403/1665964220",
        "likesGiven": 185852,
        "mediaCount": 19829,
        "pinnedTweetIds": [
          "2109949585737052316"
        ]
      },
      "media": [],
      "quotedTweet": {
        "id": "",
        "text": "",
        "url": "https://x.com/i/status/",
        "createdAt": null,
        "lang": null,
        "likes": null,
        "retweets": null,
        "replies": null,
        "quotes": null,
        "views": null,
        "bookmarks": null,
        "isReply": false,
        "inReplyToId": null,
        "conversationId": null,
        "isRetweet": false,
        "quotedTweetId": null,
        "type": "tweet",
        "isQuote": false,
        "isPinned": false,
        "source": null,
        "hasVideo": false,
        "urls": [],
        "mentions": [],
        "author": {
          "id": "",
          "handle": "",
          "name": "",
          "bio": null,
          "followers": null,
          "following": null,
          "tweets": null,
          "verified": null,
          "blueVerified": null,
          "canDm": null,
          "protected": null,
          "location": null,
          "url": null,
          "avatarUrl": null,
          "createdAt": null,
          "profileUrl": "https://x.com/",
          "bannerUrl": null,
          "likesGiven": null,
          "mediaCount": null,
          "pinnedTweetIds": null
        },
        "media": []
      },
      "retweetedTweet": null
    }
  ],
  "page": {
    "nextCursor": "AYCt…",
    "hasMore": true
  }
}

Recorded from a live call on . Long text is shortened and the cursor is cut. A live key is charged 2 credits for this call. A test key returns this same sample for 0 credits.

What it won't do

  • Public tweets only. Protected accounts and deleted tweets are not returned.
  • Each call returns one page of about 20 tweets. The script caps pages with max_pages so a loop cannot spend more than you intend.
  • Keep the key in an environment variable and out of source control.
  • It reads tweets. It does not scrape Instagram, TikTok or other sites.

Questions

How do I scrape Twitter with Python without the official API?
Call a hosted API with requests. You skip the developer app and OAuth setup and read the JSON that comes back.
Do I need Selenium or a login to scrape tweets?
No. The script sends a plain HTTP request with your key, so there is no browser to automate and no account to log in to.
Can I scrape tweets from a hashtag or keyword?
Yes. Swap the timeline URL for /x/tweets/search and pass your query as q, for example q=%23python.
How do I save the tweets?
Write the list to disk with json.dump, or load it into pandas with pandas.DataFrame(rows) for analysis.

Other languages

Build it with one API key

Try it free for 7 days with 1,000 credits.