ColdFusion UDFs for Google URL Shortening service

This post is more than 2 years old.

I wrote this in about five minutes and it doesn't support auth tokens yet, but here are two UDFs that make use of Google's URL Shortening API. First, the shorten call:

<cffunction name="googleURLShorten" output="false" returnType="string"> <cfargument name="url" type="string" required="true"> <cfset var httpResult = ""> <cfset var result = "">

&lt;cfset var body = {"longUrl"=arguments.url}&gt;

&lt;cfset body = serializeJson(body)&gt;

&lt;cfhttp url="" method="post" result="httpResult"&gt;
	&lt;cfhttpparam type="header" name="Content-Type" value="application/json"&gt;
	&lt;cfhttpparam type="body" value="#body#"&gt;
&lt;cfset result = httpResult.filecontent.toString()&gt;
&lt;cfreturn deserializeJSON(result).id&gt;


And then the reverse:

<cffunction name="googleURLExpand" output="false" returnType="string"> <cfargument name="url" type="string" required="true"> <cfset var httpResult = ""> <cfset var result = "">

&lt;cfhttp url="" method="get" result="httpResult"&gt;&lt;/cfhttp&gt;

&lt;cfset result = httpResult.filecontent.toString()&gt;
&lt;cfreturn deserializeJSON(result).longUrl&gt;


And a quick test script:

<cfset sampleURL = ""> <cfset test = googleURLShorten(sampleURL)> <cfoutput> I shorteneded #sampleURL# to #test#.<br/> </cfoutput>

<cfset reversed = googleURLExpand(test)> <cfoutput> I expanded it to #reversed#. </cfoutput>

When run, I get:

I shorteneded to I expanded it to

Love that typo on the first line. Anyway, the UDFs could be improved with simple error checking and optional token support. But this is the best you get in five minutes. ;)

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by andy matthews posted on 1/11/2011 at 3:48 AM

Sweet! I've been waiting for this to come out so that I could add it to Shrinkadoo!

Comment 2 by SuperAlly posted on 1/11/2011 at 3:50 AM

That's very cool Ray, thanks again. I was just asked about this the other day.

Comment 3 by Wookie posted on 1/13/2011 at 10:25 PM

Brilliant! Love your "5 minutes" of work.

Comment 4 by Raymond Camden posted on 1/13/2011 at 10:52 PM

I've posted both to

Comment 5 by Phillip Senn posted on 1/15/2011 at 1:02 AM

Google says "All URLs and click analytics are public and can be shared by anyone".
If you go here, you (anyone) can see usage statistics: