Generating a QR code server-side usually means pulling in a heavyweight image library, or shelling out to a third-party service that watermarks the output or rate-limits you into a paid plan. Here's the simplest version that actually works in production.
The core idea: a QR code is just a grid of black/white modules encoding your data with Reed-Solomon error correction, then rendered as an image. You don't need to implement the encoding yourself — you need an endpoint that returns either a ready-to-embed SVG or the raw module matrix if you want to render it yourself (canvas, terminal, whatever):
curl "https://qr-api.p.rapidapi.com/v1/qr?data=https://example.com&ecc=M&cellSize=8" \
-H "X-RapidAPI-Key: <your-key>" \
-H "X-RapidAPI-Host: qr-api.p.rapidapi.com" \
--output qr.svg
That's an SVG you can embed directly with an <img> tag or inline it for crisp scaling at any size — no rasterization artifacts. A few things worth knowing before you ship this:
-
Error correction level matters more than people think.
H(30% recovery) survives a logo overlay or a slightly damaged print;L(7%) is fine for a clean digital display. Don't default to the highest level everywhere — it makes the code visually denser for no benefit if nothing's ever going to obscure it. - Cap your input length. QR codes get exponentially denser as the encoded string grows. If you're encoding a URL, shorten it first; don't just dump JSON payloads into a QR code and expect a scannable result.
- SVG over PNG when you can. Vector output means no blurring when someone screenshots and enlarges it, which happens more than you'd expect with QR codes.
I built QR API as a free-to-start, stateless endpoint for exactly this — no data logged, no watermark, SVG or raw matrix output. Same provider also runs Validate (IBAN/email/phone/etc. validation) and Currency API (exchange rates) if you need those too.
Top comments (0)