An object-oriented Node.js and TypeScript library for interacting with Twitter API v2
Fetching details about a twitter user is as easy as this with twitter.js
library:
import { Client } from 'twitter.js';
import { bearerToken } from './secrets.js';
const client = new Client();
client.on('ready', async () => {
const user = await client.users.fetchByUsername({
username: 'iShiibi',
});
console.log(user.description); // Contributing to open-source 🌐
});
client.loginWithBearerToken(bearerToken);
twitter.js
also provides you the ability to make user-context
authorized requests without any hassle:
import { Client } from 'twitter.js';
import { credentials } from './secrets.js';
const client = new Client();
client.on('ready', async () => {
console.log(`Logged in as ${client.me.username}`); // Logged in as tjs_test
const user = await client.users.fetchByUsername({
username: 'iShiibi'
});
await user.follow();
});
client.login(credentials);
And did we tell you that you can opt-in for real-time events listening? With twitter.js
, you can set rules that tell Twitter API to send you tweets matching those rules in real-time. Here is a sample code for a bot that likes every tweet it gets mentioned in:
import { Client } from 'twitter.js';
import { credentials } from './secrets.js';
const client = new Client({ events: ['FILTERED_TWEET_CREATE'] });
client.on('ready', async () => {
console.log(`Logged in as ${client.me.username}`); // Logged in as tjs_test
await client.filteredStreamRules.create({ value: '@tjs_test', tag: 'Tweets mentioning the user tjs_test' });
});
client.on('filteredTweetCreate', async tweet => {
console.log(`${tweet.text}`); // hey @tjs_test, like this tweet if you're listening!
await tweet.like();
});
client.login(credentials);
The twitter.js
library is not ready for production use yet. You can expect breaking changes without any major version bump until we release v1.0.0
of the library.
There is an autogenerated documentation website for API references. We hope to replace it with a custom one soon. If you love web development, then feel free to discuss how you can contribute towards this goal in our Discord server.
We are also building a guide website to teach new users how to use the twitter.js
library.