Python

Generate QR Codes in Python

Free QR Code API · No API Key · pip install requests

Why Use an API Instead of a Library?

Python has several QR code libraries like qrcode and segno, but they add dependencies, require Pillow for PNG output, and you have to manage rendering yourself. The QR Gen API generates QR codes with a single HTTP call — no dependencies beyond requests, no image processing code, and the same result whether you run it on a laptop, a server, or a Lambda function.

Quick Start

Install requests if you haven't already:

pip install requests

Generate a QR code in three lines:

import requests

response = requests.get("https://qrgenapp.com/api/qr", params={
    "data": "https://example.com",
    "format": "png",
    "size": 512
})
response.raise_for_status()

with open("qr.png", "wb") as f:
    f.write(response.content)

print("Saved qr.png")

That's it. No API key, no signup, no configuration.

GET vs POST

For simple data like URLs and short text, GET requests with query parameters are the easiest approach. For longer or more complex data (WiFi configs, vCards, multi-line text), use POST with a JSON body to avoid URL encoding issues.

GET Request

import requests

response = requests.get("https://qrgenapp.com/api/qr", params={
    "data": "https://example.com",
    "format": "svg",
    "size": 512,
    "color": "1a1a1a",
    "ec": "H"
})
response.raise_for_status()

with open("qr.svg", "w") as f:
    f.write(response.text)

POST Request

import requests

response = requests.post("https://qrgenapp.com/api/qr", json={
    "data": "WIFI:T:WPA;S:MyNetwork;P:MyPassword;;",
    "format": "png",
    "size": 512,
    "color": "2563eb",
    "bg": "f8fafc",
    "ec": "Q"
})
response.raise_for_status()

with open("wifi-qr.png", "wb") as f:
    f.write(response.content)

Output Formats

FormatContent-TypeUse Case
pngimage/pngPrint materials, emails, general use
svgimage/svg+xmlWeb embedding, scalable graphics, smaller file size
jsonapplication/jsonCustom rendering, terminal output, canvas drawing

Generate WiFi QR Codes

def generate_wifi_qr(ssid, password, encryption="WPA", output="wifi.png"):
    """Generate a WiFi QR code that phones can scan to auto-connect."""
    import requests

    wifi_string = f"WIFI:T:{encryption};S:{ssid};P:{password};;"

    response = requests.post("https://qrgenapp.com/api/qr", json={
        "data": wifi_string,
        "format": "png",
        "size": 512,
        "ec": "H"
    })
    response.raise_for_status()

    with open(output, "wb") as f:
        f.write(response.content)

    return output

# Usage
generate_wifi_qr("CafeWiFi", "coffee2026", output="cafe-wifi.png")

Generate UPI Payment QR Codes

def generate_upi_qr(upi_id, name, amount=None, output="upi.png"):
    """Generate a UPI QR code for accepting payments."""
    import requests

    upi_uri = f"upi://pay?pa={upi_id}&pn={name}"
    if amount:
        upi_uri += f"&am={amount}"

    response = requests.post("https://qrgenapp.com/api/qr", json={
        "data": upi_uri,
        "format": "png",
        "size": 512,
        "ec": "H"
    })
    response.raise_for_status()

    with open(output, "wb") as f:
        f.write(response.content)

    return output

# Static QR (customer enters amount)
generate_upi_qr("shop@okaxis", "My Shop", output="shop-qr.png")

# Dynamic QR (amount pre-filled)
generate_upi_qr("shop@okaxis", "My Shop", amount=499.00, output="bill-499.png")

Batch Generation

Generate multiple QR codes efficiently using requests.Session for connection reuse:

import requests

urls = [
    "https://example.com/page-1",
    "https://example.com/page-2",
    "https://example.com/page-3",
    "https://example.com/page-4",
    "https://example.com/page-5",
]

session = requests.Session()

for i, url in enumerate(urls):
    response = session.get("https://qrgenapp.com/api/qr", params={
        "data": url,
        "format": "png",
        "size": 512,
        "ec": "H"
    })
    response.raise_for_status()

    filename = f"qr_{i + 1}.png"
    with open(filename, "wb") as f:
        f.write(response.content)

    print(f"Saved {filename}")

JSON Format for Custom Rendering

The JSON format returns the raw module matrix, which you can use for custom rendering — in a terminal, a canvas, or any other medium:

import requests

response = requests.get("https://qrgenapp.com/api/qr", params={
    "data": "Hello from Python",
    "format": "json"
})
data = response.json()

# Print QR code in terminal
for row in data["modules"]:
    print("".join("██" if cell else "  " for cell in row))

print(f"\nQR size: {data['size']}x{data['size']} modules")

Django / Flask Integration

Serve QR codes dynamically from your web app:

# Flask example
from flask import Flask, send_file
import requests
from io import BytesIO

app = Flask(__name__)

@app.route("/qr")
def qr_code():
    url = request.args.get("url", "https://example.com")

    response = requests.get("https://qrgenapp.com/api/qr", params={
        "data": url,
        "format": "png",
        "size": 256
    })
    response.raise_for_status()

    return send_file(
        BytesIO(response.content),
        mimetype="image/png",
        download_name="qr.png"
    )

Error Handling

import requests

try:
    response = requests.get("https://qrgenapp.com/api/qr", params={
        "data": "https://example.com",
        "format": "png"
    })
    response.raise_for_status()

    with open("qr.png", "wb") as f:
        f.write(response.content)

except requests.exceptions.HTTPError as e:
    error = e.response.json()
    print(f"API error: {error['error']}")
except requests.exceptions.ConnectionError:
    print("Could not connect to QR Gen API")

API Parameters Reference

See the full API Reference for all available parameters including data, format, size, color, bg, ec, and margin.

Start generating QR codes in Python

No API key needed. Just pip install requests and go.

Full API Reference