How To Use The Twitter API To Find Events

Twitter is popular for many reasons, but one standout feature is its fantastic implementation of programmatic content: The Twitter API is easy to use, lightweight, and powerful, and the nature of the network perfectly lends itself to the kind of content generated by robots - short, tagged bursts of text, with or without image accompaniment.

Twitter's Search API has similar virtues: In addition to being user friendly, the content it allows you to tap into represents every old school research sociologist’s dream - the often tossed-off, often unfiltered daily (or hourly) updates of millions of technophiles the world over.

This tutorial guides you through the search and messaging APIs by teaching you to build a helpful bot capable of responding to your every request with the perfect social recommendation, relying on nothing more than Node.js and the collective hipness of the twitterati. For this particular instance, we’ll be building @SouthBotFunWest to respond to questions about the popular music, film, and interactive festival with a related party recommendation, but the techniques we’re using here could be applied just about everywhere you use Twitter to post a tweet or search a query.

South-by Season

It's that time of year, when a product manager's thoughts turn to daydrinking and the streets of Austin are turned into a bacchanalia of technology celebrating the almighty God of the Web — SXSW Interactive!

SXSW has been the launching ground for many web businesses that have gone on to greater success, but none of them can hold a candle to the festival's own true prodigal son, Twitter, a technology that didn't even launch at the event — it actually debuted 9 months earlier — but became so popular at the 2008 festival that its rise became indelibly associated with the conference.

Twitter is appealing as a data source because its short snippets provide useful insight into the vagaries of popular sentiment — part of the reason it's being archived for future generations by the Library of Congress. The social network is also a fantastic source for programmatic content: NASA has a twitter handle for posting high-definition images from the Hubble Space Telescope daily (@HubbleDaily), another plank in the argument that just about everyone is getting on board the twitterbot bandwagon.

With that in mind, let's use Node, everyone's favorite server-side JavaScript solution, and twit, an npm package that taps into both the streaming and RESTful Twitter APIs, to build a bot that can harness the collective hipness of the Twitterati to provide that elusive social secret — the perfect SXSW party recommendation.


If you haven't already, install Node and npm, Node's fantastic package manager. When you've confirmed that they're in your path (an easy way to check is which node and which npm), create a directory for your project and navigate into it: We'll accomplish this with mkdir SouthBotFunWest && cd SouthBotFunWest

Next start a new Node project with npm init. You'll be prompted to enter a name and some other particulars, but if you prefer you can just hit return and skip through everything.

Now down to business! Our first and only dependency for this project will be twit, a Node module for working with the Twitter API. Use the following command to install the module and save it to your package. JSON in one go:

npm install --save twit

(If you're getting some weird warnings installing the packages, add the line 'private': true to the exported object in your package.json to get rid of them.)

At this point, you'll need to create a twitter profile (in my case, @SouthBotFunWest) and link it to an associated developer account, which you can create at When you create your account make sure to change the app's permissions to "Read, Write, and Access Direct Messages":

After you've created and linked your accounts, write down your consumer key and secret somewhere safe, and click on the "Create my access token" button. Record these too.

In your root directory, create a config.js file to store these Twitter secret keys and access tokens. Add this to your .gitignore file (if you haven't made one yet, there's no time like the present!). Your config.js should look like this:

module.exports = {
    consumer_key: "",
    consumer_secret: "",
    access_token: "",
    access_token_secret: ""

(Except with actual keys and tokens!)

You might notice that this is different than how the twit Documentation describes inputting your consumer and access keys/tokens, that we're abstracting our Twitter credentials into a separate file. We've done this for the purpose of keeping it secure. By keeping sensitive credentials in a separate file that we can add to our .gitignore, we can keep from committing that information to a public Repo, which would be very bad.

To test our Twitter API access, let's slightly modify one of the twit boilerplate examples to post a classic "hello world!" message.

The new index.js should look like this.

var Twit = require('twit');
var twitInfo = require('./config.js');

var twitter = new Twit(twitInfo);'statuses/update', { status: 'hello world!' },  Function(err, data, response) {

We talked about abstracting our sensitive credentials into a separate config.js file. The key to making that work is the require() function's ability to import javascript files. Using require('./config.js') we can make accessible the config object exported inside of it and pass it along as an argument to the new Twit constructor function. We know what the Twit constructor function is expecting, which is why the config.js file has been written so that the attribute names are consistent with the twit API.

Start the bot with node index.js. You should see the data object associated with your tweet logged to the terminal and if you go to your Twitter profile, you should see those familiar two words!


Be sure to read the next Application Development article: How Was Built Using The Wordpress API and PhoneGap