A A
RSS

Shortening URLs with bit.ly’s API in .NET

Thu, May 7, 2009

Programming

I previously wrote about shortening URLs with TinyURL’s API but my new favorite URL shortener is http://bit.ly and it also has a great API that you can use from within your .net code.  I just recently used in my twitter contest website – tweetastica.

The code is also very simple.  I didn’t add all the extra options that the API makes available and just wrote enough for me to shorten a URL.  Here it is:

public static class BitlyApi
{
    private const string apiKey = "[add api key here]";
    private const string login = "[add login name here]";

    public static BitlyResults ShortenUrl(string longUrl)
    {
        var url =
            string.Format("http://api.bit.ly/shorten?format=xml&version=2.0.1&longUrl={0}&login={1}&apiKey={2}",
                          HttpUtility.UrlEncode(longUrl), login, apiKey);
        var resultXml = XDocument.Load(url);
        var x = (from result in resultXml.Descendants("nodeKeyVal")
                 select new BitlyResults
                            {
                                UserHash = result.Element("userHash").Value,
                                ShortUrl = result.Element("shortUrl").Value
                            }
                );
        return x.Single();
    }
}

public class BitlyResults
{
    public string UserHash { get; set; }

    public string ShortUrl { get; set; }
}

Using this code is very straight forward.

var shortUrl = BitlyApi.ShortenUrl("http://www.verylongUrl.com").ShortUrl;

bit.ly has tons of features and a great API.  You can even see stats of any bit.ly generate url using /info.  for example: http://bit.ly/info/S0vRy shows you stats for http://bit.ly/S0vRy

Another feature I really like is their bookmarklet which you can keep on your browser’s bookmark bar and click it to shrink the site you are at…  It even pops this side bar with a bunch of useful info.

image

The API is pretty well documented at http://code.google.com/p/bitly-api/wiki/ApiDocumentation

Don’t forget to follow me on twitter.

Tags: , , , , ,

  • ralph
    Hi Emad

    I just tried your code, but it is throwing me an error at this part:
    var x = (from result in resultXml.Descendants("nodeKeyVal")
    ...

    It tells me : error CS1026: ) expected

    Do you have an idea why this may happen?

    Thanks
    ralph
  • Is that a compile error or runtime error? Did you copy & paste the code
    correctly? Sometimes the browser messes up the code formatting.

    Other than that you should add a breakpoint and examine the value of
    resultXml and see if it shows any error messages.

    Good luck.
  • raichelb
    Hello,
    How do I define\declare xDocument variable correctly?
    Thanks
  • Great post, thanks for sharing this class. I had to make one Tweak to use this in a non-web app in VS 2010 Beta 2. HttpUtlity is in System.Web and it looks like there is no way to reference System.Web in VS 2010 Beta 2 from a console application.

    I changed the call to HttpUtility to use System.Uri.EscapeDataString and it worked like a charm.

    Good stuff!
  • Guys,
    Dont forget to import the necessary assemblies.

    using System.Linq;
    using System.Web;
    using System.Xml.Linq;
  • I wrote an article on my blog on how to use the bit.ly API with html and jQuery: http://www.codecapers.com/post/Building-a-Link-...
blog comments powered by Disqus
Advertise Here

What I'm Doing...

Yonkly Open Source

Sign up for my newsletter

powered by MailChimp!

Cyber Identity