Open-source project
plhery/node-twitter-api-v2 avatar
plhery/node-twitter-api-v2

twitter-api-v2 for Typed Twitter API Access in Node.js

Strongly typed, full-featured, light, versatile yet powerful Twitter API v1.1 and v2 client for Node.js.

1,555 stars210 forksTypeScriptApache-2.0

At a glance

What is it?
twitter-api-v2 is a TypeScript-oriented Node.js client for Twitter API v1.1 and v2, with bundled response types, endpoint wrappers, streaming, pagination, and media helpers. Its main trade-off is its close coupling to Twitter's endpoint models and access tiers.
Who is it for?
Node.js developers that use TypeScript and need typed Twitter endpoint wrappers should adopt twitter-api-v2 after confirming that their required endpoints and access tiers are covered. Projects limited to v2, indifferent to bundled typings, or seeking the smallest possible client should compare the documented lighter alternatives first.
Can I use it commercially?
Yes. Apache-2.0 is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 13 days ago.
What is it written in?
Mainly TypeScript, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 7, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • plhery/node-twitter-api-v21,555 ★ · +0
Star history
Repository2026-10-192026-11-07
plhery/node-twitter-api-v21.6k1.6k

0 Stars in the last 19 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

Why twitter-api-v2 Covers More Than Raw Twitter Requests

twitter-api-v2 targets Node.js applications that need direct access to Twitter API v1.1 or v2 rather than a thin wrapper around HTTP calls. The package provides named methods for endpoints, bundled types for request parameters and response payloads, and typed error structures. This is aimed at developers who want the compiler to expose invalid parameters or mismatched response handling during development.

The client is split by Twitter permission level. A read-only client exposes endpoints available to a read-only application, while the main client can also reach write and direct-message methods when its authentication grants the corresponding rights. Access is not determined by TypeScript alone. Twitter still evaluates the app's access level, credentials, and permissions, so a method appearing on a client does not prove that the server will authorize the request.

Authentication support includes OAuth 1.0a, OAuth 2.0, and Basic HTTP Authorization. OAuth 2.0 can also use user context rather than only application or bearer authentication. The package includes helpers for authentication links and token exchange flows. That makes it relevant to applications that need to act for a user, publish posts, or call protected endpoints, as well as services that only retrieve public data with application credentials.

The repository also documents v1.1, v2, labs, and Ads API coverage, although the Ads API documentation is marked beta. API version support therefore does not mean every account or plan can call every listed endpoint.

Typed Clients, Request Wrappers, and Response Data

The package separates CommonJS and ES module builds and points TypeScript users to bundled declarations. `package.json` identifies `dist/cjs/index.js` as the CommonJS entry point, `dist/esm/index.js` as the module entry point, and `dist/esm/index.d.ts` as the type entry point. This packaging is useful for Node.js code that needs typed imports without sourcing community declarations separately.

Beyond dedicated endpoint wrappers, the README lists direct helpers for `GET`, `POST`, `PUT`, `DELETE`, and `PATCH`. Those helpers handle request URL and body concerns such as query-string parsing and formatting. They are the escape hatch for endpoints without a dedicated wrapper, but using them forfeits much of the endpoint-specific typing that is the main reason to choose this package. Consumers should check the v1.1 and v2 endpoint documentation before assuming every Twitter route has a named method.

Pagination is treated as a first-class concern. The documentation describes automatic paginators for endpoints such as user and tweet timelines, with asynchronous iteration that can continue until the applicable rate limit is reached. This is preferable to manually copying pagination tokens in code, but it can still consume substantial API allowance. The README does not document a configurable item cap or a universal stop condition, so callers should account for the endpoint's rate limit and the application's processing needs.

Error handling also spans both supported API generations. The package documents typed errors, descriptive messages, and error enumerations for v1.1 and v2. Applications can therefore distinguish documented API failures from local TypeScript errors, although the README does not claim that every possible Twitter response is represented.

Streaming, Media Uploads, and Runtime Trade-offs

Streaming support includes data consumption and automatic reconnection after stream errors according to the README. This applies to Twitter streaming endpoints documented for both v1.1 and v2. Automatic reconnection simplifies a persistent consumer, but it does not make an unbounded stream free of operational concerns. The provided documentation does not state retry limits, backoff settings, shutdown procedures, or a guarantee against duplicates, so those behaviors should be checked in `doc/streaming.md` before relying on exactly-once processing.

Media handling is centered on API v1.1. The package documents automatic media-type detection, chunked uploads, concurrent uploads, long video support, and subtitle support. Its README example begins with a v1.1 media request even though the application also uses v2 methods:

ts
await twitterClient.v1.uploadMedia('./big-buck-bunny.mp4');

This illustrates a mixed-version design. Code can use v2 for tweets and users while relying on v1.1 for an upload helper. Such mixing can be necessary for a particular workflow, but it also means teams must understand which API version owns each operation and whether their app has access to both.

The package advertises no runtime dependencies and an approximate minified, compressed size of 23 kB. Those are useful attributes for a dependency-sensitive service, but they do not remove the cost of Twitter's endpoint complexity. Bundled models, endpoint methods, authentication variants, and compatibility code can be substantial concerns for an application that needs only one request. The README's comparison places `twitter-lite` and `twitter-v2` at smaller compressed sizes, although neither provides the same documented response typings, media helpers, or pagination support.

Installing twitter-api-v2 and Making Initial Requests

The npm package is named `twitter-api-v2`. The README offers both Yarn and npm installation. For an npm-based TypeScript project, install it as a dependency:

bash
npm i twitter-api-v2

