How to tweet using Twitter API and NodeJSšŸ‘‹

Sai Prasad Nayak
4 min readDec 19, 2021

--

Every techie should be part of the Twitter tech community where tech geeks from all over the world share their high-grade knowledge and network with other techies. To maintain my continuity of tweets, I wanted to connect my Notion page where I jot down my tweet ideas to create tweets automatically. So as of 1st part of this tutorial, I will explain how to programmatically tweet using the Twitter API.

Step 1: Let's get our Twitter API keys. Create your Twitter developer account at https://developer.twitter.com/

Note:- You must have your mobile number registered with your Twitter account to unlock your developer account.

Twitter developer portal dashboard.

Step 2: Now you need to create your app by clicking on the ā€œAdd Appā€ button. Fill in all the necessary details that are required. There are multiple degrees of access to Twitter API i.e Essential, Elevated & Academic Research. By default, you will have Essential access. For getting elevated or academic research access you need to fill the respective application form. Your application will be instantly verified and is free.

For our current requirement, the default Essential access will be absolutely fine.

Note:- I have elevated access so some extra information would be shown on my dashboard which you may not have in yours.

Give your app a name.

You will get your API keys and secret keys after this process as shown below.

Step 3: Now comes the most important phase i.e granting permissions to your app. This is the place where I was also stuck for a day and couldnā€™t tweet using API.

Go to the app settings that you created following the dashboard sidebar.

If you just want to read tweets using the API then please skip this step and follow Step 4. By default, your access token has read-only permission.

For changing the permission of your access token follow along with me.

Now you need to set up the Oauth settings. Click on the ā€œSet upā€ button as shown below.

Next, you need to check the OAuth 1.0a toggle and select ā€œRead and Writeā€ in the below settings as shown in the image below.

You will also need to fill in the general authentication settings. You can fill in dummy data since we wonā€™t require that for making tweets.

Step 4: Go to your app settings and then to the Keys & Tokens section. Now click on the ā€œGenerateā€ button for your access token & secret.

Once you have created make sure you have ā€œRead and Writeā€ permissions granted. For reference see the below image.

For people who came directly from Step 3 and only wanted to read tweets, they will have ā€œRead-onlyā€ permission written.

Note:- Make sure to copy and keep all your API keys and secrets, access tokens etc somewhere as they won't be shown later. You have to create a new app and start again from Step 2.

This ends the whole process of generating our Twitter API keys and access tokens along with granting required permissions.

Step 5: There is a very handy npm library for making use of Twitter API a lot simpler. It's called twitter šŸ˜„.

You just need to create a node app and write the following code into a new js file and run it. You can get tutorials from the internet if you are new to NodeJS.

Key points to remember:

consumer_key = API key

consumer_secret = API secret

access_token_key = Access Token

access_token_secret = Access Token Secret

Thatā€™s it, running the above code should send your 1st programmatic tweet. There are a lot of other options and configurations which you can explore on their GitHub page. You can also explore the official Twitter API docs here.

Thanks for staying along with me in this long blog. Enjoy āœŒ

--

--

Sai Prasad Nayak
Sai Prasad Nayak

Written by Sai Prasad Nayak

Crafting Code, Building Solutions

No responses yet