tayyab / N8N_GUIDE.md
mabdullahsibghatullah123's picture
Upload 17 files
5ae7c8f verified

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.).

  1. Download the updated workflow.json from this project.
  2. Go to your n8n dashboard.
  3. Click Workflows > Import from File.
  4. Select workflow.json.
  5. Activate the workflow.
  6. 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!
  7. 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

  1. Import workflow_real.json into N8N.
  2. Double-click Google Sheets (Add) and Google Sheets (Read) nodes.
  3. Authenticate with your Google Account ("Sign in with Google").
  4. Select your Spreadsheet and Sheet Name.
    • For Add Node: Map the fields (name -> {{$json.body.name}}, etc).
  5. 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 (and GET if you want a single entry point, but easier to use distinct hooks).
  • Authentication: None (for this demo) or Header Auth.

Endpoint 1: Add Item

  1. Webhook Node: Listen for POST on /add-item.
  2. AI Node (Optional): Use an OpenAI node to predict expiry date based on the "Name" if "ExpiryDate" is empty.
  3. Google Sheets Node: "Append" row to Sheet "Inventory".
    • Map: Name, Quantity, ExpiryDate, Category.
  4. Respond to Webhook Node: Return { "success": true }.

Endpoint 2: Get Inventory

  1. Webhook Node: Listen for GET on /get-items.
  2. Google Sheets Node: "Read" all rows from Sheet "Inventory".
  3. Code Node: Calculate daysRemaining for each item.
  4. Respond to Webhook Node: Return JSON { "items": [...] }.

Endpoint 3: Dashboard Stats

  1. Webhook Node: Listen for GET on /dashboard-stats.
  2. Google Sheets Node: Read all rows.
  3. Code Node:
    • Count total items.
    • Filter items where expiryDate is within 3 days.
    • Calculate potential waste savings.
  4. Respond to Webhook Node: Return Stats JSON.

Endpoint 4: Suggest Recipes

  1. Webhook Node: Listen for POST on /suggest-recipes.
  2. Google Sheets Node: Read "Inventory" (find items expiring soon).
  3. OpenAI Node (or other LLM):
    • System Prompt: "You are a chef. Suggest 3 recipes based on these ingredients: [List of ingredients]. Return valid JSON."
  4. Respond to Webhook Node: Return the JSON list of recipes.

Connecting to Frontend

  1. After activating your workflow, copy the Production URL of your Webhook nodes.
  2. If you used different URLs for each endpoint, you might need to adjust app.js or use a single Router workflow (recommended).
    • Router Approach: Have one Webhook URL and route based on body.action or query param?
    • Current App Support: The app.js assumes a base URL structure:
      • BASE_URL/add-item
      • BASE_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.