npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ks-twitter-agent

v0.1.1

Published

A Twitter API client optimized for automation and persistent sessions (forked from agent-twitter-client). added correction for setCookie

Readme

Twitter Agent

A lightweight Twitter API client optimized for automation and persistent sessions.

Credit: This package is a fork of agent-twitter-client with added features for persistent cookie-based authentication and session management.

Installation

npm install twitter-agent

Quick Start

Method 1: Basic Authentication (One-time login)

const { Scraper } = require('twitter-agent');

const scraper = new Scraper();
await scraper.login(username, password, email, twoFactorSecret);

// Use the scraper
const tweets = await scraper.getTweets('elonmusk', 1);

Method 2: Cookie-based Authentication (Recommended)

This method allows you to maintain persistent sessions across script runs:

Step 1: Get Persistent Cookies (Do this once)

const scraper = new Scraper();
const cookies = await scraper.persistentLogin(
  username,
  password,
  email, // optional
  twoFactorSecret, // optional
);

// Save cookies for future use (e.g., in a database)
const cookieStrings = cookies.map((cookie) => cookie.toString());

Step 2: Use Saved Cookies (Subsequent runs)

// Option A: Load from cookie strings (e.g., from database)
const scraper = await Scraper.fromCookies(cookieStrings);

// Option B: Load from file
const scraper = await Scraper.fromCookiesFile('cookies.json');

Example: Complete Flow with Error Handling

const { Scraper } = require('twitter-agent');

async function getTwitterClient() {
  const COOKIES_FILE = 'twitter_cookies.json';
  let scraper;

  try {
    // Try to restore session from saved cookies
    if (fs.existsSync(COOKIES_FILE)) {
      scraper = await Scraper.fromCookiesFile(COOKIES_FILE);
      if (await scraper.isLoggedIn()) {
        return scraper;
      }
    }

    // If no valid cookies, login and save new ones
    scraper = new Scraper();
    const cookies = await scraper.persistentLogin(
      process.env.TWITTER_USERNAME,
      process.env.TWITTER_PASSWORD,
      process.env.TWITTER_EMAIL,
      process.env.TWITTER_2FA_SECRET,
    );

    // Save cookies for next time
    const cookieStrings = cookies.map((c) => c.toString());
    fs.writeFileSync(COOKIES_FILE, JSON.stringify(cookieStrings, null, 2));

    return scraper;
  } catch (error) {
    console.error('Authentication failed:', error.message);
    throw error;
  }
}

// Usage
const twitter = await getTwitterClient();
const tweets = await twitter.getTweets('elonmusk', 1);

Features

  • NEW: Persistent session management with cookies
  • NEW: Built-in session validation and recovery
  • Support for 2FA and email verification
  • Full Twitter API coverage (tweets, profiles, following, etc.)
  • Built-in rate limiting and error handling
  • TypeScript support

API Documentation

Authentication Methods

scraper.persistentLogin(username, password, email?, twoFactorSecret?)

Logs in and returns validated cookies for future use.

  • Returns: Promise<Cookie[]>

Scraper.fromCookies(cookies)

Creates a new scraper instance from cookie strings or Cookie objects.

  • Returns: Promise<Scraper>

Scraper.fromCookiesFile(path)

Creates a new scraper instance from a cookies JSON file.

  • Returns: Promise<Scraper>

Core Methods

scraper.getTweets(username, maxTweets = 200)

Fetches tweets from a user's timeline.

  • Returns: AsyncGenerator<Tweet>

scraper.searchTweets(query, maxTweets, searchMode?)

Searches for tweets matching the query.

  • Returns: AsyncGenerator<Tweet>

See the API Documentation for a complete list of methods.

Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Credit and Attribution

This project is based on agent-twitter-client by elizaOS, which provides the core Twitter API functionality. We've extended it with additional features for persistent authentication and session management.