Skip to content
A Ruby interface to the Twitter API.
Branch: master
Clone or download
Pull request Compare This branch is 8 commits behind jackieiscool:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

The Twitter Ruby Gem

Gem Version Build Status Dependency Status Code Climate Coverage Status Click here to make a donation

A Ruby interface to the Twitter API.


gem install twitter

To ensure the code you're installing hasn't been tampered with, it's recommended that you verify the signature. To do this, you need to add my public key as a trusted certificate (you only need to do this once):

gem cert --add <(curl -Ls

Then, install the gem with the high security trust policy:

gem install twitter -P HighSecurity

Quick Start Guide

So you want to get up and tweeting as fast as possible?

First, register your application with Twitter.

Then, copy and paste in your OAuth data.

Twitter.configure do |config|
  config.consumer_key = YOUR_CONSUMER_KEY
  config.consumer_secret = YOUR_CONSUMER_SECRET
  config.oauth_token = YOUR_OAUTH_TOKEN
  config.oauth_token_secret = YOUR_OAUTH_TOKEN_SECRET

That's it! You're ready to Tweet:

Twitter.update("I'm tweeting with @gem!")

For more examples of how to use the gem, read the documentation or see Usage Examples below.


Looking for the Twitter command-line interface? It was removed from this gem in version 0.5.0 and now exists as a separate project.



You should follow @gem on Twitter for announcements and updates about this library.

Mailing List

Please direct questions about this library to the mailing list.

Apps Wiki

Does your project or organization use this gem? Add it to the apps wiki!

What's New in Version 5?


The Twitter::Cursor class has been completely redesigned with a focus on simplicity and performance.

Notes Version 4 Version 5
Are you at the start of the cursor?
Return your most recent follower.
Return an array of all your friends.
Collect your 20 most recent friends.
Collect your 20 most recent friends (twice).
2.times.collect do
2.times.collect do

In the examples above, n varies with the number of people the authenticated user follows on Twitter. This resource returns up to 20 friends per HTTP GET, so if the authenticated user follows 200 people, calling Twitter.friends.take(20) would make 11 HTTP requests in version 4. In version 5, it makes just 1 HTTP request. Keep in mind, eliminating a single HTTP request to the Twitter API will reduce the latency of your application by about 500 ms.

The last example might seem contrived ("Why would I call Twitter.friends.take(20) twice?") but it applies to any Enumerable method you might call on a cursor, including: #all?, #collect, #count, #each, #inject, #max, #min, #reject, #reverse_each, #select, #sort, #sort_by, and #to_a. In version 4, each time you called one of those methods, it would perform n+1 HTTP requests. In version 5, it only performs those HTTP requests the first time any one of those methods is called. Each subsequent call fetches data from a cache.

The performance improvements are actually even better than the table above indicates. In version 5, calling Twitter::Cursor#each (or any Enumerable method) starts yielding results immediately and continues yielding as each response comes back from the server. In version 4, #each made a series of requests and waited for the last one to complete before yielding any data.

Here is a list of the interface changes to Twitter::Cursor:

  • #all has been replaced by #to_a.
  • #last has been replaced by #last?.
  • #first has been replaced by #first?.
  • #first now returns the first element in the collection, as prescribed by Enumerable.
  • #collection and its aliases have been removed.

Search Results

The Twitter::SearchResults class has also been redesigned to have an Enumerable interface. The #statuses method and its aliases (#collection and #results) have been replaced by #to_a. Additionally, this class no longer inherits from Twitter::Base. As a result, the #[] method has been removed.

Trend Results

The Twitter.trends method now returns an Enumerable Twitter::TrendResults object instead of an array. This object provides methods to determinte the recency of the trend (#as_of), when the trend started (#created_at), and the location of the trend (#location). This data was previously unavailable.

Geo Results

Similarly, the Twitter.reverse_geocode, Twitter.geo_search, and Twitter.similar_places methods now return an Enumerable Twitter::GeoResults object instead of an array. This object provides access to the token to create a new place (#token), which was previously unavailable.


The Twitter::Tweet object has been cleaned up. The following methods have been removed:

  • #from_user
  • #from_user_id
  • #from_user_name
  • #to_user
  • #to_user_id
  • #to_user_name
  • #profile_image_url
  • #profile_image_url_https

These attributes can be accessed on the Twitter::User object, returned through the #user method.


The Twitter::User object has also been cleaned up. The following aliases have been removed:

  • #favorite_count (use #favorites_count)
  • #favoriters_count (use #favorites_count)
  • #favourite_count (use #favourites_count)
  • #favouriters_count (use #favourites_count)
  • #follower_count (use #followers_count)
  • #friend_count (use #friends_count)
  • #status_count (use #statuses_count)
  • #tweet_count (use #tweets_count)
  • #update_count (use #tweets_count)
  • #updates_count (use #tweets_count)
  • #translator (use #translator?)

Null Objects

In version 4, methods you would expect to return a Twitter object would return nil if that object was missing. This may have resulted in a NoMethodError. To prevent such errors, you may have introduced checks for the truthiness of the response, for example:

status = Twitter.status(55709764298092545)
  # Do something with the Twitter::Place object
elsif status.geo
  # Do something with the Twitter::Geo object

In version 5, all such methods will return a Twitter::NullObject instead of nil. This should prevent NoMethodError but may result in unexpected behavior if you have truthiness checks in place, since everything is truthy in Ruby except false and nil. For these cases, there are now predicate methods:

status = Twitter.status(55709764298092545)
  # Do something with the Twitter::Place object
elsif status.geo?
  # Do something with the Twitter::Geo object

URI Methods

The Twitter::List, Twitter::Tweet, and Twitter::User objects all have a #uri method, which returnis an HTTPS URI to This clobbers the Twitter::List#uri method, which previously returned the list URI's path (not a URI).

These methods are aliased to #url for users who prefer that nomenclature. Twitter::User previously had a #url method, which returned the user's website. This URI is now available via the #website method.

All #uri methods now return URI objects instead of strings. To convert a URI object to a string, call #to_s on it.


Twitter API v1.1 requires you to authenticate via OAuth, so you'll need to register your application with Twitter. Once you've registered an application, make sure to set the correct access level, otherwise you may see the error:

Read-only application cannot POST

Your new application will be assigned a consumer key/secret pair and you will be assigned an OAuth access token/secret pair for that application. You'll need to configure these values before you make a request or else you'll get the error:

Bad Authentication data

Applications that make requests on behalf of a single Twitter user can pass global configuration options as a block to the Twitter.configure method.

Twitter.configure do |config|
  config.consumer_key = YOUR_CONSUMER_KEY
  config.consumer_secret = YOUR_CONSUMER_SECRET
  config.oauth_token = YOUR_OAUTH_TOKEN
  config.oauth_token_secret = YOUR_OAUTH_TOKEN_SECRET

Alternately, you can set the following environment variables:


After configuration, requests can be made like so:

Twitter.update("I'm tweeting with @gem!")

Thread Safety

Applications that make requests on behalf of multiple Twitter users should avoid using global configuration. In this case, you may still specify the consumer_key and consumer_secret globally. (In a Rails application, this could go in config/initializers/twitter.rb.)

Twitter.configure do |config|
  config.consumer_key = YOUR_CONSUMER_KEY
  config.consumer_secret = YOUR_CONSUMER_SECRET

Then, for each user's access token/secret pair, instantiate a Twitter::Client:

erik =
  :oauth_token => "Erik's access token",
  :oauth_token_secret => "Erik's access secret"

john =
  :oauth_token => "John's access token",
  :oauth_token_secret => "John's access secret"

You can now make threadsafe requests as the authenticated user:{erik.update("Tweeting as Erik!")}{john.update("Tweeting as John!")}

Or, if you prefer, you can specify all configuration options when instantiating a Twitter::Client:

client =
  :consumer_key => "an application's consumer key",
  :consumer_secret => "an application's consumer secret",
  :oauth_token => "a user's access token",
  :oauth_token_secret => "a user's access secret"

This may be useful if you're using multiple consumer key/secret pairs.


The Faraday middleware stack is fully configurable and is exposed as a Faraday::Builder object. You can modify the default middleware in-place:

Twitter.middleware.insert_after Twitter::Response::RaiseError, CustomMiddleware

A custom adapter may be set as part of a custom middleware stack:

Twitter.middleware =
  & do |builder|
    # Specify a middleware stack here
    builder.adapter :some_other_adapter

Usage Examples

All examples require an authenticated Twitter client. See the section on configuration above.

Tweet (as the authenticated user)

Twitter.update("I'm tweeting with @gem!")

Follow a user (by screen name or user ID)


Fetch a user (by screen name or user ID)


Fetch a cursored list of followers with profile details (by screen name or user ID, or by implict authenticated user)


Fetch a cursored list of friends with profile details (by screen name or user ID, or by implict authenticated user)


Fetch a collection of user_ids that the currently authenticated user does not want to receive retweets from


Fetch the timeline of Tweets by a user


Fetch the timeline of Tweets from the authenticated user's home page


Fetch the timeline of Tweets mentioning the authenticated user


Fetch a particular Tweet by ID


Collect the 3 most recent marriage proposals to @justinbieber"to:justinbieber marry me", :count => 3, :result_type => "recent").collect do |tweet|
  "#{tweet.user.screen_name}: #{tweet.text}"

Find a Japanese-language Tweet tagged #ruby (excluding retweets)"#ruby -rt", :lang => "ja").first.text

For more usage examples, please see the full documentation.


To access the Twitter Streaming API, we recommend TweetStream.


You can improve performance by loading a faster JSON parsing library. By default, JSON will be parsed with okjson. For faster JSON parsing, we recommend Oj.


Here are some fun facts about this library:

  • It is implemented in just 2,500 lines of Ruby code
  • With over 6,250 lines of specs, the spec-to-code ratio is about 2.5:1
  • The spec suite contains over 900 examples and runs in about 5 seconds
  • It has 100% C0 code coverage (the tests execute every line of source code at least once)
  • It is comprehensive: you can request all documented Twitter REST API resources (over 100 resources)
  • This gem works on every major Ruby implementation, including JRuby and Rubinius
  • The first version was released on November 26, 2006
  • This gem has just two runtime dependencies: faraday, and simple_oauth
  • Previous versions of this gem have been downloaded over a million times

Supported Ruby Versions

This library aims to support and is tested against the following Ruby implementations:

  • Ruby 1.9.2
  • Ruby 1.9.3
  • Ruby 2.0.0

If something doesn't work on one of these interpreters, it's a bug.

This library may inadvertently work (or seem to work) on other Ruby implementations, however support will only be provided for the versions listed above.

If you would like this library to support another Ruby version, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped.


This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, that version should be immediately yanked and/or a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions. As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision. For example:

spec.add_dependency 'twitter', '~> 4.0'


Copyright (c) 2006-2013 Erik Michaels-Ober, John Nunemaker, Wynn Netherland, Steve Richert, Steve Agalloco. See LICENSE for details.

You can’t perform that action at this time.