Thursday, September 9, 2010

X.CO Url Shortener

Go Daddy have just launched their x.co url shortener which is the shortest url shortener that I have been able to find. If you are doing shortening on your server (as opposed to doing it client side using JavaScript) then here is some C# code that will work on your web server to shorten a url using their API. You will need your API key which can be found here: Integrating x.co with applications you develop.

public class XcoApi
{
    private const string apiKey = "put your API key here";

    public static string ShortenUrl(string longUrl)
    {
        var shortUrl = string.Format(
            "http://x.co/Squeeze.svc/text/{0}?url={1}",
            apiKey, HttpUtility.UrlEncode(longUrl));

        WebClient wc = new WebClient();
        return wc.DownloadString(shortUrl);
    }
}

The main advantage of shortening a URL on the server side instead of the client side is that you can keep your API key hidden and therefore usable only by you.

No comments:

Post a Comment