Developers & agents
Generate a QR code in Ruby
The rqrcode gem covers Ruby: RQRCode::QRCode.new("https://example.com") then as_svg for vector output or as_png (pure-Ruby ChunkyPNG, no ImageMagick) for raster. In Rails, a helper returning as_svg keeps codes inline and crisp. Or fetch the keyless API with Net::HTTP and URI.encode_www_form.
rqrcode
gem install rqrcode (or add it to the Gemfile). Encoding and rendering are two steps:
require "rqrcode"
qrcode = RQRCode::QRCode.new("https://example.com", level: :q)
# vector — the right choice for print
File.write("qr.svg", qrcode.as_svg(module_size: 6))
# raster
png = qrcode.as_png(size: 512, border_modules: 4)
IO.binwrite("qr.png", png.to_s)
level: takes :l, :m, :q or :h — the standard
error-correction levels. A detail worth
knowing: as_png renders through ChunkyPNG, which is pure Ruby — no ImageMagick, no
native extension, nothing to install on the server. border_modules: 4 is the 4-module
quiet zone; do not shrink it. For a quick terminal check,
puts qrcode.to_s prints the matrix as text.
A Rails helper
SVG inlines cleanly into ERB and stays sharp at any display size:
# app/helpers/qr_helper.rb
module QrHelper
def qr_svg(data, size: 6)
RQRCode::QRCode.new(data).as_svg(module_size: size).html_safe
end
end
<%= qr_svg(ticket_url(@ticket)) %>
html_safe is justified here because rqrcode generated the markup — never apply it to SVG
from user input. Generating locally like this is also the right pattern for anything
sensitive: WiFi credentials and personal data should not travel to a third party just to
become an image.
The keyless API with Net::HTTP
For styled output (colours, module shapes) or typed payloads with server-side validation, the keyless API needs only the standard library:
require "net/http"
uri = URI("https://useqr.app/api/v1/qr")
uri.query = URI.encode_www_form(data: "https://example.com/sale?src=poster", size: 1024)
File.binwrite("qr.png", Net::HTTP.get(uri))
URI.encode_www_form handles the percent-encoding — string interpolation is how payloads
containing & get silently truncated. Batch runs can POST /api/v1/qr/batch with up to
100 items per call; larger pipelines are covered in
bulk generation at scale. The equivalent
Python patterns live on the
Python page.
Verify styled output
Round-trip anything you restyle before it ships:
require "json"
uri = URI("https://useqr.app/api/v1/verify")
uri.query = URI.encode_www_form(data: "https://example.com", color: "6366f1")
report = JSON.parse(Net::HTTP.get(uri))
raise report["issues"].join("; ") unless report["scannable"]
The endpoint renders, rasterises and decodes with a real decoder — the decode-verify loop as one GET, easy to drop into an RSpec example or a CI task.
FAQ
What is the standard Ruby gem for QR codes?
rqrcode. It encodes with rqrcode_core and renders SVG, PNG (via pure-Ruby ChunkyPNG) and terminal text with no native dependencies.
How do I show a QR code in a Rails view?
A helper that returns as_svg marked html_safe, called from ERB. SVG inlines into the page and stays crisp at any size and zoom.
Does rqrcode need ImageMagick?
No. PNG output goes through ChunkyPNG, which is pure Ruby, so it deploys anywhere the gem installs — including minimal containers.
How do I set error correction in rqrcode?
Pass level: :l, :m, :q or :h to RQRCode::QRCode.new. Use :h when a logo will overlay the code; :m is a sensible default otherwise.
Try it — free, no signup
Related
- A free QR code API with no key — UseQR's REST API needs no signup, no API key and no SDK. GET /api/v1/qr?data=hello returns a PNG. The shortest form is /q/hello.png, which drops straight…
- Generate a QR code in Python — Two options: call the keyless HTTP API with requests, which needs no dependencies beyond requests and no key, or use the qrcode library locally when you…
- Bulk QR generation at scale — Generating hundreds to millions of QR codes: the batch API and its limits, local generation with worker pools, determinism as a caching strategy, and manifests.
- Why you should verify that a QR code decodes — Rendering a QR code proves nothing about whether it scans. Styling, colour, logos and print all consume error-correction budget invisibly. The only…