DEV Community

Cover image for Using the Twitter API with NodeJS
Simon Pfeiffer for Codesphere Inc.

Posted on • Updated on

Using the Twitter API with NodeJS

Written by Lior Ben David.

There was a study done by Pew Research a number of years ago that found that ⅔ of all tweets with links to popular websites. To the average Twitter user that probably doesn't come as a surprise.

Twitter is incredibly easy to automate, in fact, today I'm going to show you just how easy it is to use the Twitter API with NodeJS to read, search, and post tweets.

Setup Your Twitter Dev Account

Because of the sheer volume of Twitter bots, Twitter requires an application before you can use their API. While you can start using your API key as soon as you submit the application, some features might be restricted until it is reviewed.

Apply here:

Once your application is complete, navigate to your developer dashboard and create a new application. Once, you generate your application, go to settings and make sure to turn on Read and Write Permissions:

app permissions

Next, head to the keys menu, and generate and copy your API and Access tokens. We'll use them to authenticate next.

Setting Up Our Script

Since we're going to be using NodeJS to work with the Twitter API, create a new NPM project with:

npm init

And go through the standard questions. Next, we are going to install a great wrapper for the Twitter Developer API. While this isn't necessary, it will make authentication much cleaner.

npm install twitter-api-v2

Once that's done, we can create and authenticate our client like so:

And of course, fill in the relevant API keys and tokens for your app.

Pulling and Posting Tweets

Now let's showcase some of the API calls you can make.
To get data about a tweet from its ID:

In the tweet.fields you can put all the specific data you want. To get a full list of all the parameters you can use:

To tweet something out programmatically:

And there you go!


Use Cases

Virtually anything that you might do normally on Twitter can be done through the API. That means that any automation you might want to do is completely possible. This includes:

  • Automated Run Accounts
  • Automatically retweet certain articles based on hashtags(Aggregation accounts)
  • Track trendings topics for algorithmic trading
  • Sentiment Analysis on tweets
  • Data analysis for your company's Twitter campaigns

So what are you going to build? Let us know down below!

If you're looking for a place to host your Twitter bot, try coding, debugging, and deploying on Codesphere, the most intuitive cloud provider on the market.

Top comments (5)

lerobot profile image
Le Robot

great article thanks

mustapha profile image
Mustapha Aouas

Great article, thanks for sharing 👍🏻
I wonder how you could automate a tweet with a thread 🧵 🤔

gathu17 profile image
Jerry Gathu

Nice article

php4fan profile image

"Once, you generate your application, go to settings and make sure to turn on Read and Write Permissions"

Where is that???

crypticwisdom8 profile image
Cryptic Bot • Edited

There are changes to twitter policy, now you have to apply for elevated access before you can post tweets. I have written about it here .