N8N Setup Guide for Food Waste Assistant
This guide helps you set up the backend logic using n8n. You will need to create a new workflow (or multiple) to handle the 4 main API endpoints.
Prerequisites
- An active n8n instance (Cloud or Self-hosted).
- A database (Google Sheets, Airtable, or Postgres) to store inventory. This guide assumes Google Sheets for simplicity.
Quick Start (Import Workflow)
I have updated workflow.json to be much simpler. It now uses a Single Webhook URL that handles everything (add_item, get_items, etc.).
- Download the updated
workflow.jsonfrom this project. - Go to your n8n dashboard.
- Click Workflows > Import from File.
- Select
workflow.json. - Activate the workflow.
- Open the "Webhook (Router)" node:
- Copy the Production URL.
- CRITICAL: Set Respond to "Using 'Respond to Webhook' Node".
- If you forget this, you will get "Unexpected End of JSON Input" error!
- Paste the URL into your app's Settings.
๐ Making It Real (Google Sheets)
To save data permanently, use workflow_real.json instead.
1. Prepare Google Sheet
Create a new Sheet driven by these columns in Row 1:
id | name | quantity | expiryDate | category
2. Import Real Workflow
- Import
workflow_real.jsoninto N8N. - Double-click Google Sheets (Add) and Google Sheets (Read) nodes.
- Authenticate with your Google Account ("Sign in with Google").
- Select your Spreadsheet and Sheet Name.
- For Add Node: Map the fields (
name->{{$json.body.name}}, etc).
- For Add Node: Map the fields (
- Activate and copy the new Webhook URL to your app settings.
Workflow Details
Below are the details if you want to build it manually or understand how it works.
Create a workflow with a Webhook node.
- Method:
POST(andGETif you want a single entry point, but easier to use distinct hooks). - Authentication: None (for this demo) or Header Auth.
Endpoint 1: Add Item
- Webhook Node: Listen for
POSTon/add-item. - AI Node (Optional): Use an OpenAI node to predict expiry date based on the "Name" if "ExpiryDate" is empty.
- Google Sheets Node: "Append" row to Sheet "Inventory".
- Map:
Name,Quantity,ExpiryDate,Category.
- Map:
- Respond to Webhook Node: Return
{ "success": true }.
Endpoint 2: Get Inventory
- Webhook Node: Listen for
GETon/get-items. - Google Sheets Node: "Read" all rows from Sheet "Inventory".
- Code Node: Calculate
daysRemainingfor each item. - Respond to Webhook Node: Return JSON
{ "items": [...] }.
Endpoint 3: Dashboard Stats
- Webhook Node: Listen for
GETon/dashboard-stats. - Google Sheets Node: Read all rows.
- Code Node:
- Count total items.
- Filter items where
expiryDateis within 3 days. - Calculate potential waste savings.
- Respond to Webhook Node: Return Stats JSON.
Endpoint 4: Suggest Recipes
- Webhook Node: Listen for
POSTon/suggest-recipes. - Google Sheets Node: Read "Inventory" (find items expiring soon).
- OpenAI Node (or other LLM):
- System Prompt: "You are a chef. Suggest 3 recipes based on these ingredients: [List of ingredients]. Return valid JSON."
- Respond to Webhook Node: Return the JSON list of recipes.
Connecting to Frontend
- After activating your workflow, copy the Production URL of your Webhook nodes.
- If you used different URLs for each endpoint, you might need to adjust
app.jsor use a single Router workflow (recommended).- Router Approach: Have one Webhook URL and route based on
body.actionor query param? - Current App Support: The
app.jsassumes a base URL structure:BASE_URL/add-itemBASE_URL/get-items- etc.
- Tip: In n8n, you can set the path for the Webhook node. Ensure you set 4 separate Webhook nodes with these specific suffixes, or use a Reverse Proxy to route them.
- Router Approach: Have one Webhook URL and route based on