One API to rule them all

url shortener

What's this, another URL shortener? This one comes with a twist — it's an API to an API.

See, you can access numerous other URL shorteners using this one API. Let's see what we can do with this one.

tiny-url.info logo

It turns out you can access many of the popular URL shorteners using a single API. It requires an API key however — I've obtained one, but in the code below you'll need to provide your own.

For you developers out there, the Tiny-URL homepage also conveniently links to the API documents for each URL shortener. Feel free to go crazy and write code for all of them!

Let's start at the top of a standard module and work our way down, shall we?

Enums and Constants

Public Enum providerString
  bit_ly
  goo_gl
  is_gd
  j_mp
  tinyurl_com
End Enum

Const BASE_URL As String = "http://tiny-url.info/api/v1/create"
Const API_KEY As String = "your API key here"

At the top of a standard module I define my Enum as well as the constants used throughout the code. The base URL for the API is defined on the API page.

I didn't use all of the possible URL shortener providers because some of them start with numbers (you can't start an Enum with a number) and I was lazy and didn't feel like making the necessary fixes. So I chose the more popular ones, or the ones I could at least recognize or have worked with before (see here and here).

Ancillary Functions

Let's move on to the helper functions. There are three helper functions used here.

Convert Enum to String

We'll use this to convert the numeric constant provided by the Enum into a String used by the API.

Function GetProviderString(provider As providerString) As String
  Select Case provider
    Case 0
      GetProviderString = "bit_ly"
    Case 1
      GetProviderString = "goo_gl"
    Case 2
      GetProviderString = "is_gd"
    Case 3
      GetProviderString = "j_mp"
    Case 4
      GetProviderString = "tinyurl_com"
  End Select
End Function

Check if URL is valid

We'll validate the URL using this function. It simply makes a HTTP GET request to the URL (using the fastest method we know) and checks if the response is 200 ("OK"). See HTTP Status Code Definitions for a full list of status codes.

Two Main Functions

Tiny-URL provides two major functions when shortening a URL. You can choose which provider you want, or you can let Tiny-URL choose one at random. Here we'll present functions that encapsulate both methods.

Choose Your Provider

This function lets you choose your provider (from the filtered list I provided). I chose bit.ly as a default, mostly because it's the one used by Twitter.

MSXML6.DLL must be present in the local system32 folder. The URL is validated and then passed to the API. I didn't bother with any caching this time (there's that "lazy" thing again).

Function GetShortURL(longurl As String, _
    Optional provider As providerString = bit_ly) As String
Dim URL As String
Dim result As String
Dim xml As Object  ' MSXML2.XMLHTTP60

  ' validate the URL first
  If IsValidURL(longurl) Then
    ' build URL string for passing to API
    URL = BASE_URL & "?apikey=" & API_KEY & "&format=text" & _
          "&provider=" & GetProviderString(provider) & "&url=" & longurl

    Set xml = GetMSXML

    With xml
      .Open "GET", URL, False
      .send
    End With

    result = xml.responsetext
    GetShortURL = result

  End If
End Function

The GetMSXML function may be found on the MSXML Object Library page.

Sample Usage

Sub test()
  Debug.Print GetShortURL("http://www.google.com", is_gd)
End Sub

Let Tiny-URL choose a provider at random

First, we need to define an additional constant, since the endpoint URL for random providers is different.

Const RANDOM_URL As String = "http://tiny-url.info/api/v1/random"

(If I always wanted a random provider, I would just redefine BASE_URL. Personally I like to choose because some of these URL shorteners look like they could disappear at any time.)

Otherwise the function is nearly identical. The upside to this method, however, is that no API key is required.

Function GetRandomShortURL(longurl As String) As String
Dim URL As String
Dim result As String
Dim xml As Object  ' MSXML2.XMLHTTP60

  ' validate the URL first
  If IsValidURL(longurl) Then
    ' build URL string for passing to API
    URL = RANDOM_URL & "?format=text" & "&url=" & longurl

    Set xml = GetMSXML

    With xml
      .Open "GET", URL, False
      .send
    End With

    result = xml.responsetext
    GetRandomShortURL = result

  End If
End Function

Sample Usage

Sub test()
  Debug.Print GetRandomShortURL("http://www.google.com")
End Sub
About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »


Related Articles:


Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

This article is closed to any future comments.
Peltier Tech Charting Utilities for Excel