Tweeting using Node js
Tags: tutorial, twitter, node, javascript
Published: 2020-07-25 14:24:35

###Introduction
Twitter is a great social media platform where you can interact with a lot of people all over the world. But you can use twitter in a different way, **the Developers Way**. You can do that by consuming the Twitter API.

Twitter API gives you the capability to do what you do on twitter but from outside of it. You can build applications that can tweet, message, like, and retweet. There are many ways to do that, but here you’ll build an application using Node js.

Here you’ll use only Node Js and twitter lite to post tweets to your account.

###Create a twitter developer account
- First, you’ll need to have a developer account on Twitter, you can apply for it at [Twitter Developer Website](https://developer.twitter.com/en).
- After you created the developer account, log in to it
![Twitter developer website](https://dev-to-uploads.s3.amazonaws.com/i/x7wslh3jli6iqrjj1h3c.png)
- Create an App by clicking on **Dashboard** in the top right of the page, then under *Standalone Apps* click on **+ Create App**.
- Then you’ll only choose a unique name for your App
- That’s great. Now you have an App on twitter. We’ll get back to it in a few minutes.

###Using Node JS
- If you don’t have Node js yet, you have to install it from [Node JS website](https://nodejs.org/en/) and choose the LTS version.
- If you have it installed check its version in cmd using: 
 
```
node -v
```
- Now start building the application by creating a folder and name it TweetBot (you can change the name).
- Open cmd and type the following command to create package.json file:  
```
npm init -y
```
- Then add the twitter lite using npm with the command:  
```
npm install –save twitter-lite
```
- Open VSCode or any code editor and create a file and name it config.js and add the twitter-lite configurations as follows: 
 
```
module.exports = {  
  consumer_key: ' ' ,  
  consumer_secret: ' ',  
  access_token_key: ' ',  
  access_token_secret: ' '  
}
```
- The configuration values can be found in the app you created in the twitter developer website under *Keys and Tokens* link.  
>The latest twitter changes included changing the consumer key name to be API key, and consumer secret name to be API key secret.
- You may need to generate access token and secret.
- Now create an index.js file and start it by adding the configuration file and twitter-lite to the app as follows:  
```
const config = require('./config');
const twitter = require('twitter-lite');
const client = new twitter(config);
```
- Then, you create a request for the Twitter API.
- For tweeting, we can use the post request 'statuses/update'. You can see full details for this request [here](https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update).
- You'll use twitter lite to handle the request (for more information about twitter lite visit [their Github page](https://github.com/draftbit/twitter-lite))  
```
client.post('statuses/update', { status: 'Hello world!' }).then(result => {
  console.log('You successfully tweeted this : "' + result.text + '"');
}).catch(console.error);
```
- This post method consists of 2 parts: the first is the endpoint text (which is 'statuses/update'), the second is a parameter object that requires a status attribute that contains the text to be tweeted.
- If the request is successful, the result object will contain all tweet information. If an error happens, the catch method will log it to the console.
- Now go to cmd and type:
```
node index.js
```
- And voila!!! You tweeted from your application :muscle:

In the following tutorial, we will make like, retweet and follow bot, so stay tuned :wink:

For the full code, you can visit [my github page](https://github.com/amahallawy/TweetBot)

If you like my tutorials, support me here [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/F2F41UH5T) and follow me on Twitter [![Twitter URL](https://img.shields.io/twitter/url/https/twitter.com/ahmed_mahallawy.svg?style=social&label=Follow%20%40ahmed_mahallawy)](https://twitter.com/ahmed_mahallawy)

Pictures used in cover are:
- [Computer photo created by pressfoto](https://www.freepik.com/free-photos-vectors/computer)
- [Social media vector created by stories](https://www.freepik.com/free-photos-vectors/social-media)