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

@higuchan123/twitter_lib

v1.0.5

Published

Node.js library for interacting with X web endpoints and X Chat flows.

Readme

twitter_lib.js

twitter_lib.js is a JavaScript library for automating X (Twitter) web actions.
twitter_lib.js は、X (Twitter) のWeb操作を自動化する JavaScript ライブラリです。

26/03/18 update

  • Updated the X-Client-Transaction-ID generation logic to reflect recent changes.
  • X-Client-Transaction-idの生成ロジックが変更されたのでそれに伴いアップデート

Latest X Chat/DM traffic is fully encrypted and not easy to reverse engineer, but this library provides a full reverse-engineered implementation with curl-cffi/fetch only, so encrypted DM/Chat can be operated easily from the library.
X の最新 Chat/DM の通信はすべて暗号化されておりリバースエンジニアリングは容易ではありませんが、このライブラリは curl-cffi/fetch のみでフルリバースエンジニアリング実装されており、暗号化された DM/Chat をライブラリから簡単に操作できます。

This is a full-request library built with curl-cffi/fetch only, and it does not use browser automation tools such as Playwright.
これは curl-cffi/fetch only で構成されたフルリクエストライブラリで、Playwright などのブラウザ自動操作ツールは使用していません。

Installation / インストール

npm i @higuchan123/twitter_lib

Install from npm with the command above.
上記コマンドで npm からインストールできます。

What This Library Can Do / このライブラリでできること

Encrypted X Chat and DM Operations / 暗号化X Chat・DM操作

All of the following are supported even for encrypted DMs.
以下はすべて、暗号化された DM でも対応可能です。

  • Chat recovery key generation / Chat リカバリーキー生成
  • Fetch DM inbox list / DM 一覧取得
  • Fetch DM conversation content / DM 内容取得
  • Send DM text messages / DM テキスト送信
  • Send DM image messages / DM 画像メッセージ送信
  • Accept DM requests / DM リクエスト承諾
  • Create new DM flow / DM 新規作成フロー

Accurate Browser-like X-Client-Transaction-Id / 高精度ブラウザ寄りX-Client-Transaction-Id

  • Precise, browser-like X-Client-Transaction-Id generation
  • 正確でブラウザ挙動に近い X-Client-Transaction-Id の生成

Core X Actions / 基本的なX操作

  • Retrieve all API-compatible query IDs / APIに対応するqueryIdの全取得
  • Automatic Bearer token retrieval / Bearer tokenの自動取得
  • Automatic cookie retrieval / cookieの自動取得
  • Follow users / ユーザーのフォロー
  • Fetch user tweets list / ユーザーツイート一覧取得
  • Fetch tweet info / ツイート情報取得
  • Tweet posting / ツイート投稿
  • Like / いいね
  • Bookmark / ブックマーク
  • Retweet / リツイート
  • Reply / リプライ
  • Pin tweet / 固定ツイート
  • Unpin tweet / 固定ツイート解除
  • Delete tweet / ツイート削除
  • Account language change / アカウント言語変更
  • Proxy Support / Proxy対応
  • Edit profile (name, bio, location, icon) / プロフィール編集

Function Call Examples / 関数呼び出し例

Core X Actions additions:
- Pin tweet / 固定ツイート
- Unpin tweet / 固定ツイート解除
- Delete tweet / ツイート削除

const lib = require("@higuchan123/twitter_lib");

const authToken = process.env.AUTH_TOKEN;
const passcode = process.env.XCHAT_PASSCODE;

// 1) Recover encrypted X Chat secret
// 1) 暗号化X Chatシークレットを復元
const xchat = await lib.get_xchat_recovered_secret(authToken, passcode);

// 2) Fetch DM inbox list
// 2) DM一覧取得
const inbox = await lib.fetch_xchat_inbox(xchat.cookies, xchat.bearer_token);