The README also documents the Yarn equivalent:

bash
yarn add twitter-api-v2

Choose one package manager for the project rather than running both. After installation, import `TwitterApi`, construct a client with an app-level token, and derive `readOnlyClient` for a request restricted to read endpoints. The README gives this sequence:

ts
import { TwitterApi } from 'twitter-api-v2';

const twitterClient = new TwitterApi('<YOUR_APP_USER_TOKEN>');
const readOnlyClient = twitterClient.readOnly;

A first request can retrieve a user by username:

ts
const user = await readOnlyClient.v2.userByUsername('plhery');

The success result should contain the response described by the bundled v2 user types. The token placeholder must be replaced with credentials issued for the intended Twitter application. Do not place a production token in committed source; the README excerpt does not prescribe an environment variable, so a configuration name cannot be inferred from it.

A client with write access can also post a tweet. Whether Twitter accepts that request depends on the app's access level and authentication context:

ts
await twitterClient.v2.tweet('Hello, this is a test.');

The read and write calls belong to different permission surfaces. Keeping a read-only reference for lookup code makes the intended access level visible, while retaining the main client only where write or user-context operations are needed.

Plugins, Licensing, and Version 1.29.1 Maintenance

Optional concerns are separated into official plugins rather than placed in the core runtime dependency list. `@twitter-api-v2/plugin-token-refresher` handles OAuth 2.0 user-context token refreshing. `@twitter-api-v2/plugin-rate-limit` exposes and stores rate-limit data. `@twitter-api-v2/plugin-cache-redis` stores responses in Redis and serves cached responses. Each plugin adds a separate installation and upgrade decision, so the core package's lack of dependencies should not be interpreted as a guarantee that a deployment using plugins has no external package dependencies.

The package uses the `Apache-2.0` license. That is a permissive open source license, but application owners still need to review its notice and license obligations for their distribution model. This review is a compliance task rather than evidence that Twitter API use itself is unrestricted. Twitter access remains governed by the API products, account tiers, credentials, and terms selected by the developer.

Version `1.29.1` was released on 2026-08-04, matching the repository's last push on 2026-08-04. The preceding releases listed are `1.29.0` from 2026-01-13 and `1.28.0` from 2025-11-15. That release cadence gives the package visible ongoing change, but upgrades still require compatibility work. A published minor or patch release can alter typings as Twitter response models evolve. Applications should review `changelog.md` and endpoint typings rather than assuming every update is runtime-neutral.

The repository separates source under `src/`, tests under `test/`, documentation under `doc/`, and CommonJS and ES module build configurations. The `prepublish` script runs the build, which reduces the chance of publishing stale output, but consumers should still build their own project against the installed declarations before deployment.

Choosing Between twitter-api-v2 and Lighter Twitter Clients

The clearest alternative described by the README is a lightweight client such as `twitter-lite` or `twitter-v2`. Those packages take a narrower approach. `twitter-lite` covers v1.1 and v2, while `twitter-v2` focuses on v2. The README comparison reports no response typings, media helpers, or pagination utilities for either package, and lists fewer compressed bytes than `twitter-api-v2`. They may therefore fit a small script that performs a limited set of requests and does not need the full client surface.

The older `twit` and `twitter` packages are different alternatives because the README lists them as v1.1 clients. Both have larger compressed sizes than `twitter-api-v2`, but the project comparison reports many more subdependencies, no response typings, and no pagination support. `twit` includes media helpers according to the table. These options matter when v1.1 compatibility is required, the project already follows one of their APIs, or adding this package's broader v2 surface would be unnecessary.

The decision should follow endpoint and type requirements, not package size alone. Choose `twitter-api-v2` when TypeScript response models, named v1.1 and v2 methods, paginators, media helpers, or streaming reconnect behavior are part of the application. A smaller generic client is more appropriate when the application makes a few fixed calls and does not benefit from those abstractions. A direct HTTP integration is the other option, but then the application owns endpoint URLs, query construction, authentication signing, pagination, response validation, and Twitter-specific errors itself.

Editorial conclusion

Node.js developers that use TypeScript and need typed Twitter endpoint wrappers should adopt twitter-api-v2 after confirming that their required endpoints and access tiers are covered. Projects limited to v2, indifferent to bundled typings, or seeking the smallest possible client should compare the documented lighter alternatives first. Verify authentication requirements, endpoint availability, media constraints, and the runtime cost of upgrading from version 1.29.1 before integration.

Frequently asked questions

How do I install twitter-api-v2 and create a TypeScript client?

Run `npm i twitter-api-v2` or `yarn add twitter-api-v2`, then import `TwitterApi` and construct it with the intended application token. The package publishes TypeScript declarations with its ES module build.

Does twitter-api-v2 support Twitter API v1.1 as well as v2?

Yes. It provides named wrappers for both versions, and its README also lists labs and Ads API coverage, with Ads API documentation marked beta. Availability still depends on the app's Twitter access and permissions.

Does twitter-api-v2 provide pagination, streaming, and media upload helpers?

The README documents asynchronous paginators, streaming consumption with automatic reconnection, and v1.1 media uploads with chunked, concurrent, long-video, and subtitle support. These are separate capabilities within one client rather than separate packages.

Official sources

  1. License: Apache-2.0
  2. plhery/node-twitter-api-v2 on GitHub
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/plhery-node-twitter-api-v2.svg)](https://hysenlabs.com/projects/plhery-node-twitter-api-v2)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/plhery-node-twitter-api-v2.svg)](https://hysenlabs.com/projects/plhery-node-twitter-api-v2)

Was this useful?