PLhery / node-twitter-api-v2

Strongly typed, full-featured, light, versatile yet powerful Twitter API v1.1 and v2 client for Node.js.
https://www.npmjs.com/package/twitter-api-v2
Apache License 2.0
1.31k stars 186 forks source link
npm-package streaming-api twitter-api

Twitter API v2

Version badge Checks badge Package size badge

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

Main maintainer: @alkihis - Buy Me A Coffee

Important Note

Twitter will significantly reduce its API capabilities by end of April (see this thread).

This change has major implications, and as a result, this library may no longer be maintained.

We are disappointed and discouraged by the recent turn of events at Twitter since the takeover by Elon Musk. We are saddened to see that much of the hard work of the past few years on the API, led by an amazing team including @andypiper, has been shelved.

For a more detailed explanation, please see this discussion.

Highlights

Ready for v2 and good ol' v1.1 Twitter API

Light: No dependencies, 23kb minified+gzipped

Bundled types for request parameters and responses

Streaming support

Pagination utils

User-context authentication with OAuth2

Media upload helpers

How to use

Install it through your favorite package manager:

yarn add twitter-api-v2
# or
npm i twitter-api-v2

Here's a quick example of usage:

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

// Instantiate with desired auth type (here's Bearer v2 auth)
const twitterClient = new TwitterApi('<YOUR_APP_USER_TOKEN>');

// Tell typescript it's a readonly app
const readOnlyClient = twitterClient.readOnly;

// Play with the built in methods
const user = await readOnlyClient.v2.userByUsername('plhery');
await twitterClient.v2.tweet('Hello, this is a test.');
// You can upload media easily!
await twitterClient.v1.uploadMedia('./big-buck-bunny.mp4');

Why?

Sometimes, you just want to quickly bootstrap an application using the Twitter API. Even though there are a lot of libraries available on the JavaScript ecosystem, they usually just provide wrappers around HTTP methods, and some of them are bloated with many dependencies.

twitter-api-v2 is meant to provide full endpoint wrapping, from method name to response data, using descriptive typings for read/write/DMs rights, request parameters and response payload.

A small feature comparison with other libs:

Package API version(s) Response typings Media helpers Pagination Subdeps Size (gzip) Install size
twitter-api-v2 v1.1, v2, labs 0 ~23 kB twitter-api-v2 install size badge
twit v1.1 51 ~214.5 kB twit install size badge
twitter v1.1 50 ~182.1 kB twitter install size badge
twitter-lite v1.1, v2 4 ~5.3 kB twitter-lite install size badge
twitter-v2 v2 7 ~4.5 kB twitter-v2 install size badge

Features

Here's everything twitter-api-v2 can do:

Basics:

Request helpers:

Type-safe first:

And last but not least, fully powered by native Promises.

Documentation

Learn how to use the full potential of twitter-api-v2.

Plugins

Official plugins for twitter-api-v2:

See how to use plugins here.