// 3) Fetch and decrypt DM conversation content
// 3) DM内容取得と復号
const conversationId = "1234567890-0987654321";
const page = await lib.fetch_xchat_conversation_page(
  xchat.cookies,
  xchat.bearer_token,
  conversationId,
  50
);
const decrypted = lib.decrypt_xchat_conversation_events(
  page,
  xchat.recovered_secret_hex,
  xchat.self_user_id
);

// 4) Send encrypted DM text
// 4) 暗号化DMテキスト送信
await lib.send_xchat_text_message(
  xchat.cookies,
  xchat.bearer_token,
  xchat.recovered_secret_hex,
  xchat.token_map,
  conversationId,
  "Hello from @higuchan123/twitter_lib",
  xchat.self_user_id
);

// 5) Send encrypted DM image
// 5) 暗号化DM画像送信
await lib.send_xchat_image_message(
  xchat.cookies,
  xchat.bearer_token,
  xchat.recovered_secret_hex,
  xchat.token_map,
  conversationId,
  "./image.jpg",
  xchat.self_user_id
);

// 6) Accept DM requests
// 6) DMリクエスト承諾
const requests = await lib.fetch_dm_requests(authToken, { limit: 20 });
for (const req of requests.requests || []) {
  await lib.accept_dm_request(authToken, req.conversation_id);
}

// 7) New DM creation flow (send path for new conversation)
// 7) DM新規作成フロー(新規会話への送信パス)
const recipientUserId = "1111111111";
const newConversationId = `${xchat.self_user_id}-${recipientUserId}`;
await lib.send_xchat_text_message(
  xchat.cookies,
  xchat.bearer_token,
  xchat.recovered_secret_hex,
  xchat.token_map,
  newConversationId,
  "First message in a new DM",
  xchat.self_user_id
);

// 8) Browser-like transaction id generation
// 8) ブラウザ寄りTransaction ID生成
const gen = new lib.TransactionIdGenerator(homeHtml, ondemandJsText);
await gen.ready();
const transactionId = await gen.generate(
  "GET",
  "https://x.com/i/api/graphql/.../HomeTimeline?..."
);

// 9) Core X actions
// 9) 基本的なX操作
await lib.post_tweet(authToken, "hello world");
await lib.like_tweet(authToken, "https://x.com/user/status/123");
await lib.bookmark_tweet(authToken, "https://x.com/user/status/123");
await lib.retweet(authToken, "https://x.com/user/status/123");
await lib.reply_tweet(authToken, "https://x.com/user/status/123", "Nice post!");
await lib.set_account_language(authToken, "ja");
await lib.update_account_profile(authToken, {
  name: "new name",
  description: "new bio",
  location: "Tokyo, Japan",
  icon_path: "./icon.jpg",
});

Proxy Support / Proxy対応

twitter_lib supports HTTP/HTTPS proxies.
twitter_lib は HTTP/HTTPS プロキシに対応しています。

You can pass proxy settings per call with proxy_url (or proxy) in options.
各関数の options に proxy_url(または proxy)を渡して設定できます。

You can also set environment variables: PROXY_URL, HTTPS_PROXY, HTTP_PROXY, ALL_PROXY.
環境変数 PROXY_URL, HTTPS_PROXY, HTTP_PROXY, ALL_PROXY でも設定できます。

const lib = require("@higuchan123/twitter_lib");

const authToken = process.env.AUTH_TOKEN;
const proxyUrl = "http://username:password@host:port";

// Create a shared proxied session
// プロキシ付きセッションを作成
const session = lib.create_session({ proxy_url: proxyUrl });

// Use the session for API calls
// 作成したセッションを各API呼び出しで利用
const bearer = await lib.get_bearer_token(session);
const queryIds = await lib.get_all_query_ids(session);
const timeline = await lib.fetch_home_timeline_pages(authToken, {
  session,
  bearer_token: bearer,
  limit: 20,
});

Proxy:
http://username:[email protected]:1212

For the complete library usage guide, see:
ライブラリの完全な使用方法は以下を参照してください:
https://github.com/GODFIREWA/twitter-lib/blob/main/twitter_lib.md