UNPKG

6.12 kBMarkdownView Raw
1# Twitter for Node.js
2
3An 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
9var Twitter = require('twitter');
10
11var client = new Twitter({
12 consumer_key: '',
13 consumer_secret: '',
14 access_token_key: '',
15 access_token_secret: ''
16});
17
18var params = {screen_name: 'nodejs'};
19client.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
32You 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
35var Twitter = require('twitter');
36```
37
38## For User based authentication:
39
40```javascript
41var client = new Twitter({
42 consumer_key: '',
43 consumer_secret: '',
44 access_token_key: '',
45 access_token_secret: ''
46});
47```
48
49Add your credentials accordingly. I would use environment variables to keep your private info safe. So something like:
50
51```javascript
52var 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
61You 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
64var client = new Twitter({
65 consumer_key: '',
66 consumer_secret: '',
67 bearer_token: ''
68});
69```
70
71Add your credentials accordingly. I would use environment variables to keep your private info safe. So something like:
72
73```javascript
74var 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
81NB - 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
85You now have the ability to make GET and POST requests against the API via the convenience methods.
86
87```javascript
88client.get(path, params, callback);
89client.post(path, params, callback);
90client.stream(path, params, callback);
91```
92
93## REST API
94
95You 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
97Example, lets get a [list of favorites](https://dev.twitter.com/rest/reference/get/favorites/list):
98
99```javascript
100client.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
107How about an example that passes parameters? Let's [tweet something](https://dev.twitter.com/rest/reference/post/statuses/update):
108
109```javascript
110client.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
119The REST API convenience methods will also return Promises if:
120
1211. A callback is omitted
1222. Promise's are available.
123
124If those two conditions are met, the above example becomes:
125
126```javascript
127client.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
136Note, the raw `response` object returned by the Request module is not passed through
137the fulfilled promise. If you require this, please use the callback pattern.
138
139## Streaming API
140
141Using 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
144var stream = client.stream('statuses/filter', {track: 'javascript'});
145stream.on('data', function(event) {
146 console.log(event && event.text);
147});
148
149stream.on('error', function(error) {
150 throw error;
151});
152
153// You can also get the stream in a callback if you prefer.
154client.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')
169const 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
187Originally authored by [@technoweenie](http://github.com/technoweenie)
188 and maintained by [@jdub](http://github.com/jdub)
189
190Currently maintained by [@desmondmorris](http://github.com/desmondmorris)
191
192[And we cannot forget the community](https://github.com/desmondmorris/node-twitter/graphs/contributors)