What a QR code is and why you'd generate one

A QR code is a square barcode that stores information — usually a URL, text, contact details, or WiFi credentials — that a phone camera can read instantly. You generate one when you want to let someone scan their phone instead of typing something out, or when you need to embed data into a physical product, document, or advertisement.

If you're building software or a website, you'll generate QR codes programmatically using a library or API rather than using an online generator. This lets you create hundreds of codes at once, customize them for your application, and keep the process automated as part of your workflow.

The most common approach is to use a QR code library in your programming language — Python, JavaScript, Java, C#, and others all have solid options — or to call a QR code API that returns an image you can display or save.

Key Takeaways

  • QR code libraries like qrcode (Python), QRCode.js (JavaScript), and ZXing (Java) let you generate codes directly in your code without external API calls.
  • Most libraries take a string of data and return an image file or data URL that you can save, display, or embed in a document.
  • API-based generators like the Google Charts API or qr-server.com are simpler for one-off codes but add a network request to your application.
  • You can customize size, error correction level, and appearance, though the core data and scannability should not be altered.
  • Test your generated codes with a real phone camera before deploying to production.

Using a library in Python

The qrcode library is the standard choice for Python. Install it with pip install qrcode[pil] (the [pil] part lets you save images). Then create a code in three lines:

Import the library, create a QR code object with your data, and save or display it. The data can be any string — a URL, plain text, or structured data. The library handles encoding automatically. By default it creates a PNG image, but you can also get the raw image object and manipulate it further before saving.

If you need to generate many codes at once — say, one for each product in a catalog — loop through your data and save each with a unique filename. The library is fast enough that generating hundreds of codes takes only seconds.

Using a library in JavaScript

For browser-based JavaScript, QRCode.js is lightweight and requires no build step. Include it in your HTML, then create a code by pointing it at a DOM element:

The library renders the QR code as an SVG or canvas element directly in the page. You can then let the user download it, print it, or send it to a server. If you're using Node.js on the backend, the qrcode npm package works similarly to the Python version — install with npm install qrcode and generate images as files or data URLs.

For React applications, wrap the QR code generation in a component so it regenerates whenever the data changes. This is useful for dynamic codes where the URL or text is user-generated.

Using an API instead of a library

If you don't want to add a dependency or generate codes on your server, you can call a QR code API and get back an image. The Google Charts API is simple: construct a URL with your data and request it, and you get a PNG image back. The qr-server.com API works the same way and is free with no rate limits for reasonable use.

The trade-off is that every code generation requires a network request, which adds latency. For a website that generates one code per user session, this is fine. For an application that generates hundreds of codes, a library on your server is faster and more reliable.

APIs are also useful when you're working in an environment where you can't install libraries — for example, in a spreadsheet formula or a low-code platform. You can often embed the API URL directly as an image source.

Customizing size, colors, and error correction

Most libraries let you set the size (measured in pixels or "boxes"), the color of the code and background, and the error correction level. Error correction means the code will still scan even if part of it is damaged or obscured — higher levels tolerate more damage but make the code larger and more complex.

For appearance, keep the contrast high (dark code on light background is standard) and avoid placing logos or images over the code unless you're certain the error correction level is high enough. The quiet zone — the white border around the code — should not be removed or made too small, or scanners may fail to recognize it.

Test your customizations with a real phone camera before using them in production. What looks good on screen may not scan reliably in print or at a distance.

Saving and embedding QR codes

Once you've generated a code, you need to decide whether to save it as a file or embed it as a data URL. Saving as a file (PNG, SVG, or PDF) is best if you're printing the code or storing it for later use. A data URL is best if you're displaying it in a web page and don't need to persist it separately.

For high-quality printing, SVG or PDF formats are better than PNG because they scale without losing sharpness. Most libraries can output SVG directly, or you can convert a PNG to SVG using a tool like Potrace if needed.

If you're embedding codes in documents (Word, PDF, email), save as PNG or SVG and insert the image normally. Make sure the resolution is high enough that the code remains scannable when printed or viewed on screen.

Testing your QR codes

Before you deploy codes to production, test them with at least two different phone cameras — iOS and Android, or different apps. Point the camera at the code and confirm it opens the correct URL or displays the correct text. Test at different distances and angles to make sure it's robust.

If a code fails to scan, check that the data is not corrupted, the contrast is high, and the quiet zone is intact. If you customized the appearance, try reverting to defaults to isolate whether your changes broke it. Most scanning failures are caused by low contrast, missing quiet zone, or data that's too long for the error correction level you chose.

For codes you're printing, test a printed sample before mass-producing. Screen brightness and print quality can affect scannability.

Frequently Asked Questions

What's the difference between a library and an API for QR codes?

A library runs on your machine or server and generates codes locally — it's faster and doesn't require a network call. An API is a web service you call to generate codes — it's simpler if you can't install dependencies, but slower and dependent on the API being available.

How much data can a QR code hold?

A standard QR code can hold up to 4,296 characters of alphanumeric text, though longer data makes the code larger and more complex. URLs are typically 50 to 200 characters. If your data is longer, consider using a shorter URL or a different encoding format.

Can I make a QR code that's a different color?

Yes, most libraries let you set custom colors for the code and background. Keep the contrast high so scanners can read it reliably. Avoid light codes on light backgrounds or dark on dark.

What format should I save a QR code in?

PNG is fine for web and email. SVG or PDF is better for printing because they scale without losing sharpness. Choose based on where the code will be used and how large it needs to be.

Do I need to pay for a QR code library?

No. The most popular libraries — qrcode for Python, QRCode.js for JavaScript, ZXing for Java — are open source and free. Some online QR code generators charge for features like custom branding, but the core functionality is always free.