Generate QR Codes with ColdFusion Component

This morning I released an updated version of my ColdFusion CFC Wrapper, which is now at version 2.0.

The core reason for the update was to provide the new methods now included within the official API.

What’s changed?

There are four official new methods that have been added into the ColdFusion wrapper for the API, which are:

  • referers
  • countries
  • clicksByMinute
  • clicksByDay

These methods are a great way to provide a little analytical data for any generated URLs. Superb stuff and incredibly easy to obtain, especially though the CFC.

QR Codes

A significant addition to the service is the ability to generate Quick Response (QR) codes, or data matrix barcodes – those awesome little 2D barcodes that can be read by most smartphones, scanners etc.. a perfect way of storing and publishing text, information, or most commonly, a URL.

Iron Man QR Code It’s not new technology, it’s not really a new idea, but with the constant release of smartphones and devices capable of reading them (and so much more) it’s becoming more common-place within today’s society.

Naturally, as a format widely accessible to not only providers but also consumers, it’s become a ‘2.0’ option of delivery for marketing campaigns and advertising – an ideal, enigmatic way to promote your services or products.

Lost QR Code

As reported in Mashable, Google recently released their URL shortening service, which by all accounts included QR Code generation from the start. As such, it wasn’t a surprise to see step up and include this support in their services to ensure that their users weren’t missing out on the same capabilities.

Grabbing a QR Code

Although not an official method included within the API, ColdFusion makes it incredibly easy to obtain and retrieve this information, it has been included within the CFC.

Here’s how easy it is to generate the QR Code using the CFC:

	Instantiate the object.
<cfset objBitly = createObject('component', 
			username	=	'< your account username >',
			apikey		=	'< your api key >',
			parse		=	true
		) />

	The generateQRCode() method returns binary data 
	(the byte array) of the generated image.
<cfset qrResponse = objBitly.generateQRCode(
						shortURL	= ''
					) />

	Dump out the binary response from the 
	initial call (just to see what was returned).
<cfdump var="#qrResponse#" 	
		label="QRCode binary response" />

	Write the binary data as an image and display
	directly into the browser. Thanks, ColdFusion.
<cfimage action="writeToBrowser" 
		 source="#qrResponse#" />

The returned response from the cfdump of the binary data is shown below:

QR Code Binary Dump

And the image generated by passing the binary data into the cfimage tag can be seen here (in this case, this image contains a link to QR Code

What’s Happening Under The Hood?

Obtaining the generated QR Code is incredibly easy. The guys at have engineered their systems to allow anyone to obtain a QR Code by appending the string ‘.qrcode’ to the end of any shortened URL, for example:

Running directly in the browser, you get the generated image returned, which you can then copy / save to suit your needs. The actual call within the CFC Wrapper is very straightforward:

<cffunction name="generateQRCode" 
			hint="I take a generated URL, and return the 
			binary data of the generated QR Code image.">
	<cfargument name="shortURL" 
				required="true" type="string"  	
				hint="I am a short URL that you wish to 
						create a QR Code for." />
		<cfset var cfhttp 		= '' />
		<cfset var returnData 	= '' />
				Make the cfhttp request to obtain the image
				(appending the string '.qrcode' onto
				the shortened URL)
			<cfhttp url="#arguments.shortURL#.qrcode" />
			<!--- Successful response? --->
			<cfif cfhttp.StatusCode EQ '200 OK'>
					Obtain the binary data using the 
					toByteArray() method
				<cfset returnData = 
						cfhttp.fileContent.toByteArray() />
	<cfreturn returnData />

How do I get it?

You can download the revised version of the API wrapper from

  • Glyn

    Very cool post, will be playing with this for sure, I was not even aware this was in there :)

  • Nelson

    Hi Matt,

    I tried to pass an argument to objBitly.gererateQRCode rather than hardcoding the short URL by using:

    qrCode = objBitly.generateQRCode(;

    But got an error:
    coldfusion.runtime.TemplateNotFoundException: File not found: /testing/error.cfm

    Any idea why it does not work?


  • Matt

    Hi Nelson

    Thanks for your comment. The TemplateNotFoundException error would seem to indicate an error somewhere with perhaps a mapping on your server, as it’s trying to display an error page.

    The argument passed in to the generateQRCode() method as shown in your comment would fail. There are two types of return method, JSON and XML. If you are using the XML output, you will need to add an extra identifier to return the string value from the XML, like so:

    qrCode = objBitly.generateQRCode(;

    If you are using the JSON output format, the argument would need to resemble this:

    qrCode = objBitly.generateQRCode(;

    I hope this helps. :)

  • Bernhard


    found your incredible API and tried to generate a QR Code in CF9. Unfortunately I get the error “ColdFusion was unable to create an image from the specified source file.Ensure that the file is a valid image file.”

    What am I doing wrong please? Any ideas?

    Thanks very much, greetings

    • Bernhard

      Sorry, forgot to post the dump:


    • Bernhard

      Ahhh, me dummy person…..forgot to shorten den longURL. Sorry for bothering ;).

      • Matt

        Hi Bernhard. I’m glad you got it working :)