Shorten your URL with in Asp.Net Using C#

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 =
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; }

The API is pretty well documented at


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s