1 | # Twitter for Node.js
|
2 |
|
3 | An asynchronous client library for the Twitter [REST](https://dev.twitter.com/rest/public) and [Streaming](https://dev.twitter.com/streaming/overview) API's.
|
4 |
|
5 | [![Build Status](https://travis-ci.org/desmondmorris/node-twitter.svg?branch=master)](https://travis-ci.org/desmondmorris/node-twitter)
|
6 | [![NPM](https://nodei.co/npm/twitter.png?mini=true)](https://nodei.co/npm/twitter/)
|
7 |
|
8 | ```javascript
|
9 | var Twitter = require('twitter');
|
10 |
|
11 | var client = new Twitter({
|
12 | consumer_key: '',
|
13 | consumer_secret: '',
|
14 | access_token_key: '',
|
15 | access_token_secret: ''
|
16 | });
|
17 |
|
18 | var params = {screen_name: 'nodejs'};
|
19 | client.get('statuses/user_timeline', params, function(error, tweets, response) {
|
20 | if (!error) {
|
21 | console.log(tweets);
|
22 | }
|
23 | });
|
24 | ```
|
25 |
|
26 | ## Installation
|
27 |
|
28 | `npm install twitter`
|
29 |
|
30 | ## Quick Start
|
31 |
|
32 | You will need valid Twitter developer credentials in the form of a set of consumer and access tokens/keys. You can get these [here](https://apps.twitter.com/). Do not forgot to adjust your permissions - most POST request require write permissions.
|
33 |
|
34 | ```javascript
|
35 | var Twitter = require('twitter');
|
36 | ```
|
37 |
|
38 | ## For User based authentication:
|
39 |
|
40 | ```javascript
|
41 | var client = new Twitter({
|
42 | consumer_key: '',
|
43 | consumer_secret: '',
|
44 | access_token_key: '',
|
45 | access_token_secret: ''
|
46 | });
|
47 | ```
|
48 |
|
49 | Add your credentials accordingly. I would use environment variables to keep your private info safe. So something like:
|
50 |
|
51 | ```javascript
|
52 | var client = new Twitter({
|
53 | consumer_key: process.env.TWITTER_CONSUMER_KEY,
|
54 | consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
|
55 | access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
|
56 | access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
|
57 | });
|
58 | ```
|
59 | ## For Application Only based authentication:
|
60 |
|
61 | You will need to fetch a bearer token from Twitter as documented [Here](https://dev.twitter.com/oauth/application-only), once you have it you can use it as follows.
|
62 |
|
63 | ```javascript
|
64 | var client = new Twitter({
|
65 | consumer_key: '',
|
66 | consumer_secret: '',
|
67 | bearer_token: ''
|
68 | });
|
69 | ```
|
70 |
|
71 | Add your credentials accordingly. I would use environment variables to keep your private info safe. So something like:
|
72 |
|
73 | ```javascript
|
74 | var client = new Twitter({
|
75 | consumer_key: process.env.TWITTER_CONSUMER_KEY,
|
76 | consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
|
77 | bearer_token: process.env.TWITTER_BEARER_TOKEN
|
78 | });
|
79 | ```
|
80 |
|
81 | NB - You will not have access to all endpoints whilst using Application Only authentication, but you will have access to higher API limits.
|
82 |
|
83 | ## Requests
|
84 |
|
85 | You now have the ability to make GET and POST requests against the API via the convenience methods.
|
86 |
|
87 | ```javascript
|
88 | client.get(path, params, callback);
|
89 | client.post(path, params, callback);
|
90 | client.stream(path, params, callback);
|
91 | ```
|
92 |
|
93 | ## REST API
|
94 |
|
95 | You simply need to pass the endpoint and parameters to one of convenience methods. Take a look at the [documentation site](https://dev.twitter.com/rest/public) to reference available endpoints.
|
96 |
|
97 | Example, lets get a [list of favorites](https://dev.twitter.com/rest/reference/get/favorites/list):
|
98 |
|
99 | ```javascript
|
100 | client.get('favorites/list', function(error, tweets, response) {
|
101 | if(error) throw error;
|
102 | console.log(tweets); // The favorites.
|
103 | console.log(response); // Raw response object.
|
104 | });
|
105 | ```
|
106 |
|
107 | How about an example that passes parameters? Let's [tweet something](https://dev.twitter.com/rest/reference/post/statuses/update):
|
108 |
|
109 | ```javascript
|
110 | client.post('statuses/update', {status: 'I Love Twitter'}, function(error, tweet, response) {
|
111 | if(error) throw error;
|
112 | console.log(tweet); // Tweet body.
|
113 | console.log(response); // Raw response object.
|
114 | });
|
115 | ```
|
116 |
|
117 | ### Promises
|
118 |
|
119 | The REST API convenience methods will also return Promises if:
|
120 |
|
121 | 1. A callback is omitted
|
122 | 2. Promise's are available.
|
123 |
|
124 | If those two conditions are met, the above example becomes:
|
125 |
|
126 | ```javascript
|
127 | client.post('statuses/update', {status: 'I Love Twitter'})
|
128 | .then(function (tweet) {
|
129 | console.log(tweet);
|
130 | })
|
131 | .catch(function (error) {
|
132 | throw error;
|
133 | })
|
134 | ```
|
135 |
|
136 | Note, the raw `response` object returned by the Request module is not passed through
|
137 | the fulfilled promise. If you require this, please use the callback pattern.
|
138 |
|
139 | ## Streaming API
|
140 |
|
141 | Using the `stream` convenience method, you to open and manipulate data via a stream piped directly from one of the streaming API's. Let's see who is talking about javascript:
|
142 |
|
143 | ```javascript
|
144 | var stream = client.stream('statuses/filter', {track: 'javascript'});
|
145 | stream.on('data', function(event) {
|
146 | console.log(event && event.text);
|
147 | });
|
148 |
|
149 | stream.on('error', function(error) {
|
150 | throw error;
|
151 | });
|
152 |
|
153 | // You can also get the stream in a callback if you prefer.
|
154 | client.stream('statuses/filter', {track: 'javascript'}, function(stream) {
|
155 | stream.on('data', function(event) {
|
156 | console.log(event && event.text);
|
157 | });
|
158 |
|
159 | stream.on('error', function(error) {
|
160 | throw error;
|
161 | });
|
162 | });
|
163 | ```
|
164 |
|
165 | **Note** twitter stream several types of events, see [the docs](https://dev.twitter.com/streaming/overview/messages-types) for more info. There is no canonical way of detecting tweets versus other messages, but some users have had success with the following strategy.
|
166 |
|
167 | ```javascript
|
168 | _ = require('lodash')
|
169 | const isTweet = _.conforms({
|
170 | contributors: _.isObject,
|
171 | id_str: _.isString,
|
172 | text: _.isString,
|
173 | })
|
174 | ```
|
175 |
|
176 | ## Examples
|
177 |
|
178 | * [Tweet](https://github.com/desmondmorris/node-twitter/tree/master/examples#tweet)
|
179 | * [Search](https://github.com/desmondmorris/node-twitter/tree/master/examples#search)
|
180 | * [Streams](https://github.com/desmondmorris/node-twitter/tree/master/examples#streams)
|
181 | * [Proxy](https://github.com/desmondmorris/node-twitter/tree/master/examples#proxy)
|
182 | * [Media](https://github.com/desmondmorris/node-twitter/tree/master/examples#media)
|
183 | * [Chunked Media](https://github.com/desmondmorris/node-twitter/tree/master/examples#chunked-media)
|
184 |
|
185 | ## Contributors
|
186 |
|
187 | Originally authored by [@technoweenie](http://github.com/technoweenie)
|
188 | and maintained by [@jdub](http://github.com/jdub)
|
189 |
|
190 | Currently maintained by [@desmondmorris](http://github.com/desmondmorris)
|
191 |
|
192 | [And we cannot forget the community](https://github.com/desmondmorris/node-twitter/graphs/contributors)
|