Skip to content

Basic Examples

Simple examples to get started with QR Code API.

cURL Examples

Generate a Simple QR Code

bash
curl "https://www.qrcodeapi.io/api/generate?data=Hello%20World" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -o hello.png

URL to QR Code

bash
curl "https://www.qrcodeapi.io/api/generate" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -G \
  --data-urlencode "data=https://example.com" \
  -o url-qr.png

Custom Size

bash
# Small (100px)
curl "https://www.qrcodeapi.io/api/generate?data=test&size=100" \
  -H "Authorization: Bearer YOUR_API_KEY" -o small.png

# Large (1000px)
curl "https://www.qrcodeapi.io/api/generate?data=test&size=1000" \
  -H "Authorization: Bearer YOUR_API_KEY" -o large.png

Custom Colors

bash
# Blue QR code
curl "https://www.qrcodeapi.io/api/generate" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -G \
  --data-urlencode "data=https://example.com" \
  --data "color=1e40af" \
  --data "background=eff6ff" \
  -o blue-qr.png

SVG Output

bash
curl "https://www.qrcodeapi.io/api/generate?data=test&format=svg" \
  -H "Authorization: Bearer YOUR_API_KEY" -o qr.svg

JavaScript Examples

Browser - Display QR Code

html
<!DOCTYPE html>
<html>
<head>
  <title>QR Code Demo</title>
</head>
<body>
  <input type="text" id="data" placeholder="Enter text or URL">
  <button onclick="generate()">Generate</button>
  <img id="qr" alt="QR Code">

  <script>
    async function generate() {
      const data = document.getElementById('data').value;
      const response = await fetch(
        `https://www.qrcodeapi.io/api/generate?data=${encodeURIComponent(data)}`,
        { headers: { 'Authorization': 'Bearer YOUR_API_KEY' } }
      );
      
      const blob = await response.blob();
      document.getElementById('qr').src = URL.createObjectURL(blob);
    }
  </script>
</body>
</html>

Node.js - Save to File

javascript
const fs = require('fs');
const https = require('https');

const url = 'https://www.qrcodeapi.io/api/generate?data=Hello';
const options = {
  headers: { 'Authorization': 'Bearer YOUR_API_KEY' }
};

https.get(url, options, (res) => {
  const file = fs.createWriteStream('qr.png');
  res.pipe(file);
  file.on('finish', () => {
    console.log('QR code saved to qr.png');
  });
});

Fetch API

javascript
async function generateQR(data) {
  const response = await fetch(
    `https://www.qrcodeapi.io/api/generate?data=${encodeURIComponent(data)}`,
    {
      headers: { 'Authorization': `Bearer ${process.env.API_KEY}` }
    }
  );
  
  if (!response.ok) {
    throw new Error(`HTTP ${response.status}`);
  }
  
  return response.blob();
}

// Usage
const blob = await generateQR('https://example.com');

Python Examples

Save QR Code to File

python
import requests

def generate_qr(data, filename='qr.png'):
    response = requests.get(
        'https://www.qrcodeapi.io/api/generate',
        params={'data': data},
        headers={'Authorization': 'Bearer YOUR_API_KEY'}
    )
    
    with open(filename, 'wb') as f:
        f.write(response.content)
    
    print(f'Saved to {filename}')

generate_qr('https://example.com')

Display in Jupyter Notebook

python
import requests
from IPython.display import Image, display

def show_qr(data, size=300):
    response = requests.get(
        'https://www.qrcodeapi.io/api/generate',
        params={'data': data, 'size': size},
        headers={'Authorization': 'Bearer YOUR_API_KEY'}
    )
    
    display(Image(response.content))

show_qr('https://example.com')

Using with Pillow

python
import requests
from PIL import Image
from io import BytesIO

def get_qr_image(data):
    response = requests.get(
        'https://www.qrcodeapi.io/api/generate',
        params={'data': data},
        headers={'Authorization': 'Bearer YOUR_API_KEY'}
    )
    
    return Image.open(BytesIO(response.content))

# Get image and manipulate
img = get_qr_image('https://example.com')
img = img.resize((500, 500))
img.save('resized-qr.png')

PHP Examples

Basic Generation

php
<?php
$data = 'https://example.com';
$apiKey = 'YOUR_API_KEY';

$url = 'https://www.qrcodeapi.io/api/generate?' . http_build_query([
    'data' => $data
]);

$context = stream_context_create([
    'http' => [
        'header' => "Authorization: Bearer $apiKey"
    ]
]);

$qr = file_get_contents($url, false, $context);
file_put_contents('qr.png', $qr);
echo "QR code saved!";
?>

Serve Directly to Browser

php
<?php
$data = $_GET['data'] ?? 'default';
$apiKey = 'YOUR_API_KEY';

$url = 'https://www.qrcodeapi.io/api/generate?' . http_build_query([
    'data' => $data,
    'size' => 300
]);

$context = stream_context_create([
    'http' => [
        'header' => "Authorization: Bearer $apiKey"
    ]
]);

header('Content-Type: image/png');
readfile($url, false, $context);
?>

Common QR Code Types

URL

bash
curl "...&data=https://example.com" -o url.png

Email

bash
curl "...&data=mailto:hello@example.com" -o email.png

Phone Number

bash
curl "...&data=tel:+1234567890" -o phone.png

SMS

bash
curl "...&data=sms:+1234567890?body=Hello" -o sms.png

WiFi Network

bash
curl "...&data=WIFI:T:WPA;S:NetworkName;P:password;;" -o wifi.png

vCard Contact

bash
curl "..." \
  --data-urlencode "data=BEGIN:VCARD
VERSION:3.0
N:Doe;John
FN:John Doe
TEL:+1234567890
EMAIL:john@example.com
END:VCARD" \
  -o contact.png

Geographic Location

bash
curl "...&data=geo:40.7128,-74.0060" -o location.png

Calendar Event

bash
curl "..." \
  --data-urlencode "data=BEGIN:VEVENT
SUMMARY:Meeting
DTSTART:20240115T100000Z
DTEND:20240115T110000Z
LOCATION:Office
END:VEVENT" \
  -o event.png

Error Handling

JavaScript

javascript
async function generateQR(data) {
  try {
    const response = await fetch(
      `https://www.qrcodeapi.io/api/generate?data=${encodeURIComponent(data)}`,
      { headers: { 'Authorization': `Bearer ${API_KEY}` } }
    );
    
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message);
    }
    
    return response.blob();
  } catch (error) {
    console.error('Error generating QR:', error.message);
    throw error;
  }
}

Python

python
import requests

def generate_qr(data):
    try:
        response = requests.get(
            'https://www.qrcodeapi.io/api/generate',
            params={'data': data},
            headers={'Authorization': f'Bearer {API_KEY}'}
        )
        response.raise_for_status()
        return response.content
    except requests.exceptions.HTTPError as e:
        error = response.json()
        print(f"Error: {error['message']}")
        raise