Arunava Karmakar
3 min readJul 8, 2023

Guide on Making a Simple QR Code API using Python

Introduction:

In today’s digital world, Quick Response (QR) codes have become increasingly popular for a variety of applications. From sharing URLs to making payments, QR codes provide a convenient way to store and transmit information. In this blog post, we will walk through the process of creating a simple QR code API using Python. By the end of this guide, you will have a basic understanding of how to generate QR codes programmatically and expose them through a RESTful API.

Prerequisites:

Before diving into the implementation, make sure you have the following prerequisites:

1. Python: Ensure that you have Python installed on your machine. You can download the latest version from the official Python website.

2. Flask: We will be using the Flask web framework to create the API. You can install Flask by running the command `pip install flask` in your terminal.

3. qrcode: To generate QR codes, we will be using the qrcode library. Install it by running the command `pip install qrcode`.

Step 1: Setting up the Flask API:

To begin, create a new directory for your project and navigate to it using the command line. Inside the project directory, create a new Python file called `app.py`.

Import the necessary modules at the top of your `app.py` file:

from flask import Flask, request
import qrcode

Next, create a new instance of the Flask application:

app = Flask(__name__)

Step 2: Creating the QR code generation endpoint:
We will now define an endpoint that generates QR codes based on the input data. Add the following route decorator above a function called `generate_qr_code()`:

@app.route('/qr_code', methods=['POST'])
def generate_qr_code():
data = request.form.get('data')
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
qr.add_data(data)
qr.make(fit=True)
qr_img = qr.make_image(fill_color="black", back_color="white")
qr_img.save('qr_code.png')
return 'QR code generated!'

This endpoint accepts a POST request with the input data in the request body. The `qrcode` library is then used to generate the QR code image based on the provided data. The image is saved as `qr_code.png` in the project directory.

Step 3: Running the API:
To start the Flask API, add the following code at the end of `app.py`:

if __name__ == '__main__':
app.run()

Save the file and run it from the command line using the command `python app.py`. The Flask development server should start, and you’ll see an output similar to `Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)`.

Step 4: Testing the API:
You can now test the API using a tool like cURL or Postman. Send a POST request to `http://127.0.0.1:5000/qr_code` with the following form data:

data: Hello, World!

Upon successful execution, you will see the message “QR code generated!” in the response.

Step 5: Viewing the Generated QR Code:
To view the generated QR code, open the project directory and locate the file `qr_code.png`. You can use an image viewer to see the QR code image representing the input data.

Conclusion:
In this blog post, we have walked through the process of creating a simple QR code API using Python. We used the

Flask web framework to create the API and the qrcode library to generate QR codes programmatically. By following the steps outlined in this guide, you should now have a basic understanding of how to build your own QR code API. From here, you can extend the functionality by adding error handling, authentication, or integrating with external services. QR codes have a wide range of applications, and with this newfound knowledge, you can explore their potential in your projects.

Arunava Karmakar

I am game developer and a hobby programmer who likes to learn new skills