URL shortening with bit.ly in Scala

Short URL’s are becoming the need of most of the application. We’ve used the bit.ly for the URL shortening in our application. Just a little bit work to do for achieving the URL shortening with bit.ly. Here are the steps to do that :

You should have the following credentials in order to call the bit.ly API.

i. apiKey : You’d get it on registering with bit.ly.
ii. login : Your bit.ly log-in name .

Now i think we’ve enough credentials. Lets prepare a Scala utility to achieve the short URL from bit.ly for the long one that we’ll provide to it.

The Scala utility object

object bitlyURLShortening extends App {

  // URL to be shortened
  val longUrl = "http://neelkanthsachdeva.wordpress.com/"

  // URL shortner utility method
  def returnShortUrlViabitly(longUrl: String) {

    //apiKey & login of a user
    val apiKey = "********************"
    val login = "********************"

    // API method call
    val bitlyURL = "https://api-ssl.bitly.com/v3/shorten"

    // Building the request (Choose your own way in order to make it)
    val promise = WS.url(bitlyURL).setQueryParameter("apiKey", apiKey).
      setQueryParameter("login", login).setQueryParameter("longUrl", longUrl).get

     *   extracting out the JSON from bit.ly containing
          the short url

    val shortUrlJson = promise.get.asJson.toString
    println("Here is the JSON containing short URL : " + shortUrlJson)



Output would look like this:

Here is the JSON containing short URL :


About Neelkanth Sachdeva

Project Manager @ eCIFM Solutions Inc. Previous : Senior Software Engineer @ Evernym Inc. Previous : Software Consultant @ Knoldus Software LLP Software Consultant @ Inphina Technologies
This entry was posted in Cloud, Scala, Web and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s