Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 1083 of the free egghead.io lessons, plus get Node.js content delivered directly to your inbox!

Existing egghead members will not see this. Sign in.

Set Up a Basic Twitter Bot with Twit.js

4:46 Node.js lesson by

We’ll go over the basic Twitter bot setup, including how to start a project from scratch, install necessary packages, and store our private keys in environment variables.

Get the Code Now
click to level up

egghead.io comment guidelines


We’ll go over the basic Twitter bot setup, including how to start a project from scratch, install necessary packages, and store our private keys in environment variables.


One suggestion would be doing all your videos in ES6. It's the standard now. Does anybody actually use var anymore?

In reply to egghead.io
Brenton Morris

Michael. You are a moron.

In reply to Michael
Stephen James

I suggest going with the dotenv package. Rather than bash_profile or profile then you can have different settings for different apps.

We're going to make a directory for our first Twitter bot. To set up our project, we're first going to do npm init we can kind of press enter through all of these. This will create our package.json file, which we can see here.

We're also going to make our index.js file and a ReadMe. First, we're going to install and we'll do --save twit, which is the library we'll be working with that interface with the Twitter API. That --save flag saves Twit to our dependency list, which we can see here in package.json.

The next step we need to do to make a Twitter bot, is to go to twitter.com/apps and Create New App. This is going to be our bot that we use to learn a lot of things, so we'll call it the LearningBot. We can skip the callback URL for now, and we'll create our app.

Ah, so we need a different name, let's try TheLearningBot, and that works. Once we've gotten to this point, we'll need four tings. I'm not going to show you the actual keys and access tokens, but if you go to this Keys and Access Tokens tab, there will be two listed at the top called the consumer key and the consumer secret, which we'll talk about in a second.

Further down the page, there will be a section called your access token. You want to click this link here that says Create My Access Token. That will generate two additional things, an access token and an access token secret. We'll come back to that in a minute, just remember where to find those variables.

Let's go back to our code for a minute. First thing we're going to do is var twit = require twit. Then to create our bot, we need to pass it all those keys we just created. The syntax for that is var bot = new twit, we this consumer key, we'll need consumer secret, we'll the access token, we'll need the access token secret.

Then we'll also have a timeout variable for any HTTP requests which we'll set to one minute. We need to go back to Twitter and get this information. We'll use this as a fake one, temporarily, this is not a real key.

We don't want to just put our keys in our code since we'll be uploading it to GitHub and everything like that. Instead, we want to put our Twitter information in environment variables. The way we do that, is we go to our home folder, and we're going to open something called .bashProfile.

If you don't have this file but you do have .profile, you can also put your environment variables in there. This will save our environment variables so that we can access them locally. The syntax here is export whatever you want your variable name to be, so let's say learningBotConsumerKey, and then I'm going to make one up here.

Export learningBotConsumerSecret, export learningBotAccessToken, and then export learningBotAccessTokenSecret. Once you have that, go back here and say source.bashProfile, and that will make sure your environment variables are saved.

The way to get your environment variables back into Node is to say process.env. the name of the variable, so for this it is learningBotConsumerKey, and you'll do a comma after that, process.env.learningBotConsumerSecret, so now we just input the last two variables. Then if we navigate back to our code, we're all set up.

Joel's Head
Why are we asking?