Skip to content

Building a serverless backend for ingesting data from a smartphone

Published:  at  12:40 PM

In this guide we will explore the process of creating a serverless backend solution that efficiently stores and manages data captured by smartphones, particularly focusing on scanned QR codes.

Table of contents

Open Table of contents

Background

QR codes are increasing used everywhere, rightfully so because of its efficiency and ease of use. I wanted to find a way to store and retrieve QR codes after having scanned them. Having a way to revisit a single place to check and retrieve all my previously scanned codes in an accessible manner was something that peaked my interests.

Introduction

Android has an inbuilt QR code scanner that comes bundled with Google Play Services. Also, there exists a myriad applications available to scan and decode the data from a given image of any type, including images captured by your device’s camera or saved on disk.

Binary eye is an open source project that provides QR code scanning capabilities for Android. It can be used to scan and decode barcodes, including the popular Code 128 format commonly found in retail environments. It also includes an option to forward the scanned data from within Binary Eye to any URL of your choice. A webhook of sorts. You just set it and forget it.

I just had to build a way to capture and ingest data emitted by binary eye.

Setup

First off, for building a serverless handler, I decided to stick with Cloudflare workers. As the logic was simple and could be easily handled by a worker. Hono is a great framework that helps build serverless functions that can be deployed on a lot of platforms, including cloudflare workers.

For storage, tursoDB fits my requirements perfectly. Built on top of sqlite, it has a generous free tier and also works well in serverless environments.

Architecture overview

Here’s a snippet of the worker that handles the incoming request:

app.post("/qr", async c => {
  const data = await c.req.json<QrData>();
  const db = c.get("db");

  console.log("qr data", data);

  const res = await db.insert(qrTable).values({
    content: data.content,
    raw: data.raw,
    timestamp: data.timestamp.toLocaleString(),
  });

  if (!res.lastInsertRowid) {
    return c.json(
      {
        status: "error",
        message: "Faile to Save QR",
      },
      500
    );
  }
  return c.json({
    status: "OK",
    message: "Saved QR",
  });
});
export default app;

I used drizzle for the ORM, which also has good documentation on how to use it with Turso.

Conclusion

It does seem like an overkill at first to put in so much effort into storing QR codes. However, I think its a good starting point for me. I can now use the same approach to store and retrieve other data from my phone.

I am that kind of a person who messes with my phone quite often. It’s nice to have a place where I can store all sorts of information without having to worry about it being lost or accidentally erased.

I plan on using it to for other use cases. For instance, I can store my SMS data and bookmarked location data on maps or even just other URL’s that I want to preserve for later.



Previous Post
Building a stateful serverless feature flag application on the edge
Next Post
Part-1: Phone In A Box Series