What you need to generate a QR code
A QR code is a square barcode that stores data — usually a URL, text, or contact information — that a phone camera can read instantly. You can generate one three ways: using an online tool that requires no coding, installing a library in your programming project, or building the logic yourself from scratch. Most people use an online generator for one-off codes; developers use libraries when QR codes are part of a larger application.
The data you want to encode can be as short as a single character or as long as several thousand characters, depending on the error correction level you choose. Higher error correction means the code will still scan even if part of it is damaged or obscured, but it also makes the code larger and more complex-looking.
Key Takeaways
- Online QR code generators like QR-Server, QRCode.com, and Segno require no setup and work in your browser — paste your data, download the image, and you're done.
- Python's qrcode library and Node.js's qrcode package let you generate codes programmatically within your application with just a few lines of code.
- Most libraries let you control the size, error correction level, and output format (PNG, SVG, ASCII), so you can match your design needs.
- QR codes can store URLs, plain text, contact information (vCard format), WiFi credentials, or any string up to several thousand characters.
Using an online QR code generator
The fastest route if you need a code once or twice is an online tool. QR-Server (qr-server.com) and QRCode.com both work the same way: paste your data into a text field, and the site generates a scannable image you can download as PNG or SVG. No account needed, no software to install.
QR-Server also lets you customize the size and error correction level by adding parameters to the URL itself — for example, adding ?size=300x300 to the end of the generated URL makes the code 300 pixels square. This is useful if you're embedding codes in a document or email and need them to fit a specific space.
The trade-off is that online generators store your data on their server temporarily while they create the image. If you're encoding sensitive information like internal API keys or private URLs, a local tool is safer.
Generating QR codes in Python
If you're building a Python application that needs to generate codes on demand — for example, a ticketing system or a checkout page — the qrcode library is the standard choice. Install it with pip install qrcode[pil] (the [pil] part lets you save images; without it you can only generate ASCII art).
A basic example takes three lines:
import qrcode qr = qrcode.QRCode() qr.add_data("https://example.com") qr.make() img = qr.make_image() img.save("code.png")
You can control the error correction level by passing error_correct=qrcode.constants.ERROR_CORRECT_H (H is highest; L, M, and Q are lower levels). Higher correction makes the code larger but more resilient to damage. You can also set the box size in pixels and the border width in boxes.
For web applications, you can skip saving to disk and instead return the image directly to the browser using BytesIO, or generate SVG output instead of PNG if your design needs a vector format.
Generating QR codes in Node.js
The qrcode npm package is the Node equivalent. Install it with npm install qrcode, then generate a code in a few lines:
const QRCode = require('qrcode'); QRCode.toFile('code.png', 'https://example.com', { errorCorrectionLevel: 'H', width: 300 }, (err) => { if (err) throw err; console.log('QR code saved'); })
The package also supports generating to a data URL (for embedding in HTML), to a canvas element (in the browser), or to a string of SVG markup. Error correction levels are 'L', 'M', 'Q', and 'H', with the same trade-off as Python: higher correction means a larger, more complex code.
For Express applications, you can generate codes on the fly and stream them to the client without writing to disk, which is useful for dynamic URLs like order confirmations or event tickets.
Other programming languages and libraries
Most languages have QR code libraries. Java has ZXing (Zebra Crossing), which is also used by Android's built-in barcode scanner. C# has QRCoder. PHP has phpqrcode. Go has qrcode. Ruby has rqrcode. Each follows the same pattern: instantiate a code object, add your data, set options like size and error correction, and render to a file or string.
If you're choosing a library, check whether it supports the output format you need (PNG, SVG, ASCII, PDF) and whether it lets you set error correction level. Most do. Some libraries also support generating codes from images or files rather than just text strings, which is less common but useful for certain applications.
What data to encode and how to format it
A QR code can hold any text string, but certain formats are recognized by phone cameras and apps. A plain URL like https://example.com will open in the browser. A vCard (contact information) starts with BEGIN:VCARD and includes fields like NAME, TEL, and EMAIL. WiFi credentials use a special format starting with WIFI:.
For most uses, just encode the URL or text you want. The phone's camera app will recognize URLs and offer to open them. For contact information or WiFi, use your library's built-in vCard or WiFi methods if it has them, or format the string manually and test it with a phone before deploying.
Keep in mind that longer data makes larger codes. A short URL encodes in a smaller, simpler code than a long one. If you're printing codes or displaying them on small screens, consider using a URL shortener first.
Frequently Asked Questions
What's the difference between error correction levels?
Error correction levels L, M, Q, and H let the code be read even if up to 7%, 15%, 25%, or 30% of it is damaged or obscured. Higher levels make the code larger and more complex. Use L for codes in controlled environments (printed documents, screens); use H for codes that might get wet, folded, or partially covered.
Can I customize the colors or add a logo to a QR code?
Yes, but carefully. Most libraries let you set the foreground and background colors. Adding a logo in the center is possible but reduces readability — if you do it, use high error correction (H level) and test the code with multiple phones before deploying. Some online generators have built-in logo options.
What size should I make a QR code?
A code 200 pixels square is readable from about 2 feet away on a phone. Larger codes are readable from farther away. Smaller codes (under 100 pixels) are harder to scan. If you're printing, aim for at least 1 inch square. Test with your phone before finalizing the size.
Can I track how many times a QR code is scanned?
Not from the code itself — QR codes are just data. To track scans, use a URL shortener (like bit.ly) or a custom tracking service that logs requests. Encode the tracking URL in the QR code instead of the final destination URL.
What happens if I encode a very long URL?
The code becomes larger and more complex. If your URL is very long, use a URL shortener first, then encode the short URL. This keeps the code small and easier to scan, and also lets you change where the short URL points without regenerating the code.