The tweet.js script opens X (Twitter) in Playwright, logs in (or uses saved state from twitter-auth.json), and posts a tweet. The post text comes from TWEET_TEXT.
- Node.js 18+ and npm
- Playwright browsers installed:
npm installandnpx playwright install - X account with username/password and optionally a TOTP secret for 2FA
- Install dependencies and browsers:
npm install npx playwright install
- Copy and fill env vars:
In
cp .env.example .env
.env, setTWITTER_USERNAMEandTWITTER_PASSWORD. For 2FA you can addTWITTER_2FA_SECRET(TOTP secret) or a one-timeTWITTER_2FA_CODE.
TWEET_TEXT— post text (default: "Привет, X!").BROWSER—chromium(default) orwebkit.HEADLESS—true|false; withMANUAL_LOGIN=truethe window is always visible.MANUAL_LOGIN—true|false; whentrue, log in manually in the opened browser, then press Enter in the terminal.TWITTER_USERNAME,TWITTER_PASSWORD— used for automatic login (MANUAL_LOGIN=false).TWITTER_2FA_SECRETorTWITTER_2FA_CODE— pass 2FA without manual input.- Session persists to
twitter-auth.jsonnext to the script.
Auto-login with creds from .env:
TWEET_TEXT="Post via Playwright" npm run tweetFirst run with manual login (if there are captchas/confirmations):
MANUAL_LOGIN=true HEADLESS=false npm run tweet
# log in in the opened browser, return to the terminal and press EnterSubsequent background run using saved session:
HEADLESS=true TWEET_TEXT="New post" npm run tweetDelete twitter-auth.json to log in again (manual or automatic).