A Node.js script that uses the Twitter API v2 to search and collect tweets in bulk, save progress incrementally to CSV, and handle rate limits and safe interruption (Ctrl+C).
- Authentication with Twitter v2 Bearer Token
- Incremental CSV export with headers
- Safe interruption (SIGINT) and progress saving
- Rate-limit handling with automatic wait until reset
- Configurable query, date range, and output folder
- Node.js v14+
- A Twitter Developer account with Elevated or Academic Research access
- A valid Bearer Token
-
Clone this repository
git clone https://github.com/miiguellssantos/X-API-Research.git cd X-API-Research -
Install dependencies
npm install twitter-api-v2 luxon node-notifier
-
Add your Bearer Token
- Open
twitterAPI.js - Replace the placeholder in the
BEARER_TOKENconstant with your actual token
- Open
- Queries
Edit thequeriesarray intwitterAPI.jsto define one or more search queries. - Date range
AdjuststartDateandendDate(ISO 8601 format) inmain()to control the period. - Output path
Change thepath.resolve(...)inmain()to point to your desired CSV directory.
node twitterAPI.js- The script logs authentication status, each batch progress, and any rate-limit waits.
- Press Ctrl+C to safely stop the run; progress will be saved up to the last batch.
| Geo | Created At | Likes | Retweets | Tweet ID | Username | Text | User Location | Followers Count |
|---|---|---|---|---|---|---|---|---|
| JSON | YYYY-MM-DDTHH:MM:SSZ |
int | int | string | string | string | string | int |
- Add more tweet or user fields by modifying
paramsingetTweetDataWithStopSupport(). - Re-enable desktop notifications (using
node-notifier) by uncommenting thenotifier.notify(...)block.