is a popular URL shortening service that is used to shorten urls like to a url like This means the URL takes up less space and is friendlier for platforms like Twitter, which have post length restrictions. Account Setup

The first thing you will neeed to do is setup up an account with Simply visit to sign up for an account.

Once signed up, you will need to verify your email address. Click the drop down in the upper right corner and choose settings, alternatively you can simply click here to go directly to the page. On this screen you will see your email address, along with a prompt to verify it. Click the link and then go to your email and click the link sends you to verify your email address.

Verify Email Screenshot

Finally, you need to visit this page to get your login name and API key. Note both of these values as you will need them for your rails application.

Rails Application Setup

The first thing we need to do is add the bitly to our gemfile. Open up your gemfile and add in the code listed below.


gem 'bitly', '~> 0.9.0'

In this example, we will create a controller called Urls, which will allow us to create a bitly link based on parameters that are passed in. Run the command below to create the Urls controller now.

Terminal Commands:

rails g controller urls new create

The next thing we need to do is add a route and set up a homepage in our routes file. Open up your routes file and modify it so that it looks like the code listed below. Be sure not to overwrite your applications name on the first line.


BitlyExample::Application.routes.draw do
  resources :urls, only [:new, :create]
  root to: 'urls#new'

Next we need to create an initializer. Create an file called bitly.rb in your initializers folder and add in the code listed below. Make sure you replace USERNAME with your username and API_KEY with your API key you noted earlier.


Bitly.configure do |config|
  config.api_version = 3
  config.login = "USERNAME"
  config.api_key = "API_KEY"

The next thing we need to do is add some code to our Urls controller. Open up your Urls controller and modify it so that it looks like code listed below.


class UrlsController < ApplicationController
  def new

  def create
    if !params[:url].blank?
      client = Bitly.client
      @url = client.shorten(params[:url])

The magic happens in the create method. The create method takes a parameter called url and shortens it using the bitly client gem.

Great! Now we need to add in some code for our Urls new view. Open up the view now and add in the code listed below.


<h1>Create a new URL:</h1>

<%= form_for "", url: urls_path, method: :post do |f| %>
 <%= f.label :url %>
 <%= f.text_field :url %>
 <%= f.submit "Create Url" %>
<% end %>

Finally, add this code to your Urls create view.


<h1>Your url for <%= @url.long_url %> is: </h1>
<%= link_to @url.short_url, @url.short_url, target: "_blank" %><br /><br />
<%= link_to "Create another Url", root_path %>

The @url.short_url attribute returns the shortened version of the URL, while @url.long_url returns the original url. If you start your rails server and navigate to http://localhost:3000 you will see that you can generate urls on the fly right from your own website. That's it! Thanks for reading!