Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
twitter-api-v2
Advanced tools
Strongly typed, full-featured, light, versatile yet powerful Twitter API v1.1 and v2 client for Node.js.
The twitter-api-v2 npm package is a comprehensive library for interacting with the Twitter API v2. It allows developers to perform a wide range of actions such as posting tweets, retrieving user information, managing direct messages, and more. The package is designed to be easy to use and supports both REST and streaming APIs.
Posting a Tweet
This feature allows you to post a tweet to your Twitter account. The code sample demonstrates how to initialize the TwitterApi client with an access token and post a tweet saying 'Hello, world!'.
const { TwitterApi } = require('twitter-api-v2');
const client = new TwitterApi('YOUR_ACCESS_TOKEN');
async function postTweet() {
const tweet = await client.v2.tweet('Hello, world!');
console.log(tweet);
}
postTweet();
Fetching User Information
This feature allows you to fetch information about a specific Twitter user by their username. The code sample shows how to retrieve and log the user information for the 'TwitterDev' account.
const { TwitterApi } = require('twitter-api-v2');
const client = new TwitterApi('YOUR_ACCESS_TOKEN');
async function getUserInfo() {
const user = await client.v2.userByUsername('TwitterDev');
console.log(user);
}
getUserInfo();
Streaming Tweets
This feature allows you to stream tweets in real-time based on certain criteria. The code sample demonstrates how to set up a stream to listen for tweets and log the tweet data as it comes in.
const { TwitterApi } = require('twitter-api-v2');
const client = new TwitterApi('YOUR_ACCESS_TOKEN');
async function streamTweets() {
const stream = await client.v2.searchStream({ 'tweet.fields': ['author_id'] });
for await (const { data } of stream) {
console.log(data);
}
}
streamTweets();
The 'twit' package is another popular library for interacting with the Twitter API. It supports both REST and streaming APIs, similar to twitter-api-v2. However, 'twit' is designed for the older Twitter API v1.1, whereas twitter-api-v2 is specifically for the newer API v2.
The 'twitter-lite' package is a lightweight alternative for interacting with the Twitter API. It supports both REST and streaming APIs and is designed to be minimalistic and efficient. While it can be used with both API v1.1 and v2, it may lack some of the more advanced features and ease of use provided by twitter-api-v2.
The 'node-twitter-api' package provides a simple interface for interacting with the Twitter API. It supports basic functionalities such as posting tweets and fetching user information. However, it is less comprehensive and may not support all the features available in twitter-api-v2.
Strongly typed, full-featured, light, versatile yet powerful Twitter API v1.1 and v2 client for Node.js.
✅ Ready for v2 and good ol' v1.1 Twitter API
✅ Light: No dependencies, 11.7kb minified+gzipped
✅ Bundled types for request parameters and responses
✅ Streaming support
✅ Pagination utils
✅ Media upload helpers
Sometimes, you just want to quickly bootstrap an application using the Twitter API. Even if they're a lot a available librairies on the JavaScript ecosystem, they usually just provide wrappers around HTTP methods, and some of them are bloated with many dependencies.
twitter-api-v2
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 comparaison with other libs:
Package | API version(s) | Response typings | Media helpers | Pagination | Subdependencies | Size (gzip) |
---|---|---|---|---|---|---|
twitter-api-v2 | v1.1, v2, labs | ✅ | ✅ | ✅ | 0 | ~11.7 kB |
twit | v1.1 | ❌ | ✅ | ❌ | 51 | ~214.5 kB |
v1.1 | ❌ | ❌ | ❌ | 50 | ~182.1 kB | |
twitter-lite | v1.1, v2 | ❌ | ❌* | ❌ | 4 | ~5.3 kB |
twitter-v2 | v2 | ❌ | ❌ | ❌ | 7 | ~4.5 kB |
*No support for media/upload
, cannot send a multipart/form-data
encoded-body without tricks
Here's the detailed feature list of twitter-api-v2
:
GET
, POST
, PUT
, DELETE
and PATCH
),
that handle query string parse & format, automatic body formatting and moreAnd last but not least, fully powered by native Promise
s.
Install it through your favorite package manager:
yarn add twitter-api-v2
# or
npm i twitter-api-v2
Here's is a quick example of usage:
import TwitterApi from 'twitter-api-v2';
// Instanciate 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 roClient = twitterClient.readOnly;
// Play with the built in methods
const user = await roClient.v2.userByUsername('plhery');
await twitterClient.v1.tweet('Hello, this is a test.');
// You can upload media easily!
await twitterClient.v1.uploadMedia('./big-buck-bunny.mp4');
// Or manually call the API
await twitterClient.v2.get('tweets/search/recent', { query: 'nodeJS', max_results: 100 });
const tweets = await twitterClient.get('https://api.twitter.com/2/tweets/search/recent?query=nodeJS&max_results=100');
Note: Top-level use of await
is not available in most of Node.js usage. You might need to wrap await
s into async functions. See MDN related documentation.
You want to know more about client usage? See the Basics!
Wanna see that in action? Jump to Examples part.
Lost between the different ways to auth inside Twitter API? Don't know how to implement 3-legged OAuth flow?
See Authentification part to know more and have a comprehensive guide a every Twitter authentification process.
APIs dedicated to streaming are available in Streaming part.
Each Twitter endpoint > method association is described in details inside the v1.1 comprehensive documentation and the v2 comprehensive documentation.
FAQs
Strongly typed, full-featured, light, versatile yet powerful Twitter API v1.1 and v2 client for Node.js.
The npm package twitter-api-v2 receives a total of 87,077 weekly downloads. As such, twitter-api-v2 popularity was classified as popular.
We found that twitter-api-v2 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.