# Building a QR code generator with AI

URL: https://ekom.blog/building-a-qr-code-generator-with-ai/
Published: 2023-06-09
Author: Ekom
Topics: Build with AI

> Build a one-page QR code generator with AI help: the prompt, a short working script using a library, and what to test so the codes scan every time.

You need a QR code for a menu, a poster or a business card. You search for a generator and land on a site that wants an email address, adds its own logo and, worse, makes your code point to its own redirect so it stops working when your "free trial" ends.

A QR code is just a picture of some text. You can make one yourself, for free, in a single HTML file. This is a good first AI-assisted project: small, visible and easy to test.

The finished thing: one page where you type a web address, press a button and download a QR code image, with nothing sent to anyone's server.

## What a QR code really is

A QR code encodes text (usually a URL) as a pattern of squares. A phone's camera reads the pattern and turns it back into text. There's no tracking built in. A code that "expires" isn't a QR property: it's a redirect on someone's server.

This is the key lesson for anyone who prints codes: encode your own address directly, so the code keeps working as long as your page does. If you want to change where it points later, point it at a URL on your own site that you can redirect. I discuss redirects in redirects on WordPress: 301, 302 and the mistakes I keep seeing.

## Step 1: don't write the maths yourself

Generating a QR code involves error correction and a lot of fiddly encoding. Use a well-known library. A popular open-source one is the qrcode package for JavaScript. It can draw a code onto a canvas element in the browser.

Download the library file and keep a copy next to your page, rather than loading it from a random address each time. That way your tool still works offline and can't change under you.

## Step 2: write the prompt

Tell the AI exactly what you want, what you don't, and the limits. Something like:

Write a single HTML file with a text input for a URL, a "Generate" button, and a canvas that shows a QR code. Use the qrcode JavaScript library, loaded from a local file called qrcode.min.js. Add a "Download PNG" link. Validate that the input starts with http:// or https://. No frameworks, no tracking, no data sent anywhere. Keep it under 80 lines and add comments.

Why it works: it names the library, the file layout, the validation and the limits. That follows the approach in prompting for code: how to ask AI for something that actually runs.

## Step 3: the result

Expect something close to this. Read it, don't just paste it:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>QR code generator</title>
<script src="qrcode.min.js"></script>
</head>
<body>
<input id="url" type="url" placeholder="https://example.com" size="40">
<button id="make">Generate</button>
<p><canvas id="qr"></canvas></p>
<a id="download" download="qr-code.png" hidden>Download PNG</a>

<script>
const input = document.getElementById("url");
const canvas = document.getElementById("qr");
const link = document.getElementById("download");

document.getElementById("make").addEventListener("click", () => {
const value = input.value.trim();
if (!/^https?:\/\//i.test(value)) {
alert("Please enter an address starting with http:// or https://");
return;
}
QRCode.toCanvas(canvas, value, { width: 300, margin: 2 }, (error) => {
if (error) { alert("Could not make the code."); return; }
link.href = canvas.toDataURL("image/png");
link.hidden = false;
});
});
</script>
</body>
</html>

Save it as an .html file in the same folder as qrcode.min.js, open it in your browser, and try it.

## What to check in the code

- Does the library file exist where the script says? The most common failure is a wrong path.

- Is the library's function name the one you have? Different builds expose slightly different names. If QRCode isn't defined, check the library's README for the browser build.

- Is validation doing something? Type hello and see the alert.

- Does anything leave your computer? Open the Network tab in developer tools while you generate a code. There should be no requests.

## Step 4: test like it's going to print

A QR code that looks fine on a screen can fail in the real world. Test with at least two phones and two scanner apps, or the built-in camera. Then:

- Print it at the smallest size you'll use. If it won't scan at that size, make it bigger.

- Keep high contrast. Dark on light. Inverted (light on dark) codes fail on some scanners.

- Keep the quiet zone. The white margin round the code matters. Don't crop it.

- Keep URLs short. Longer text means a denser pattern that's harder to scan at small sizes. Use a short, clean address on your own domain.

- Test from a distance, like a poster on a wall.

Don't put a logo over the middle unless you've tested it thoroughly. Codes tolerate some damage by design, but it's easy to go too far.

## Step 5: improve it, one change at a time

Ideas for next steps, each as its own prompt:

- An error-correction level option (higher means more resilient and denser).

- A choice of size.

- Downloading as SVG for print, where scaling matters. Ask for it explicitly.

- A campaign tag: adding ?utm_source=poster to the URL so you can see scans in your analytics. This is a lovely use for web people: a poster in a shop can become a measurable channel.

That last one is where this tool earns its keep. Without tags, scans from print show up as plain "direct" traffic, and you never learn what worked.

## What tends to go wrong

Common problems you'll meet if you build this:

- The download link saves a blank image because the code was drawn after the link was read. Set the link after drawing.

- The AI suggests loading the library from a CDN with a made-up address. Don't accept a link you haven't checked.

- It adds a "save to server" feature you didn't ask for. Remove it.

## A safety note on QR codes

Be wary of scanning codes in public places. Stickers placed over genuine codes are a known trick. When you print your own, put it somewhere that's hard to overlay, and show the short URL in plain text near the code so people can compare.

## Key takeaways

- A QR code is just encoded text, with no expiry built in; link directly to your own page.

- Use a trusted library; don't write the encoding yourself.

- Give the AI a precise prompt with limits, then read and test what it returns.

- Print-test at the real size, with real phones.

## Related reading

- Never put API keys in front-end code

- Building a tiny tool with AI: from idea to working page in an afternoon

- Licence keys explained for small software

Next step: make one for your own website, tag it, and stick it somewhere to see whether anybody scans it.

---

From Ekom (https://ekom.blog/). More: https://ekom.blog/llms.txt
