Today We will be learning How to talk to ‘Twitter’ using Twitter API. With ‘Twit Package’ of NPM(Node Package Manager), It got easier to directly talk to Twitter i.e Post tweets, read tweets, post multimedia and do other stuff, Sounds interesting to you, but wondering How to do it?
Come let’s do it.
The very first thing you need to install is Node into your system.
You can download node from here Click here to download Node
Once you have downloaded Node, check the version you are using by typing ‘node –version’
Now to start a new project we will make a new folder called ‘Twit’ or any other of your choice.This will be the project folder which will have all the files. Now using the command line we will go the folder and type ‘npm init’. The moment you say npm init , It will ask a few questions like
Once it is done You will have a package.json file inside the project folder. A package.json file is a json file which is a settings or config file and contains “dependencies”.
After this, we will install the ‘Twit’ package from npm using the following command ‘npm install twit – -save’.Using — save as an argument to npm install means save this as a reference to your package.json file ie. The dependency will be added to your package.json file
Below is my package.json file which has ‘twit’ dependency.
Now we are done with installing the dependency.
Let’s set up our project to use Twitter API.Now we will make a js file inside our project
For example twitter-example.js
Inside our js file, we will write the code to connect to the Twitter API. Now how to run this file All you need to do is type ‘node twitter-example.js’. The moment you write this the code inside the js file will start to execute. You can also run your js file like this ‘npm start’ but for that, you will have to make changes to your package.json
Add a line
“start” : “node twitter-example.js” inside script like this
Alright, Now we are done setting up the dependencies. Now we will go to dev.twitter.com to generate
If you are wondering what are these keys and tokens for, they are secret keys and access tokens which give access to use the twitter API and let you play around with your account through your application/project. So now we will generate them first. Let’s see How it is done.
Step 1. Goto dev.twitter.com
Step2. Scroll down to the bottom and find Tools under this find ‘Manage my apps’
Step 3. On top right you will find ‘Create new app’
Step 4. Fill in your details Name, Description, website and agree to the Agreement
Step5 . Create your twitter Application
Step 6. Goto keys and access token, find your keys there
Step 7. Hit Create my access token and generate access token
If you are having trouble creating one please follow the link below
Generate Keys and tokens
Now you will have all the above-said keys and tokens with you. We will make use of those keys in out js file.
1.Search Post using the Twitter API.
Now that we have everything set up we will search for the particular post. Now, How do we do that? Continue reading