Post a tweet from an API using Node and Expressjs
Tags: node, javascript, webdev
Published: 2020-04-18 19:20:53

<p>
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.
</p>
<ol>
<li>First, you will need a <a href="https://developer.twitter.com/en" target="_blank">Twitter Developer Account</a> 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.</li>
<li>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.</li>
<li>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.</li>
</ol>
<p>
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.
</p>
<p>
Open your editor and in the terminal create a Nodejs application using the npm init command. Once done create a .js file.
</p>
<p>
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.
</p>
<p>
You will need an npm package called <a href="https://www.npmjs.com/package/twit" target="_blank">Twit Package</a> that provides us an API to post our tweet from our application. Once downloaded, import the package in your file.
</p>
<p>
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.
</p>
![screenshot-1](https://img.techpowerup.org/200528/carbon.png)
<p>
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.
</p>
![screenshot-3](https://img.techpowerup.org/200528/carbon-1385.png)
<p>Twit provide us a method called post and the methods accepts three-parameter:</p>
<ul>
<li>First parameter is the route API provided by twitter.</li>
<li>Second parameter is an object called status and the value is the message to be posted in your twitter as feed.</li>
<li>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.</li>
</ul>
![screenshot-2](https://img.techpowerup.org/200528/carbon-1.png)
<p>
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.
</p>
<p>
Once all the steps have been completed run your Node application using the node <filename> and you can check your twitter account whether the message was posted or not.
</p>
<p>
That’s it you have created a Nodejs application that can post a tweet from your API to your twitter account.
</p>
<p>
To refer the code Here's my <a href="https://codesandbox.io/s/twitter-api-yrrqf" target="_blank">CodeSandBox</a> 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.
</p>

Connect with me on Twitter @<a href="https://twitter.com/kishoreio" target="_blank">kishoreio</a>