The bug to avoid first: building the request with an f-string. f"...qr.png?url={link}" with link = "https://shop.com/?a=1&b=2" produces a code for https://shop.com/?a=1, because the API reads &b=2 as a separate parameter. Nothing errors, and the image looks fine. urlencode fixes it.
Save a PNG with the standard library
from urllib.parse import urlencode
from urllib.request import urlretrieve
params = urlencode({"url": "https://example.com/menu?table=4", "size": 1024})
urlretrieve(f"https://qrcodesforever.com/qr.png?{params}", "menu-qr.png")
That writes a 1024-pixel PNG whose code opens https://example.com/menu?table=4. Swap qr.png for qr.svg to get a vector file; size only applies to PNG. Allowed sizes are 512, 1024, 2048 and 4096, and anything else silently gives 1024.
On a bad input, urlretrieve raises HTTPError. A 400 means the content was missing or invalid, a 413 means it won't fit in one code, and e.read() has the one-line reason.
With requests
import requests
r = requests.get(
"https://qrcodesforever.com/qr.svg",
params={"type": "wifi", "ssid": "Cafe Guest", "password": "coffee123"},
timeout=10,
)
r.raise_for_status()
with open("wifi.svg", "wb") as f:
f.write(r.content)
requests encodes params itself. The code inside wifi.svg contains WIFI:T:WPA;S:Cafe Guest;P:coffee123;;, which iPhone and Android cameras offer to join. If the network name or password contains ;, ,, :, " or \, the API escapes it with a backslash, as the Wi-Fi format requires.
Batch from a CSV
import csv
import time
from urllib.parse import urlencode
from urllib.request import urlretrieve
with open("links.csv", newline="") as f:
for row in csv.DictReader(f): # columns: name, url
q = urlencode({"url": row["url"], "size": 1024})
urlretrieve(f"https://qrcodesforever.com/qr.png?{q}", f"{row['name']}.png")
time.sleep(0.2)
The pause keeps the batch sequential and gentle; there's no published rate limit. For a one-off list, the bulk QR code generator takes the same CSV in a browser.
Payment codes are where the API saves the most work. This dictionary makes a SEPA transfer code (EPC, sometimes called GiroCode):
{"type": "epc", "name": "Max Mustermann", "iban": "DE89370400440532013000", "amount": "25.00", "reference": "Invoice 123"}
The API checks the IBAN's checksum before drawing, then writes the eleven-line BCD payload that European banking apps read. A mistyped IBAN gets a 400 instead of a code that fails at the bank. The embed reference shows the exact payload and every other kind.
In Django and Flask templates
To show a code on a page, skip the download and let the browser fetch the image. Jinja (Flask) and Django both have a urlencode filter:
<img src="https://qrcodesforever.com/qr.svg?url={{ page_url | urlencode }}"
alt="QR code for this page" width="160" height="160">
In Django templates the filter is written {{ page_url|urlencode }}. Both leave / unencoded and encode the rest, which our API reads correctly.
When a library fits better
If you need to work offline, or the content shouldn't leave your machine, use segno or qrcode. Both make plain codes well. You'd then write the Wi-Fi escaping, vCard lines and EPC fields yourself. Before printing a large run either way, scan a few files with your phone or our QR code scanner.