Learning Javascript, barcode scanning and wiggling into private grocery store APIs
Tags: api, javascript, barcodescanner, networksniffer
Published: 2019-04-06 03:38:46

---
title: Learning Javascript, barcode scanning and wiggling into private grocery store APIs
published: true
description: Come with me on an adventure
tags: api, javascript, barcode scanner, network sniffer
---

I've been a backend web developer for several years now. Swimming in the waters of databases, object oriented programming, and beautiful frameworks like Laravel to create some fairly robust web software for the company I work for, DieselCore. But here I was, realizing that learning a new programming language was well overdue. Sure, I had worked with Javascript plugins and packages before, I considered myself to be capable enough to figure the implementation part out even if I didn't really understand the language itself.

I bit the bullet and signed up for [Wes Bos](https://twitter.com/wesbos)' free 30 day javascript course, [Javascript 30](https://javascript30.com). From the day I write this post to when I signed up has been exactly 14 days, and I'm still only halfway through. That has roughly been the extent of my javascript training, in truth, with only a few other small introductions to the language in passing.

That's why I feel proud to have created what I did in such a short time frame. A working web application that scans barcodes, talks to the grocery chain's non-public API, adds products to a list, calculates the total and factors in sales tax.

#### First-world problems

Let me back up a few steps to a couple of days ago. My wife, Kaitlynn, and I are having dinner while our kids play and she drops a real first-world problem on me: "I *hate* going to [HEB](https://www.heb.com) (our local grocery store) because I can never tell what our total will be until I'm at the checkout. By then I may decide something in my basket isn't worth it." I grin, "oh, that's terrible." She laughs and continues, "but if I use the app to order, I feel cheated because we end up focusing on the price and eating the same food over and over. I just miss out on the experience to see new foods that spark fresh meal ideas if I'm not there."

I'll admit, I have a personal love for web APIs, having built several, myself. I begin to think of solutions. "I'm sure there's an app for what you want", I say. I look it up, and there is, but it's limited to locations that are nowhere near ours. Even if it's a first-world problem, I like to problem-solve, and my wife is a fantastic homemaker. If I can make her work even a little bit easier with what I do, I'm hooked.

I check out the HEB app and sure enough: they have barcode scanning. I look up a barcode and try it run it through HEB's web site search - no dice, UPC codes don't return results. But I **know** there is an endpoint out there serving up product results for UPC codes. But how do I find out where it is?

#### A clever solution

I remember something my father-in-law always talked about: WiFi honeypots. With the right software you can view network traffic. So, I hatched a plan. I would monitor my home network traffic while using the app and try to catch the outgoing API call.

After awhile I finally settled on the simplest solution: an iOS app called [Thor](https://itunes.apple.com/us/app/thor-http-sniffer-capture/id1210562295?mt=8). I started the network sniffer, opened the app and scanned. I crossed my fingers and began scrolling through the network logs hoping I would strike gold.

![alt text](https://i.imgur.com/b4928Mp.png "Thor, an iOS network sniffer helped me discover HEB's api endpoint")

**Bingo!** I had an endpoint, and more importantly, I had an api key. This was starting to look like a real possibility now.

I made a few test calls and it worked perfectly. I got to work building the interface. I opted to use the fantastic CSS framework, [Tailwind](https://tailwindcss.com), for my UI. I hooked up all the Javascript components using what I had learned in the prior 14 days and things started looking good.

![alt text](https://i.imgur.com/fD2whud.png "Tailwind CSS and FontAwesome create some effortlessly stunning web apps")

I did encounter some issues while attempting to utilize Javascript's ``fetch`` function, namely [CORs](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) errors. So, for the remote API calls I whipped up a quick PHP script to send cURL calls on behalf of my javascript application. That was the extent of PHP's involvement in this project!

#### Smile for the camera

Now all I had to do was find a way to use a device's camera to convert a barcode and pass it to the API. I had been avoiding this part of the project as much as possible because I knew the limitations of my knowledge well enough to know barcode scanning was more advanced than I could handle right now.

Luckily, there are some really solid JS frameworks out there that handle this very thing. After some digging I found [Quagga JS](https://serratus.github.io/quaggaJS). I had a little bit of trouble getting the configuration right, mainly because the examples they provided didn't 100% match the source code they used, but eventually I got it working.

Here's a fun fact for anyone who has never worked with a device's camera permissions with Javascript: **You need an SSL certificate**. The browser won't even request access if your script is running on an unprotected protocol. For me, fixing this problem was a cinch because I use [Valet](https://laravel.com/docs/5.8/valet) to run my local development server. `valet secure <folder>` and done. I was running https protocol. It was just that easy.

In less than 2 full days I had a working beta version of the web app which promised to cure my wife's shopping woes. See for yourself. Click the image below to watch it in action.

[![View the HEB barcode scanning web app](https://i.vimeocdn.com/video/773388894.jpg)](https://vimeo.com/328779418 "View the HEB barcode scanning web app - Click to watch!")

Thanks for taking the time to share in my story. If you're a developer, I hope this inspires you to go out and make something great.

#### Technologies used

* [Thor](https://itunes.apple.com/us/app/thor-http-sniffer-capture/id1210562295?mt=8) - iOS network sniffer app
* [Tailwind CSS](https://tailwindcss.com) - css framework for ui development
* [Font Awesome](https://fontawesome.com/) - icons
* [Quagga JS](https://serratus.github.io/quaggaJS) - camera access and barcode scanning
* [Valet](https://laravel.com/docs/5.8/valet) - local web server w/ ssl support
* [HEB](https://www.heb.com) - for their kind understanding that I borrowed their api 😅

#### By the way...
HEB, if you're reading this, I'm open for contract web development work, I'm great with APIs 😉

##### I love to share

I have several free open source packages available for download on my [Github profile](https://github.com/mtownsend5512?tab=repositories). Check them out and see if any of them can save you time in your next project!