Skip to content


Subversion checkout URL

You can clone with
Download ZIP
A simple library to shorten and expand URL's.
Latest commit 0c5f0a1 @joshnesbitt Bump.


Google Url Shortener.

  • Overview
  • Installation
  • Usage
  • CLI
  • Specs
  • Bugs


Google URL Shortener is a library to interact with the URL shortener API. It provides:

  • A CLI to easily shorten/expand URL’s
  • A clean API to handle shortening/expanding URL’s in an application
  • An interface to view analytical data for any short URL


The project is hosted on Getting it is simple:

gem install google_url_shortener



You need to provide an API key (get one here) to use the URL shortener service. To do this set the api_key:

Google::UrlShortener::Base.api_key = "KEY"

You can also turn on verbose logging to inspect requests made by the library. Behind the scenes the library uses RestClient to call the API, therefore whatever you set here will be used as the RestClient log (RestClient.log).

Google::UrlShortener::Base.log = $stdout

Shorten/Expand a URL

To shorten a URL:

url = => "")
url.shorten! # =>

Or the shorthand form:

Google::UrlShortener.shorten!("") # =>

To expand a URL:

url = => "")
url.expand! # =>

Or the shorthand form:

Google::UrlShortener.expand!("") # =>

Getting more data from an expanded URL

More data is available from a URL once it’s been expanded:

url = => "")
url.expand! # =>
url.created_at # => 2011-01-11
url.created_at.year # => 2011 # => Google::UrlShortener::Analytics # => Google::UrlShortener::AnalyticsGroup # => { "Chrome" => 1 } # => { "GB" => 1 } # => { "Macintosh" => 1 } # => { "Unknown/empty" => 1 } # => 23 # => 3

Available scopes are:




Basic usage

There is also a CLI wrapper for Google URL Shortener. Firstly, you need to tell the CLI your API key (get one here):

googl install AIzaSyByl4x5CMcnm2rNWafmaUz5sljmzMWIgZ0

This saves your API key in ~/.googl.

To shorten a URL:

googl shorten
googl s

To expand a URL:

googl expand
googl e

You can also get analytical data from a short URL using the -a or --analytics flag:

googl expand -a


You can use the CLI alongside something like pbcopy to make shortening URL’s crazy fast:

googl shorten | pbcopy

Also use a bash function to simplify it further:

shorten (){
	googl shorten $1 | pbcopy
	echo "$1 shortened and copied to clipboard"

Which can be used as:



Run the specs with the following command:

bundle exec rspec --require ./spec/spec_helper.rb --format nested --color spec/lib/*.rb spec/lib/**/*.rb


If you have any problems with Google Url Shortener, please file an issue.

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don’t break it in a
    future version unintentionally.
  • Commit, do not mess with rakefile, version, or history.
    (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.


Copyright © 2010 Josh Nesbitt <>. See LICENSE for details.

Something went wrong with that request. Please try again.