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 | [![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
|
8 | var Twitter = require('twitter');
|
9 |
|
10 | var client = new Twitter({
|
11 | consumer_key: '',
|
12 | consumer_secret: '',
|
13 | access_token_key: '',
|
14 | access_token_secret: ''
|
15 | });
|
16 |
|
17 | var params = {screen_name: 'nodejs'};
|
18 | client.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 |
|
31 | 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.
|
32 |
|
33 | ```javascript
|
34 | var Twitter = require('twitter');
|
35 |
|
36 | var client = new Twitter({
|
37 | consumer_key: '',
|
38 | consumer_secret: '',
|
39 | access_token_key: '',
|
40 | access_token_secret: ''
|
41 | });
|
42 | ```
|
43 |
|
44 | Add your credentials accordingly. I would use environment variables to keep your private info safe. So something like:
|
45 |
|
46 | ```javascript
|
47 | var 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 |
|
55 | You now have the ability to make GET and POST requests against the API via the convenience methods.
|
56 |
|
57 | ```javascript
|
58 | client.get(path, params, callback);
|
59 | client.post(path, params, callback);
|
60 | client.stream(path, params, callback);
|
61 | ```
|
62 |
|
63 | ## REST API
|
64 |
|
65 | 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.
|
66 |
|
67 | Example, lets get a [list of favorites](https://dev.twitter.com/rest/reference/get/favorites/list):
|
68 |
|
69 | ```javascript
|
70 | client.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 |
|
77 | How about an example that passes parameters? Let's [tweet something](https://dev.twitter.com/rest/reference/post/statuses/update):
|
78 |
|
79 | ```javascript
|
80 | client.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 |
|
89 | 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:
|
90 |
|
91 | ```javascript
|
92 | client.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 |
|
113 | Originally authored by [@technoweenie](http://github.com/technoweenie)
|
114 | and maintained by [@jdub](http://github.com/jdub)
|
115 |
|
116 | Currently 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 |
|
123 | node-twitter: Copyright (c) 2014 Desmond Morris
|
124 |
|
125 | Permission is hereby granted, free of charge, to any person obtaining
|
126 | a copy of this software and associated documentation files (the
|
127 | "Software"), to deal in the Software without restriction, including
|
128 | without limitation the rights to use, copy, modify, merge, publish,
|
129 | distribute, sublicense, and/or sell copies of the Software, and to
|
130 | permit persons to whom the Software is furnished to do so, subject to
|
131 | the following conditions:
|
132 |
|
133 | The above copyright notice and this permission notice shall be
|
134 | included in all copies or substantial portions of the Software.
|
135 |
|
136 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
137 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
138 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
139 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
140 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
141 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
142 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|