McAPI - QR API Django / Python

Python sample code to create QR barcodes with the McAPI QR REST API service. The sample uses Python 3 but the API can also be used with older versions. For encoding Unicode / utf-8 content, Python 3 is preferred. Make sure to install the requests module, e.g. with PIP:

$ python3 -m pip install requests

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 Python 3 with requests module

Shown is the creation of a QR code with Python 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:

import requests

url = 'https://mcapi-qr.p.rapidapi.com/'

payload = '{\
    "data": "https://mcapi.io",\
    "dotSize": 10,\
    "finderColor": "#0000FF",\
    "dotColor": "#FF0000",\
    "dotStyle": 1,\
    "dotShape": 1,\
    "format": "png"\
}'
headers = {
    'content-type': 'application/json',
    'x-rapidapi-key': 'YOUR_API_KEY",
    'x-rapidapi-host': 'mcapi-qr.p.rapidapi.com"
}

response = requests.request('POST', url, data=payload, headers=headers)

The response data will be in response.text, 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 Python

The returned code from the Python request:

Image of McAPI QR API Sample Barcode Python

Encoding UTF-8 characters and text in QR codes

Special care has to be taken when creating QR barcode with UTF-8 characters or strings (like german "umlauts", e.g. "ÄÖÜ", or non-latin script, e.g. "日本語"). Before making the request make sure to properly encode the payload for UTF-8. Here's an example with the same parameters as above but with Unicode content (umlauts and japanese characters); note the difference in the request call for the data argument:

import requests

url = 'https://mcapi-qr.p.rapidapi.com/'

payload = '{\
    "data": "123456abcdefÄÖÜ日本語",\
    "dotSize": 10,\
    "finderColor": "#0000FF",\
    "dotColor": "#FF0000",\
    "dotStyle": 1,\
    "dotShape": 1,\
    "format": "png"\
}'
headers = {
    'content-type': 'application/json',
    'x-rapidapi-key': 'YOUR_API_KEY",
    'x-rapidapi-host': 'mcapi-qr.p.rapidapi.com"
}

response = requests.request('POST', url, data=payload.encode('utf-8'), headers=headers)

The generated QR code with Unicode content:

Image of McAPI QR API Sample Barcode Unicode Python

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.