# 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.

Source: https://useqr.app/docs/developers/generate-a-qr-code-in-ruby · Last reviewed 2026-08-21 · UseQR is free forever, no signup.

---

## rqrcode

`gem install rqrcode` (or add it to the Gemfile). Encoding and rendering are two steps:

```ruby
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](/docs/spec/error-correction-levels-explained). 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](/glossary/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:

```ruby
# 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
```

```erb
<%= 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](/docs/developers/free-qr-code-api-no-key) needs only the standard
library:

```ruby
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](/docs/developers/bulk-qr-generation-at-scale). The equivalent
Python patterns live on the
[Python page](/docs/developers/generate-a-qr-code-in-python).

## Verify styled output

Round-trip anything you restyle before it ships:

```ruby
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](/docs/developers/why-verify-that-your-qr-code-decodes) 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

- https://useqr.app/url
- https://useqr.app/json
- https://useqr.app/validate
