DEV Community

Cover image for Post a tweet from an API using Node and Expressjs
Kishore S
Kishore S

Posted on • Updated on

Post a tweet from an API using Node and Expressjs

Here are the simple steps to follow to post a tweet from Nodejs application. Before that, there is some prerequisite so, let’s get started.

  1. First, you will need a Twitter Developer Account to get the required keys. Sign up for the account and fill the necessary details. Make sure you elaborate on the details that they have asked this ensure’s them to approve your application quickly.
  2. Once your application is approved, just go to the header section there you can see your name, click that under you can see a section called apps.
  3. After that click create an app and there fill the details asked by them and once the app is created, go to keys and token section there you can find four keys.

Once you completed the above steps, now it’s time to create the Nodejs application that can post a tweet from your API to your twitter account.

Open your editor and in the terminal create a Nodejs application using the npm init command. Once done create a .js file.

For this application, we are using Expressjs which is a Nodejs framework for routing. Download the package using the command npm install express. Once downloaded import the express in your file.

You will need an npm package called Twit Package that provides us an API to post our tweet from our application. Once downloaded, import the package in your file.

Then we need to create an instance from the class provided by Twit package using the new keyword and we need to provide the four keys that we got from the twitter developer account.

screenshot-1

To be more flexible it would be great to create a query parameter and send the message there so you don’t have to manually change the message in the post method each time. Create a post method in your application.

screenshot-3

Twit provide us a method called post and the methods accepts three-parameter:

  • First parameter is the route API provided by twitter.
  • Second parameter is an object called status and the value is the message to be posted in your twitter as feed.
  • Third is a callback function that gets fired once the message has been posted in your twitter feed and we can use this function to send a response to your application that the task is completed.

screenshot-2

Now type the message you want to post in your twitter feed in the object that was passed as a third parameter the post method of Twit.

Once all the steps have been completed run your Node application using the node and you can check your twitter account whether the message was posted or not.

That’s it you have created a Nodejs application that can post a tweet from your API to your twitter account.

To refer the code Here's my CodeSandBox link of the source code, feel free to modify the code and also I have added extra methods that let you get the latest tweet from your twitter timeline.

Connect with me on Twitter @kishoreio

Top comments (0)