Using the Twitter API with NodeJS
Tags: node, twitter, javascript, webdev
Published: 2021-11-05 14:22:32

Written by Lior Ben David.

---

There was a [study](https://www.pewresearch.org/internet/2018/04/09/bots-in-the-twittersphere/) 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:

https://developer.twitter.com/en/apply-for-access

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3u499mgv65ww1cq3kk0v.png)

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:

{% gist https://gist.github.com/LiorB-D/24f41bffeb785f2eea7902959be1e9eb %}

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:

{% gist https://gist.github.com/LiorB-D/f2091ddc091fbd6b663d25835b25f46f %}

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:

https://developer.twitter.com/en/docs/api-reference-index

To tweet something out programmatically:

{% gist https://gist.github.com/LiorB-D/73084083729ab4e003c27a6610321ee1 %}

And there you go!

![tweet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rh1gv04j9ssvpyuvzyeb.png)

---

###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](https://link.codesphere.com/FG), the most intuitive cloud provider on the market.