Twitter streaming API for NodeJs


I'm trying to get tweets from an account either mine or someone else's.

Create an account on twitter, and create an application, I have the token and the keys, I would like to know if anyone has used this API, because I want to get the last tweet published.

Likewise, if someone knows another way I would appreciate it, I found this, but I would like to know if it is necessary to use the require, or use express or something because it must be with nodejs.


asked by Pedro Miguel Pimienta Morales 13.02.2016 в 18:01

1 answer


To use the twitter package, you'll need to:

Add the dependency in the file package.json , the easiest way is: (inside the project folder)

npm --save install twitter

Then you must import the module to your application and configure it with the keys you have. By the way, that's what require is for, to import another module to the current module.

var Twitter = require('twitter');

var twclient = new Twitter({
  consumer_key: '', // debes poner los datos correctos en estos campos
  consumer_secret: '',
  access_token_key: '',
  access_token_secret: ''

All this as you know is in the documentation, now:

To get the latest Twit

You must obtain the route statuses/user_timeline . Look at the documentation as there are other options for the consultation, here prepare a very simple:

// preparamos los datos del usuario a consultar
var usuario = {
       // en screen_name, pones el nombre de usuario sin la arroba.
       screen_name: 'pedromiguelpimienta', 
       // la cantidad de mensajes a obtener
       count: 1,
       // solo mensajes propios (no re-tweets) 
       include_rts: false

twclient.get('statuses/user_timeline', usuario, function(error, tweets, response){
  if(error) throw error; // se produjo un error, manejar aquí

  if (tweets.length) {
     console.log(tweets[0].text);  // el texto del ultimo twit, si hay alguno

By the way this is a very popular library, only today received more than 1000 downloads.


In this repository, I leave you a functional example with the instructions so you can try it.


answered by 14.02.2016 / 00:06