McAPI - QR API Ruby

Ruby sample code to create QR barcodes with the McAPI QR REST API service. The sample uses Ruby 2.6 but the API can also be used with Ruby 3. The sample uses the packages uri, net/http and openssl. which should all be available with a standard Ruby installation. If not, simply install them using GEM.

Requirements: A free RapidAPI account. All samples below can be tried with the free plan of the McAPI QR API. Replace YOUR_API_KEY with your own api key.

The sample can be used with the free tier of the API, see RapidAPI McAPI QR Listing for available plans.

See the overview page for available parameters and error codes.

Create QR code using Ruby

Shown is the creation of a QR code with Ruby encoding the URL https://mcapi.io. The code is created in red (dot color) and blue (finder color) with a dot size of 10 pixels. The dot style is set to "1" (for separated dots), the shape is set to "1" (for dots in disc or circle shape). The output format is "png". As the QR error correction is not specified, the API will use ECC level "M", sufficient for most purposes. Use "Q" (numeric value "3") for highest error correction capability.

The source code:

require 'uri'
require 'net/http'
require 'openssl'

url = URI("https://mcapi-qr.p.rapidapi.com/")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(url)
request["content-type"] = 'application/json'
request["x-rapidapi-key"] = 'YOUR_API_KEY'
request["x-rapidapi-host"] = 'mcapi-qr.p.rapidapi.com'
request.body = "{
    \"data\": \"https://mcapi.io\",
    \"dotSize\": 10,
    \"finderColor\": \"#0000FF\",
    \"dotColor\": \"#FF0000\",
    \"dotStyle\": 1,
    \"dotShape\": 1,
    \"format\": \"png\"
}"

response = http.request(request)

The response data will be in response.read_body, e.g.:

{
  "service":"McAPI QR Code Generator, https://mcapi.io",
  "version":"V1",
  "format":"png",
  "code":"data:image/png;base64,iVBORw ... K5CYII="
}

McAPI QR API - Sample QR barcode with Ruby

The returned code from the Ruby request:

Image of McAPI QR API Sample Barcode Ruby

Encoding UTF-8 characters and text in QR codes

Ruby can natively handle UTF-8 characters or strings (like german "umlauts", e.g. "ÄÖÜ", or non-latin script, e.g. "日本語"). Here's an example with the same parameters as above but with Unicode content (umlauts and japanese characters):

require 'uri'
require 'net/http'
require 'openssl'

url = URI("https://mcapi-qr.p.rapidapi.com/")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(url)
request["content-type"] = 'application/json'
request["x-rapidapi-key"] = 'YOUR_API_KEY'
request["x-rapidapi-host"] = 'mcapi-qr.p.rapidapi.com'
request.body = "{
    \"data\": \"123456abcdefÄÖÜ日本語\",
    \"dotSize\": 10,
    \"finderColor\": \"#0000FF\",
    \"dotColor\": \"#FF0000\",
    \"dotStyle\": 1,
    \"dotShape\": 1,
    \"format\": \"png\"
}"

response = http.request(request)

The generated QR code with Unicode content:

Image of McAPI QR API Sample Barcode Unicode Ruby

Scanning QR codes

It's recommended to test scan the created QR codes. On iOS, the built-in camera app will natively detect and scan QR codes in the camera's view. For Android, use Google's Zxing Barcode Scanner app (free). Both apps will scan from the computer display, no need to print the codes.

Back to McAPI QR API main page.