UNPKG

5.07 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[![wercker status](https://app.wercker.com/status/624dbe8ad011852d1e01d7dc03941fc5/s/master "wercker status")](https://app.wercker.com/project/bykey/624dbe8ad011852d1e01d7dc03941fc5) [![NPM](https://nodei.co/npm/twitter.png?mini=true)](https://nodei.co/npm/twitter/)
6
7```javascript
8var Twitter = require('twitter');
9
10var client = new Twitter({
11 consumer_key: '',
12 consumer_secret: '',
13 access_token_key: '',
14 access_token_secret: ''
15});
16
17var params = {screen_name: 'nodejs'};
18client.get('statuses/user_timeline', params, function(error, tweets, response){
19 if (!error) {
20 console.log(tweets);
21 }
22});
23```
24
25## Installation
26
27`npm install twitter`
28
29## Quick Start
30
31You 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.
32
33```javascript
34var Twitter = require('twitter');
35
36var client = new Twitter({
37 consumer_key: '',
38 consumer_secret: '',
39 access_token_key: '',
40 access_token_secret: ''
41});
42```
43
44Add your credentials accordingly. I would use environment variables to keep your private info safe. So something like:
45
46```javascript
47var client = new Twitter({
48 consumer_key: process.env.TWITTER_CONSUMER_KEY,
49 consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
50 access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
51 access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
52});
53```
54
55You now have the ability to make GET and POST requests against the API via the convenience methods.
56
57```javascript
58client.get(path, params, callback);
59client.post(path, params, callback);
60client.stream(path, params, callback);
61```
62
63## REST API
64
65You 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.
66
67Example, lets get a [list of favorites](https://dev.twitter.com/rest/reference/get/favorites/list):
68
69```javascript
70client.get('favorites/list', function(error, tweets, response){
71 if(error) throw error;
72 console.log(tweets); // The favorites.
73 console.log(response); // Raw response object.
74});
75```
76
77How about an example that passes parameters? Let's [tweet something](https://dev.twitter.com/rest/reference/post/statuses/update):
78
79```javascript
80client.post('statuses/update', {status: 'I Love Twitter'}, function(error, tweet, response){
81 if(error) throw error;
82 console.log(tweet); // Tweet body.
83 console.log(response); // Raw response object.
84});
85```
86
87## Streaming API
88
89Using 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:
90
91```javascript
92client.stream('statuses/filter', {track: 'javascript'}, function(stream) {
93 stream.on('data', function(tweet) {
94 console.log(tweet.text);
95 });
96
97 stream.on('error', function(error) {
98 throw error;
99 });
100});
101```
102
103## Examples
104
105* [Tweet](https://github.com/desmondmorris/node-twitter/tree/master/examples#tweet)
106* [Search](https://github.com/desmondmorris/node-twitter/tree/master/examples#search)
107* [Streams](https://github.com/desmondmorris/node-twitter/tree/master/examples#streams)
108* [Proxy](https://github.com/desmondmorris/node-twitter/tree/master/examples#proxy)
109* [Media](https://github.com/desmondmorris/node-twitter/tree/master/examples#media)
110
111## Contributors
112
113Originally authored by [@technoweenie](http://github.com/technoweenie)
114 and maintained by [@jdub](http://github.com/jdub)
115
116Currently maintained by [@desmondmorris](http://github.com/desmondmorris)
117
118[And we cannot forget the community](https://github.com/desmondmorris/node-twitter/graphs/contributors)
119
120
121## LICENSE
122
123node-twitter: Copyright (c) 2014 Desmond Morris
124
125Permission is hereby granted, free of charge, to any person obtaining
126a copy of this software and associated documentation files (the
127"Software"), to deal in the Software without restriction, including
128without limitation the rights to use, copy, modify, merge, publish,
129distribute, sublicense, and/or sell copies of the Software, and to
130permit persons to whom the Software is furnished to do so, subject to
131the following conditions:
132
133The above copyright notice and this permission notice shall be
134included in all copies or substantial portions of the Software.
135
136THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
137EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
138MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
139NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
140LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
141OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
142WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.