NewConnect Claude, Cursor, ChatGPT, and other AI agents to API Ninjas via our MCP server

How to Generate QR Codes in Python

QR Code APIUpdated May 23, 2026
To generate a QR code in Python, send a GET request to the API Ninjas QR Code endpoint and write the binary response to a .png file:
qr.py
import requests

API_KEY = 'YOUR_API_KEY'
response = requests.get(
    'https://api.api-ninjas.com/v1/qrcode',
    params={'data': 'https://api-ninjas.com', 'format': 'png'},
    headers={'X-Api-Key': API_KEY},
)
with open('qr.png', 'wb') as f:
    f.write(response.content)

Prerequisites

Step 1: Get your API key

Sign up at api-ninjas.com/register.

Step 2: Make the API request

Pass the text or URL you want to encode as the data parameter, and format=png for a PNG image:

qr.py
import requests

API_KEY = 'YOUR_API_KEY'
response = requests.get(
    'https://api.api-ninjas.com/v1/qrcode',
    params={'data': 'https://api-ninjas.com', 'format': 'png'},
    headers={'X-Api-Key': API_KEY},
)
with open('qr.png', 'wb') as f:
    f.write(response.content)

Step 3: Use the response

The response body is raw image bytes — write them to a file using binary mode ('wb'). Do not call response.json() for image responses.

Complete example with sizing and colors

qr.py
import os
import requests

API_KEY = os.environ['API_NINJAS_KEY']
URL = 'https://api.api-ninjas.com/v1/qrcode'


def generate_qr(data: str, output: str, size: int = 256, fg: str = '000000', bg: str = 'ffffff') -> None:
    response = requests.get(
        URL,
        params={
            'data': data,
            'format': 'png',
            'size': size,
            'fg_color': fg,
            'bg_color': bg,
        },
        headers={'X-Api-Key': API_KEY},
        timeout=10,
    )
    response.raise_for_status()
    with open(output, 'wb') as f:
        f.write(response.content)


if __name__ == '__main__':
    generate_qr('https://api-ninjas.com', 'site.png', size=512)
    print('Wrote site.png')

Customizing QR code colors

Use fg_color and bg_color (6-digit hex, no leading #):

Python
# Brand-colored QR code (blue on light gray)
generate_qr(
    'https://api-ninjas.com',
    'brand.png',
    size=512,
    fg='1664ea',
    bg='f6f9fc',
)

Encoding a vCard contact

QR codes can encode contact details so a phone scan adds the person to its contacts:

Python
vcard = '''BEGIN:VCARD
VERSION:3.0
FN:Jane Doe
ORG:Acme Inc.
TEL:+1-555-0100
EMAIL:jane@example.com
END:VCARD'''

generate_qr(vcard, 'jane-vcard.png', size=512)

Encoding Wi-Fi credentials

Generate a QR code that joins guests to your Wi-Fi network:

Python
# Generate a QR code that connects to a Wi-Fi network when scanned
wifi = 'WIFI:T:WPA;S:GuestNetwork;P:correcthorsebatterystaple;;'
generate_qr(wifi, 'guest-wifi.png')

Generating QR codes in bulk

Python
urls = {
    'home':    'https://api-ninjas.com',
    'pricing': 'https://api-ninjas.com/pricing',
    'docs':    'https://api-ninjas.com/api',
}

for name, url in urls.items():
    generate_qr(url, f'{name}.png')
    print(f'Wrote {name}.png')

For barcode generation use the Barcode API. For more developer utilities see Password Generator and Lorem Ipsum.

Frequently asked questions

What output formats are supported?

The API returns PNG by default. You can also request other image formats by setting the format parameter.

Can I customize the QR code colors?

Yes. Use fg_color and bg_color query parameters with 6-character hex codes (no leading #) — for example, fg_color=1664ea&bg_color=ffffff.

How big are the generated QR codes?

The default size is 256 pixels. You can request larger images with the size parameter (e.g., size=512 for a 512×512 image).

Can I encode Wi-Fi credentials, vCards, or other formats?

Yes. QR codes are content-agnostic — encode any text you like. Common formats include WIFI: strings for Wi-Fi credentials and BEGIN:VCARD blocks for contact cards.

Is there a free tier?

Yes. Every API Ninjas account includes a free tier — no credit card required.