QR Codes for free

Posted by codepope on Sunday, July 24, 2022
Last Modified on Saturday, August 31, 2024

[ Update from 2024: Seems this API has gone away..]

QR Codes for free¹

QR Codes are one of the weirder successes out there. Honestly, I thought they were ridiculous originally, when seeing them plastered on the side of a building in San Francisco. Why? They just felt like an odd clash of low and high tech to me.

Well I was wrong and in other parts of the world, they turned out to be the cheapest way of wirelessly pushing small bits of data into devices, and not by using RF but instead using the camera.

And now, well, QR codes are ubiquitous. Technology has reached the point where cameras just look for QR codes when they are looking at anything (no loading up a QR code reader) and they are used everywhere (despite the dangers; that URL in the QR code could take you anywhere). But they are solidly here and they are useful.

Making QR Codes

So, generating a QR code is something that comes up for me a lot, usually to embed in a project. I’m just doing another badge at the moment and I wanted a QR code for that. Now, there’s lots of online services which make QR codes, but they are all about trying to upsell you on their proxy/tracking features because everything is marketing-driven these days.

While I was looking for a free service I stumbled over an old Google API for generating QR codes, surprisingly hidden inside Google Charts in a now deprecated but still functional API. The Infographics QR Codes endpoint is a super simple API for getting a QR code. You do a GET on it, you get back a PNG with your QR Code. No auth, tokens, magic handshakes, that’s it. Thats’s the API.

Driving the Google Charts API

There are, of course, parameters to pass to configure what. Let’s look at an example:

If you visit this in your browser

https://chart.googleapis.com/chart?cht=qr&chs=256x256&chl=https://codepope.dev

You’ll get

First QR Code

Let’s break that URL down.

First, there’s the endpoint:

https://chart.googleapis.com/chart

Super generic, this endpoint is, so the first parameter we get to pass is going to select what kind of chart we want:

?cht=qr

This selects the QR code maker.

Our QR code needs a size (there’s no default) and thats passed with the chs parameter:

&chs=256x256

Its value is widthxheight.

Finally, we can give it the string that will be the payload for this QR Code, using the chl parameter:

&chl=https://codepope.dev

Usual rules apply here; the string needs to be URL escaped (putting quotes around it just adds quotes to the string thats encoded into the payload).

And that’s it. You can make QR codes without libraries or pay-for services.

Bonus Round (of Parameters)

Well not quite it. There’s two other parameters you can optionally set. One is probably only useful for some people - choe defaults to utf-8 and selects how the payload should be encoded (Shift_JIS and ISO-8859-1 are the other options). The other is more directly useful - chld lets you set the level of error correction in the QR Code (L (the default), M, Q, H - 7%, 15%, 25%, 30%) and optionally append a margin width (which defaults to 4, for the width of 4 QR code rows).

So, if we wanted our QR code set to maximum resilience and only have a one row wide margin, we would add &chld=H|1 into the parameters giving:

https://chart.googleapis.com/chart?cht=qr&chs=256x256&chld=H|1&chl=https://codepope.dev

resulting in:

Big Strong QR Code

Have QR-Fun!

¹ Where free means using a deprecated API but there doesn’t seem to be any sign of Google killing it and really they shouldn’t as its a really useful service to the world and OH GOD WHAT HAVE I SAID THEY’LL REALISE THEY HAVEN’T KILLED IT NOW… caveat implentor vs life’s too short.