nextjs 14 api route exampleapi route in next js simple example code and demo output

Today I am going to tackle a very simple problem of QR code decoding with Next.js 14 API routes example program. We will create a very simple API route for QR code decoding. We are using the jsQR library. As with the new app router support in the Next.js, now the API routes are written with the route.js files inside any folder of your choice. Which means now any route could serve it’s own API. You can also write server functions which we explain in one of our previous posts. But we are not going to cover that part in this. Our main goal is to use ESP32-CAM to send an image to this API route and that API route will return with the decoding string back to ESP32-CAM.

In today’s post we will stick to the Next.js part of the project so we don’t get distracted. First of all we have to create a new next.js application. Which could be done with simple command like follows

npx create-next-app@latestCode language: CSS (css)

Which will result in bunch of questions in the command prompt like this image showing the results.

Once done with that, open the folder with the VS Code.

Now create a new api directory under the src \app folder. Inside that api folder, create a new file name it route.js. Because we disabled the typescript support while creating the new project, so we are using the javascript only.

Inside that route.js file create a simple hello world route with GET method like this.

export async function GET(req, res) {
    const name = req.nextUrl.searchParams.get("name") || "World";
    return NextResponse.json({ message: `Hello, ${name}!` });
Code language: JavaScript (javascript)

This will serve a simple hello world api end point which you can access with like this.

Accessing Base64 Image

Now we have to accep the base64 image in our API and have to convert that back into the image data. There are bunch of libraries and methods to accomplish this but we are going to be as simple as possible and the approach we are using is described in this jsQR issue page.

export async function POST(req) {
    const data = await req.json();
    const base64Image = data.img;
}Code language: JavaScript (javascript)

This is a simple POST method which get the image data provided with the img field as json input to the API end point.

Decode Base64 QR Code Image

Now that we have a base64 image we can try to decode the image with the jsQR library. first we have to import that library then pass an image data to the instance and it will return a bunch of output. We can simply decode that output and get our requried data. This is how we updated our POST route above.

export async function POST(req) {
    const data = await req.json();
    const base64Image = data.img;
    const imageBuffer = Buffer.from(base64Image, 'base64');
    if (imageBuffer[0] === 0x89 && imageBuffer[1] === 0x50) {
      } else if (imageBuffer[0] === 0xFF && imageBuffer[1] === 0xD8) {
      } else {
        throw new Error('Unsupported image format');

        const qrArray = getImageDataFromBuffer(imageBuffer);
        const code = jsQR(, qrArray.width, qrArray.height);

    return NextResponse.json({ message: });
Code language: JavaScript (javascript)

Test API with PostMan

Now we are ready to test our API end point with the postman. We simply pass the base64 encoded as raw input to the API end point. Here is the sample input which we given to the API for our QR code image. We used this simple image to base64 online encoder to generate our base64 encoding. To view and verify that our base64 encoding is correctly working we can use a base64 Image viewer freely available online to validate our base64 encoding. Here is the sample input we provided for the test case.

}Code language: JSON / JSON with Comments (json)

the output looks like this in the postman software.

qr code decoding api testing in next js 14 app router

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.