I built a Food Nutrition Data Tool that uses NLP and connected it to an API — Github Link Included

Robert O’Connor
5 min readFeb 12, 2021

Important links referenced in this post

  1. The source code for this project is here
  2. API used in the project is here

Let’s cut to the chase, you want to see how it works and what it’s capable of, understandable, I'm a no-BS man myself and I really don’t like waffling on and wasting people's time.

So I'm going to try to provide as much value in this post as possible. I think it’s important for you to understand why I built this and why I think it’s so powerful. Maybe you can take this and improve it, monetize it or build a business focused around this service.

Prerequisites

  1. Node.js installed locally on your machine. You can get it here.
  2. Ability to follow the simple instructions that open and run the project locally.

What does it do?

I’ve named this tool Nutri-S, the “S” standing for Service, and it provides detailed nutritional information about any food you request. It uses NLP (Natural Language Processing) to take any written sentence such as “One egg and 100 grams of chicken”, extract the important information such as “Food name”, “Serving size”, “Amount” and responds with a nicely formatted breakdown of over 70 nutrition metrics including vitamins, fats, amino acids, calories, and protein for that exact food amount.

open-source web app for this food data

Why I built this?

Honestly, I’ve always had an interest in nutrition and the effects it has on human health, the pandemic was the perfect catalyst to propel me into building a service that would give me and others access to powerful nutritional data that would allow us to build cool nutrition-focused products and make better food choices in our lives.

Install

Head over to my GitHub here and download (Clone if you’re techie) the repository onto your local machine. Once you’ve done that navigate into the project and install the dependencies.

git clone https://github.com/HealthyTechGuy/foodNutritionDataTool.gitcd foodNutritionDataToolnpm i

Get your API key

I have set up this application to use the Nutrition Service on Rapid API, head over to RapidAPI here and subscribe to a plan to get the API key which you’ll need for the next step.

In the config.json file, you’ll see the following key-value pair.

{"NutriSRapidAPIKey" : "REPLACE-WITH-YOUR-RAPID-API-KEY-HERE"}

You guessed it! replace “REPLACE-WITH-YOUR-RAPID-API-KEY-HERE” with the API key you just got from RapidAPI.

Run the project

Ensuring you’re inside the project directory, run:

npm start

Then you can click here which will take you to “http://localhost:3000” and you should be able to see the UI as shown below.

Enough Jabber, let’s see a Demo

The web app you see below is something I built in a couple of days so please keep the grilling for your next socially distanced BBQ. I don’t consider myself an experienced React developer, that being said I know my way around React enough to build this simple web app. The bulk of my time and effort was spent building the complex API that powers all this nutrition magic in the backend.

Let’s test out a simple request first with “One Apple” -

Scroll down to see our results:

You’ll notice that the card on the left and the Pie Chart have been populated with some data, hovering over it we can see all the nutritional information that has been returned as shown here:

Examining the nutrition data

Now that’s all well and good, but realistically people are going to want to use more complex sentences.

Food by weight in grams?

That’s easy, simply put in the number of grams and the food that we want the data for, for example:

Notice the result in the Nutrition Card (Serving Size 100g) is now calculating nutritional data for 100 grams of this food:

Information about drinks?

“One can of coke” will work without any problems, however, unsurprisingly there’s not much nutritional data:

So what kind of data are we actually getting here?

Well, I’ve taken the liberty of including an expandable accordion which is what “See Raw Nutrition Data In JSON” is for, click on this and you’ll be able to see exactly what we are getting from the API.

I’ve also logged this response out so you can also see it in the Developer Tools:

The Food Data In Raw JSON format

Of course, each food is different and will return a different number of nutritional metrics.

I'm sure I don’t need to explain the power of this any further, there are many scenarios I can think of that would benefit from on-demand Nutritional data but I’ll keep those ideas to myself 😉.

What about the Data Quality?

As you can probably imagine, a lot of this data is publicly available information, all you need to do is google a food name followed by “Nutrition” and you’ll get a plethora of results. This API aggregates and compares all these results to find the most accurate, and it is surprisingly accurate.

What about the Cost?

One of the reasons I wanted to build this was because there really aren’t any similar services that are affordable, do your own due diligence and see for yourself on RapidAPI.

I hope you found this useful, let me know if you build anything cool, and feel free to contribute to the open-source project on my GitHub or get in touch with me here.

--

--

Robert O’Connor

Writing about building businesses and simplifying software development. https://rob-oconnor.com